Skip to content

Commit c9cc920

Browse files
authored
Merge pull request #96 from Mrb83/feature/Mrb83/NetworkedVar
NetworkedVar
2 parents d3cd108 + 48272f2 commit c9cc920

File tree

14 files changed

+1474
-12
lines changed

14 files changed

+1474
-12
lines changed

MLAPI-Editor/NetworkedBehaviourEditor.cs

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MLAPI.Attributes;
2+
using MLAPI.Data;
23
using MLAPI.MonoBehaviours.Core;
34
using System;
45
using System.Collections.Generic;
@@ -9,26 +10,110 @@ namespace UnityEditor
910
{
1011
[CustomEditor(typeof(NetworkedBehaviour), true)]
1112
[CanEditMultipleObjects]
12-
public class NetworkedBehaviourInspector : Editor
13+
public class NetworkedBehaviourEditor : Editor
1314
{
1415
private bool initialized;
15-
protected List<string> syncedVarNames = new List<string>();
16+
private HashSet<string> syncedVarNames = new HashSet<string>();
17+
private List<string> networkedVarNames = new List<string>();
18+
private Dictionary<string, FieldInfo> networkedVarFields = new Dictionary<string, FieldInfo>();
19+
private Dictionary<string, object> networkedVarObjects = new Dictionary<string, object>();
1620

1721
private GUIContent syncedVarLabelGuiContent;
22+
private GUIContent networkedVarLabelGuiContent;
1823

1924
private void Init(MonoScript script)
2025
{
2126
initialized = true;
2227

28+
syncedVarNames.Clear();
29+
networkedVarNames.Clear();
30+
networkedVarFields.Clear();
31+
networkedVarObjects.Clear();
32+
2333
syncedVarLabelGuiContent = new GUIContent("SyncedVar", "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.");
2435

2536
FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
2637
for (int i = 0; i < fields.Length; i++)
2738
{
2839
Attribute[] attributes = (Attribute[])fields[i].GetCustomAttributes(typeof(SyncedVar), true);
2940
if (attributes.Length > 0)
3041
syncedVarNames.Add(fields[i].Name);
42+
43+
Type ft = fields[i].FieldType;
44+
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkedVar<>))
45+
{
46+
networkedVarNames.Add(fields[i].Name);
47+
networkedVarFields.Add(fields[i].Name, fields[i]);
48+
}
49+
}
50+
}
51+
52+
void RenderNetworkedVar(int index)
53+
{
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+
}
64+
Type type = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
65+
Type genericType = type.GetGenericArguments()[0];
66+
67+
EditorGUILayout.BeginHorizontal();
68+
if (genericType == typeof(string))
69+
{
70+
NetworkedVar<string> var = (NetworkedVar<string>)networkedVarFields[networkedVarNames[index]].GetValue(target);
71+
var.Value = EditorGUILayout.TextField(networkedVarNames[index], var.Value);
72+
}
73+
else if (genericType.IsValueType)
74+
{
75+
MethodInfo method = typeof(NetworkedBehaviourEditor).GetMethod("RenderNetworkedVarValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
76+
MethodInfo genericMethod = method.MakeGenericMethod(genericType);
77+
genericMethod.Invoke(this, new object[] { (object)index });
3178
}
79+
else
80+
{
81+
EditorGUILayout.LabelField("Type not renderable");
82+
}
83+
GUILayout.Label(networkedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(networkedVarLabelGuiContent).x));
84+
EditorGUILayout.EndHorizontal();
85+
}
86+
87+
void RenderNetworkedVarValueType<T>(int index) where T : struct
88+
{
89+
NetworkedVar<T> var = (NetworkedVar<T>)networkedVarFields[networkedVarNames[index]].GetValue(target);
90+
Type type = typeof(T);
91+
object val = var.Value;
92+
string name = networkedVarNames[index];
93+
if (type == typeof(int))
94+
val = EditorGUILayout.IntField(name, (int)val);
95+
else if (type == typeof(uint))
96+
val = EditorGUILayout.LongField(name, (long)val);
97+
else if (type == typeof(short))
98+
val = EditorGUILayout.IntField(name, (int)val);
99+
else if (type == typeof(ushort))
100+
val = EditorGUILayout.IntField(name, (int)val);
101+
else if (type == typeof(sbyte))
102+
val = EditorGUILayout.IntField(name, (int)val);
103+
else if (type == typeof(byte))
104+
val = EditorGUILayout.IntField(name, (int)val);
105+
else if (type == typeof(long))
106+
val = EditorGUILayout.LongField(name, (long)val);
107+
else if (type == typeof(ulong))
108+
val = EditorGUILayout.LongField(name, (long)val);
109+
else if (type == typeof(bool))
110+
val = EditorGUILayout.Toggle(name, (bool)val);
111+
else if (type == typeof(string))
112+
val = EditorGUILayout.TextField(name, (string)val);
113+
else
114+
EditorGUILayout.LabelField("Type not renderable");
115+
116+
var.Value = (T)val;
32117
}
33118

34119
public override void OnInspectorGUI()
@@ -47,6 +132,9 @@ public override void OnInspectorGUI()
47132
EditorGUI.BeginChangeCheck();
48133
serializedObject.Update();
49134

135+
for (int i = 0; i < networkedVarNames.Count; i++)
136+
RenderNetworkedVar(i);
137+
50138
SerializedProperty property = serializedObject.GetIterator();
51139
bool expanded = true;
52140
while (property.NextVisible(expanded))
@@ -78,7 +166,7 @@ public override void OnInspectorGUI()
78166
expanded = false;
79167
}
80168
serializedObject.ApplyModifiedProperties();
81-
EditorGUI.EndChangeCheck();
169+
EditorGUI.EndChangeCheck();
82170
}
83171
}
84172
}

0 commit comments

Comments
 (0)