NetworkBehaviourTests: GetSyncVarGameObjectOnServer

This commit is contained in:
vis2k 2020-03-07 18:29:54 +01:00
parent f51e587c67
commit c6c1a36559

View File

@ -115,6 +115,21 @@ public void SetSyncVarGameObjectExposed(GameObject newGameObject, ulong dirtyBit
} }
} }
// we need to inherit from networkbehaviour to test protected functions
public class NetworkBehaviourGetSyncVarGameObjectComponent : NetworkBehaviour
{
//[SyncVar]
public GameObject test;
// usually generated by weaver
public uint testNetId;
// SetSyncVarGameObject wrapper to expose it
public GameObject GetSyncVarGameObjectExposed()
{
return GetSyncVarGameObject(testNetId, ref test);
}
}
public class NetworkBehaviourTests public class NetworkBehaviourTests
{ {
GameObject gameObject; GameObject gameObject;
@ -1029,6 +1044,39 @@ public void SetSyncVarGameObjectZeroNetId()
GameObject.DestroyImmediate(test); GameObject.DestroyImmediate(test);
GameObject.DestroyImmediate(go); GameObject.DestroyImmediate(go);
} }
[Test]
public void GetSyncVarGameObjectOnServer()
{
// isServer is only true if we have a server running and netId set
Transport.activeTransport = Substitute.For<Transport>();
NetworkServer.Listen(1);
identity.netId = 42; // otherwise isServer is false
Assert.That(identity.isServer, Is.True);
// add test component
NetworkBehaviourGetSyncVarGameObjectComponent comp = gameObject.AddComponent<NetworkBehaviourGetSyncVarGameObjectComponent>();
comp.syncInterval = 0; // for isDirty check
// create a syncable GameObject
GameObject go = new GameObject();
NetworkIdentity ni = go.AddComponent<NetworkIdentity>();
ni.netId = 43;
// assign it in the component
comp.test = go;
comp.testNetId = ni.netId;
// get it on the server. should simply return the field instead of
// doing any netId lookup like on the client
GameObject result = comp.GetSyncVarGameObjectExposed();
Assert.That(result, Is.EqualTo(go));
// clean up
NetworkServer.Shutdown();
Transport.activeTransport = null;
GameObject.DestroyImmediate(go);
}
} }
// we need to inherit from networkbehaviour to test protected functions // we need to inherit from networkbehaviour to test protected functions