|
1 | 1 | using System.Text.RegularExpressions;
|
2 | 2 | using NUnit.Framework;
|
| 3 | +using Unity.Netcode.Editor; |
3 | 4 | using UnityEngine;
|
4 | 5 | using UnityEngine.TestTools;
|
5 | 6 |
|
@@ -64,9 +65,95 @@ public void GetBehaviourIndexOne()
|
64 | 65 | Object.DestroyImmediate(gameObject);
|
65 | 66 | }
|
66 | 67 |
|
| 68 | + /// <summary> |
| 69 | + /// Verifies that a NetworkObject component that is positioned after a NetworkBehaviour component will |
| 70 | + /// be migrated to a component index value that is before the lowest NetworkBehaviour component index value. |
| 71 | + /// (The lowest NetworkBehaviour component's index value will also change when this happens) |
| 72 | + /// </summary> |
| 73 | + [Test] |
| 74 | + public void NetworkObjectComponentOrder() |
| 75 | + { |
| 76 | + var gameObject = new GameObject(nameof(GetBehaviourIndexOne)); |
| 77 | + // Add the Networkbehaviour first |
| 78 | + var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>(); |
| 79 | + // Add an empty MonoBehaviour inbetween the NetworkBehaviour and NetworkObject |
| 80 | + gameObject.AddComponent<EmptyMonoBehaviour>(); |
| 81 | + // Add the NetworkObject |
| 82 | + var networkObject = gameObject.AddComponent<NetworkObject>(); |
| 83 | + var componentIndices = GetIndices(gameObject); |
| 84 | + |
| 85 | + // Verify the NetworkObject procedes the NetworkBehaviour |
| 86 | + Assert.True(componentIndices.NetworkObjectIndex > componentIndices.NetworkBehaviourIndex, $"[Initial Setup] NetworkObject index ({componentIndices.NetworkObjectIndex}) is not greater than the NetworkBehaviour index ({componentIndices.NetworkBehaviourIndex})!"); |
| 87 | + |
| 88 | + // Force-Invoke the CheckForNetworkObject method in order to verify the NetworkObject is moved |
| 89 | + NetworkBehaviourEditor.CheckForNetworkObject(gameObject); |
| 90 | + var adjustedIndices = GetIndices(gameObject); |
| 91 | + |
| 92 | + Assert.True(ValidateComponentIndices(componentIndices, GetIndices(gameObject)), "NetworkObject did not get migrated below the NetworkBehaviour!"); |
| 93 | + |
| 94 | + // Cleanup |
| 95 | + Object.DestroyImmediate(gameObject); |
| 96 | + } |
| 97 | + |
| 98 | + private bool ValidateComponentIndices(ComponentIndices previous, ComponentIndices current) |
| 99 | + { |
| 100 | + if (previous.NetworkObjectIndex != current.NetworkObjectIndex && previous.NetworkBehaviourIndex != current.NetworkBehaviourIndex) |
| 101 | + { |
| 102 | + if (current.NetworkObjectIndex < previous.NetworkObjectIndex && current.NetworkObjectIndex < current.NetworkBehaviourIndex) |
| 103 | + { |
| 104 | + return true; |
| 105 | + } |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + private ComponentIndices GetIndices(GameObject gameObject) |
| 111 | + { |
| 112 | + // Get the index/order values for the added NetworkBehaviour and NetworkObject |
| 113 | + var components = gameObject.GetComponents<MonoBehaviour>(); |
| 114 | + var componentIndices = new ComponentIndices() |
| 115 | + { |
| 116 | + NetworkObjectIndex = -1, |
| 117 | + NetworkBehaviourIndex = -1 |
| 118 | + }; |
| 119 | + for (int i = 0; i < components.Length; i++) |
| 120 | + { |
| 121 | + if (componentIndices.NetworkObjectIndex != -1 && componentIndices.NetworkBehaviourIndex != -1) |
| 122 | + { |
| 123 | + break; |
| 124 | + } |
| 125 | + var component = components[i]; |
| 126 | + var networkObjectComponent = component as NetworkObject; |
| 127 | + if (networkObjectComponent != null) |
| 128 | + { |
| 129 | + componentIndices.NetworkObjectIndex = i; |
| 130 | + continue; |
| 131 | + } |
| 132 | + var networkBehaviourComponent = component as EmptyNetworkBehaviour; |
| 133 | + if (networkBehaviourComponent != null) |
| 134 | + { |
| 135 | + componentIndices.NetworkBehaviourIndex = i; |
| 136 | + continue; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return componentIndices; |
| 141 | + } |
| 142 | + |
| 143 | + private struct ComponentIndices |
| 144 | + { |
| 145 | + public int NetworkObjectIndex; |
| 146 | + public int NetworkBehaviourIndex; |
| 147 | + } |
| 148 | + |
67 | 149 | public class EmptyNetworkBehaviour : NetworkBehaviour
|
68 | 150 | {
|
69 | 151 |
|
70 | 152 | }
|
| 153 | + |
| 154 | + public class EmptyMonoBehaviour : MonoBehaviour |
| 155 | + { |
| 156 | + |
| 157 | + } |
71 | 158 | }
|
72 | 159 | }
|
0 commit comments