Skip to content

Commit 94a4bf6

Browse files
fix: 2+ inheritance from network behaviour causes compilation exception (#1078) (#1079)
* fix: 2+ inheritance from network behaviour causes compilation exception (#1078) * Update com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs Co-authored-by: M. Fatih MAR <[email protected]> * updated comment based on feedback Co-authored-by: M. Fatih MAR <[email protected]>
1 parent d6d4bd0 commit 94a4bf6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
595595

596596
// override NetworkBehaviour.__getTypeName() method to return concrete type
597597
{
598-
var baseType = typeDefinition.BaseType.Resolve();
599-
var baseGetTypeNameMethod = baseType.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getTypeName)));
598+
var networkBehaviour_TypeDef = m_NetworkBehaviour_TypeRef.Resolve();
599+
var baseGetTypeNameMethod = networkBehaviour_TypeDef.Methods.First(p => p.Name.Equals(nameof(NetworkBehaviour.__getTypeName)));
600600

601601
var newGetTypeNameMethod = new MethodDefinition(
602602
nameof(NetworkBehaviour.__getTypeName),

com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,30 @@ public void AccessNetworkObjectTest()
4747
}
4848

4949
[Test]
50-
public void GetNetworkBehaviourNameTest()
50+
public void GivenClassDerivesFromNetworkBehaviour_GetTypeNameReturnsCorrectValue()
5151
{
52-
var gameObject = new GameObject(nameof(GetNetworkBehaviourNameTest));
52+
var gameObject = new GameObject(nameof(GivenClassDerivesFromNetworkBehaviour_GetTypeNameReturnsCorrectValue));
5353
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
5454

5555
Assert.AreEqual(nameof(EmptyNetworkBehaviour), networkBehaviour.__getTypeName());
5656
}
5757

58+
[Test]
59+
public void GivenClassDerivesFromNetworkBehaviourDerivedClass_GetTypeNameReturnsCorrectValue()
60+
{
61+
var gameObject = new GameObject(nameof(GivenClassDerivesFromNetworkBehaviourDerivedClass_GetTypeNameReturnsCorrectValue));
62+
var networkBehaviour = gameObject.AddComponent<DerivedNetworkBehaviour>();
63+
64+
Assert.AreEqual(nameof(DerivedNetworkBehaviour), networkBehaviour.__getTypeName());
65+
}
66+
67+
// Note: in order to repro https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1078
68+
// this child class must be defined before its parent to assure it is processed first by ILPP
69+
public class DerivedNetworkBehaviour : EmptyNetworkBehaviour
70+
{
71+
72+
}
73+
5874
public class EmptyNetworkBehaviour : NetworkBehaviour
5975
{
6076

0 commit comments

Comments
 (0)