polygone/Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Editor/PostProcessScene.cs
2021-08-02 05:44:37 -04:00

67 lines
2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using MLAPI;
using UnityEditor.Callbacks;
using UnityEngine;
namespace UnityEditor
{
public class NetworkScenePostProcess : MonoBehaviour
{
[PostProcessScene(int.MaxValue)]
public static void ProcessScene()
{
// find all scene objects that have not been spawned yet
// TODO: long term, replace the means of finding candidate objects to repace FindObjectsOfType
var traverseSortedObjects = FindObjectsOfType<NetworkObject>().Where(x => x.IsSceneObject == null).ToList();
traverseSortedObjects.Sort((x, y) =>
{
List<int> xSiblingIndex = x.TraversedSiblingIndex();
List<int> ySiblingIndex = y.TraversedSiblingIndex();
while (xSiblingIndex.Count > 0 && ySiblingIndex.Count > 0)
{
if (xSiblingIndex[0] < ySiblingIndex[0])
{
return -1;
}
if (xSiblingIndex[0] > ySiblingIndex[0])
{
return 1;
}
xSiblingIndex.RemoveAt(0);
ySiblingIndex.RemoveAt(0);
}
return 0;
});
for (ulong i = 0; i < (ulong)traverseSortedObjects.Count; i++)
{
traverseSortedObjects[(int)i].NetworkInstanceId = i;
}
}
}
internal static class PrefabHelpers
{
internal static List<int> TraversedSiblingIndex(this NetworkObject networkObject)
{
var paths = new List<int>();
var transform = networkObject.transform;
while (transform != null)
{
paths.Add(transform.GetSiblingIndex());
transform = transform.parent;
}
paths.Reverse();
return paths;
}
}
}