Skip to content

Commit 48272f2

Browse files
committed
Fixed NetworkedVar editor issues
1 parent 6ff2461 commit 48272f2

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

MLAPI-Editor/NetworkedBehaviourEditor.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using MLAPI;
2-
using MLAPI.Attributes;
1+
using MLAPI.Attributes;
32
using MLAPI.Data;
43
using MLAPI.MonoBehaviours.Core;
54
using System;
@@ -26,8 +25,13 @@ private void Init(MonoScript script)
2625
{
2726
initialized = true;
2827

28+
syncedVarNames.Clear();
29+
networkedVarNames.Clear();
30+
networkedVarFields.Clear();
31+
networkedVarObjects.Clear();
32+
2933
syncedVarLabelGuiContent = new GUIContent("SyncedVar", "This variable has been marked with the [SyncedVar] attribute.");
30-
networkedVarLabelGuiContent = new GUIContent("[NetworkedVar]", "This variable has been marked with the [SyncedVar] attribute.");
34+
networkedVarLabelGuiContent = new GUIContent("NetworkedVar", "This variable is a NetworkedVar. It can not be serialized and can only be changed during runtime.");
3135

3236
FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
3337
for (int i = 0; i < fields.Length; i++)
@@ -47,6 +51,16 @@ private void Init(MonoScript script)
4751

4852
void RenderNetworkedVar(int index)
4953
{
54+
if (!networkedVarFields.ContainsKey(networkedVarNames[index]))
55+
{
56+
serializedObject.Update();
57+
SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
58+
if (scriptProperty == null)
59+
return;
60+
61+
MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
62+
Init(targetScript);
63+
}
5064
Type type = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
5165
Type genericType = type.GetGenericArguments()[0];
5266

@@ -74,37 +88,30 @@ void RenderNetworkedVarValueType<T>(int index) where T : struct
7488
{
7589
NetworkedVar<T> var = (NetworkedVar<T>)networkedVarFields[networkedVarNames[index]].GetValue(target);
7690
Type type = typeof(T);
77-
ValueType val = var.Value;
91+
object val = var.Value;
7892
string name = networkedVarNames[index];
7993
if (type == typeof(int))
80-
val = EditorGUILayout.IntField(name, Convert.ToInt32(val));
94+
val = EditorGUILayout.IntField(name, (int)val);
8195
else if (type == typeof(uint))
82-
val = (uint)EditorGUILayout.IntField(name, Convert.ToInt32(val));
96+
val = EditorGUILayout.LongField(name, (long)val);
8397
else if (type == typeof(short))
84-
val = (short)EditorGUILayout.IntField(name, Convert.ToInt32(val));
98+
val = EditorGUILayout.IntField(name, (int)val);
8599
else if (type == typeof(ushort))
86-
val = (ushort)EditorGUILayout.IntField(name, Convert.ToInt32(val));
100+
val = EditorGUILayout.IntField(name, (int)val);
87101
else if (type == typeof(sbyte))
88-
val = (sbyte)EditorGUILayout.IntField(name, Convert.ToInt32(val));
102+
val = EditorGUILayout.IntField(name, (int)val);
89103
else if (type == typeof(byte))
90-
val = (byte)EditorGUILayout.IntField(name, Convert.ToInt32(val));
104+
val = EditorGUILayout.IntField(name, (int)val);
91105
else if (type == typeof(long))
92-
val = EditorGUILayout.LongField(name, Convert.ToInt64(val));
106+
val = EditorGUILayout.LongField(name, (long)val);
93107
else if (type == typeof(ulong))
94-
val = (ulong)EditorGUILayout.LongField(name, Convert.ToInt64(val));
108+
val = EditorGUILayout.LongField(name, (long)val);
95109
else if (type == typeof(bool))
96-
val = EditorGUILayout.Toggle(name, Convert.ToBoolean(val));
97-
else if (type == typeof(char))
98-
{
99-
char[] chars = EditorGUILayout.TextField(name, Convert.ToString(val)).ToCharArray();
100-
if (chars.Length > 0)
101-
val = chars[0];
102-
}
103-
// TODO - more value types here
110+
val = EditorGUILayout.Toggle(name, (bool)val);
111+
else if (type == typeof(string))
112+
val = EditorGUILayout.TextField(name, (string)val);
104113
else
105-
{
106114
EditorGUILayout.LabelField("Type not renderable");
107-
}
108115

109116
var.Value = (T)val;
110117
}
@@ -162,4 +169,4 @@ public override void OnInspectorGUI()
162169
EditorGUI.EndChangeCheck();
163170
}
164171
}
165-
}
172+
}

0 commit comments

Comments
 (0)