1+ using System . Collections ;
12using NUnit . Framework ;
23using UnityEngine ;
4+ using UnityEngine . TestTools ;
35using Object = UnityEngine . Object ;
46
57namespace Unity . Netcode . EditorTests
@@ -35,8 +37,6 @@ public void AccessNetworkObjectTest()
3537 var gameObject = new GameObject ( nameof ( AccessNetworkObjectTest ) ) ;
3638 var networkBehaviour = gameObject . AddComponent < EmptyNetworkBehaviour > ( ) ;
3739
38- networkBehaviour . NoNetworkRpc ( ) ;
39-
4040 Assert . That ( networkBehaviour . NetworkObject , Is . Null ) ;
4141
4242 var networkObject = gameObject . AddComponent < NetworkObject > ( ) ;
@@ -71,6 +71,34 @@ public void AccessNetworkObjectTestInDerivedClassWithOverrideFunctions()
7171 Object . DestroyImmediate ( gameObject ) ;
7272 }
7373
74+ [ UnityTest ]
75+ public IEnumerator RpcShouldNoopWhenInvokedWithoutANetworkManagerSession ( )
76+ {
77+ var noNetworkError = "Rpc methods can only be invoked after starting the NetworkManager!" ;
78+ var gameObject = new GameObject ( nameof ( AccessNetworkObjectTestInDerivedClassWithOverrideFunctions ) ) ;
79+ var networkBehaviour = gameObject . AddComponent < RpcNetworkBehaviour > ( ) ;
80+
81+ // No networkManager exists so error should be logged
82+ LogAssert . Expect ( LogType . Error , noNetworkError ) ;
83+ networkBehaviour . NoNetworkRpc ( ) ;
84+
85+ // Ensure RPC was not invoked locally
86+ yield return null ;
87+ Assert . That ( networkBehaviour . RpcWasInvoked , Is . False ) ;
88+
89+ var networkManager = gameObject . AddComponent < NetworkManager > ( ) ;
90+ networkManager . SetSingleton ( ) ;
91+
92+ LogAssert . Expect ( LogType . Error , noNetworkError ) ;
93+ networkBehaviour . NoNetworkRpc ( ) ;
94+
95+ // Ensure RPC was not invoked locally
96+ yield return null ;
97+ Assert . That ( networkBehaviour . RpcWasInvoked , Is . False ) ;
98+
99+ Object . DestroyImmediate ( gameObject ) ;
100+ }
101+
74102 // Note: in order to repro https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1078
75103 // this child class must be defined before its parent to assure it is processed first by ILPP
76104 internal class DerivedNetworkBehaviour : EmptyNetworkBehaviour
@@ -79,10 +107,16 @@ internal class DerivedNetworkBehaviour : EmptyNetworkBehaviour
79107
80108 internal class EmptyNetworkBehaviour : NetworkBehaviour
81109 {
110+ }
111+
112+ internal class RpcNetworkBehaviour : NetworkBehaviour
113+ {
114+ public bool RpcWasInvoked ;
115+
82116 [ Rpc ( SendTo . Everyone ) ]
83117 public void NoNetworkRpc ( )
84118 {
85- Debug . Log ( "No Network Rpc" ) ;
119+ RpcWasInvoked = true ;
86120 }
87121 }
88122 }
0 commit comments