Skip to content

Commit e11135d

Browse files
committed
Added scene post processor to generate unique id for every object in a scene
1 parent 692c290 commit e11135d

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

MLAPI-Editor/MLAPI-Editor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Compile Include="NetworkedObjectEditor.cs" />
4141
<Compile Include="NetworkingManagerEditor.cs" />
4242
<Compile Include="Properties\AssemblyInfo.cs" />
43+
<Compile Include="ScenePostProcesser.cs" />
4344
<Compile Include="TrackedObjectEditor.cs" />
4445
</ItemGroup>
4546
<ItemGroup>

MLAPI-Editor/ScenePostProcesser.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using MLAPI;
4+
using MLAPI.Logging;
5+
using UnityEditor.Callbacks;
6+
using UnityEngine;
7+
8+
namespace UnityEditor
9+
{
10+
public class NetworkScenePostProcess : MonoBehaviour
11+
{
12+
[PostProcessScene]
13+
public static void OnPostProcessScene()
14+
{
15+
List<NetworkedObject> networkedObjects = FindObjectsOfType<NetworkedObject>().ToList();
16+
networkedObjects.Sort((n1, n2) => CompareSiblingPaths(GetSiblingsPath(n1.transform), GetSiblingsPath(n2.transform)));
17+
18+
for (int i = 0; i < networkedObjects.Count; i++)
19+
{
20+
networkedObjects[i].SceneId = (uint)i;
21+
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("PostProcessing for object \"" + networkedObjects[i].name +
22+
"\" completed on scene \"" + networkedObjects[i].gameObject.scene.name +
23+
"\" with objectSceneId \"" + i + "\"");
24+
}
25+
}
26+
27+
private static List<int> GetSiblingsPath(Transform transform)
28+
{
29+
List<int> result = new List<int>();
30+
while (transform != null)
31+
{
32+
result.Add(transform.GetSiblingIndex());
33+
transform = transform.parent;
34+
}
35+
36+
result.Reverse();
37+
return result;
38+
}
39+
40+
private static int CompareSiblingPaths(List<int> l1, List<int> l2)
41+
{
42+
while (l1.Count > 0 && l2.Count > 0)
43+
{
44+
if (l1[0] < l2[0]) return -1;
45+
if (l1[0] > l2[0]) return 1;
46+
47+
l1.RemoveAt(0);
48+
l2.RemoveAt(0);
49+
}
50+
return 0;
51+
}
52+
}
53+
}

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ internal set
8383
/// </summary>
8484
public bool isSpawned { get; internal set; }
8585
internal bool? sceneObject = null;
86+
[HideInInspector]
87+
[SerializeField]
88+
public uint? SceneId = null;
8689

8790
private void OnDestroy()
8891
{

MLAPI/NetworkingManagerComponents/Core/LogHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ public enum LogLevel
1010
Error,
1111
Nothing
1212
}
13-
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
1413

15-
internal static class LogHelper
14+
public static class LogHelper
1615
{
17-
internal static LogLevel CurrentLogLevel
16+
public static LogLevel CurrentLogLevel
1817
{
1918
get
2019
{
@@ -25,8 +24,9 @@ internal static LogLevel CurrentLogLevel
2524
}
2625
}
2726

28-
internal static void LogInfo(string message) => Debug.Log("[MLAPI] " + message);
29-
internal static void LogWarning(string message) => Debug.LogWarning("[MLAPI] " + message);
30-
internal static void LogError(string message) => Debug.LogError("[MLAPI] " + message);
27+
public static void LogInfo(string message) => Debug.Log("[MLAPI] " + message);
28+
public static void LogWarning(string message) => Debug.LogWarning("[MLAPI] " + message);
29+
public static void LogError(string message) => Debug.LogError("[MLAPI] " + message);
3130
}
31+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
3232
}

0 commit comments

Comments
 (0)