Skip to content

Commit 5cb049a

Browse files
committed
Added null networkedVar handling to inspector drawing
1 parent 7e83b74 commit 5cb049a

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

MLAPI-Editor/NetworkedBehaviourEditor.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using MLAPI;
5+
using MLAPI.NetworkedVar;
56
using UnityEngine;
67

78
namespace UnityEditor
@@ -51,6 +52,15 @@ void RenderNetworkedVar(int index)
5152
MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
5253
Init(targetScript);
5354
}
55+
56+
object value = networkedVarFields[networkedVarNames[index]].GetValue(target);
57+
if (value == null)
58+
{
59+
Type fieldType = networkedVarFields[networkedVarNames[index]].FieldType;
60+
INetworkedVar var = (INetworkedVar) Activator.CreateInstance(fieldType, true);
61+
networkedVarFields[networkedVarNames[index]].SetValue(target, var);
62+
}
63+
5464
Type type = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
5565
Type genericType = type.GetGenericArguments()[0];
5666

@@ -83,19 +93,19 @@ void RenderNetworkedVarValueType<T>(int index) where T : struct
8393
if (type == typeof(int))
8494
val = EditorGUILayout.IntField(name, (int)val);
8595
else if (type == typeof(uint))
86-
val = EditorGUILayout.LongField(name, (long)((uint)val));
96+
val = (uint)EditorGUILayout.LongField(name, (long)((uint)val));
8797
else if (type == typeof(short))
88-
val = EditorGUILayout.IntField(name, (int)((short)val));
98+
val = (short)EditorGUILayout.IntField(name, (int)((short)val));
8999
else if (type == typeof(ushort))
90-
val = EditorGUILayout.IntField(name, (int)((ushort)val));
100+
val = (ushort)EditorGUILayout.IntField(name, (int)((ushort)val));
91101
else if (type == typeof(sbyte))
92-
val = EditorGUILayout.IntField(name, (int)((sbyte)val));
102+
val = (sbyte)EditorGUILayout.IntField(name, (int)((sbyte)val));
93103
else if (type == typeof(byte))
94-
val = EditorGUILayout.IntField(name, (int)((byte)val));
104+
val = (byte)EditorGUILayout.IntField(name, (int)((byte)val));
95105
else if (type == typeof(long))
96106
val = EditorGUILayout.LongField(name, (long)val);
97107
else if (type == typeof(ulong))
98-
val = EditorGUILayout.LongField(name, (long)((ulong)val));
108+
val = (ulong)EditorGUILayout.LongField(name, (long)((ulong)val));
99109
else if (type == typeof(bool))
100110
val = EditorGUILayout.Toggle(name, (bool)val);
101111
else if (type == typeof(string))

0 commit comments

Comments
 (0)