forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkedBehaviourEditor.cs
More file actions
175 lines (154 loc) · 7.48 KB
/
Copy pathNetworkedBehaviourEditor.cs
File metadata and controls
175 lines (154 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Reflection;
using MLAPI;
using MLAPI.NetworkedVar;
using UnityEngine;
namespace UnityEditor
{
[CustomEditor(typeof(NetworkedBehaviour), true)]
[CanEditMultipleObjects]
public class NetworkedBehaviourEditor : Editor
{
private bool initialized;
private List<string> networkedVarNames = new List<string>();
private Dictionary<string, FieldInfo> networkedVarFields = new Dictionary<string, FieldInfo>();
private Dictionary<string, object> networkedVarObjects = new Dictionary<string, object>();
private GUIContent networkedVarLabelGuiContent;
private void Init(MonoScript script)
{
initialized = true;
networkedVarNames.Clear();
networkedVarFields.Clear();
networkedVarObjects.Clear();
networkedVarLabelGuiContent = new GUIContent("NetworkedVar", "This variable is a NetworkedVar. It can not be serialized and can only be changed during runtime.");
FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
for (int i = 0; i < fields.Length; i++)
{
Type ft = fields[i].FieldType;
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkedVar<>))
{
networkedVarNames.Add(fields[i].Name);
networkedVarFields.Add(fields[i].Name, fields[i]);
}
}
}
void RenderNetworkedVar(int index)
{
if (!networkedVarFields.ContainsKey(networkedVarNames[index]))
{
serializedObject.Update();
SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
if (scriptProperty == null)
return;
MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
Init(targetScript);
}
object value = networkedVarFields[networkedVarNames[index]].GetValue(target);
if (value == null)
{
Type fieldType = networkedVarFields[networkedVarNames[index]].FieldType;
INetworkedVar var = (INetworkedVar) Activator.CreateInstance(fieldType, true);
networkedVarFields[networkedVarNames[index]].SetValue(target, var);
}
Type type = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
Type genericType = type.GetGenericArguments()[0];
EditorGUILayout.BeginHorizontal();
if (genericType == typeof(string))
{
NetworkedVar<string> var = (NetworkedVar<string>)networkedVarFields[networkedVarNames[index]].GetValue(target);
var.Value = EditorGUILayout.TextField(networkedVarNames[index], var.Value);
}
else if (genericType.IsValueType)
{
MethodInfo method = typeof(NetworkedBehaviourEditor).GetMethod("RenderNetworkedVarValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
MethodInfo genericMethod = method.MakeGenericMethod(genericType);
genericMethod.Invoke(this, new object[] { (object)index });
}
else
{
EditorGUILayout.LabelField("Type not renderable");
}
GUILayout.Label(networkedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(networkedVarLabelGuiContent).x));
EditorGUILayout.EndHorizontal();
}
void RenderNetworkedVarValueType<T>(int index) where T : struct
{
NetworkedVar<T> var = (NetworkedVar<T>)networkedVarFields[networkedVarNames[index]].GetValue(target);
Type type = typeof(T);
object val = var.Value;
string name = networkedVarNames[index];
if (NetworkingManager.Singleton != null && NetworkingManager.Singleton.IsListening)
{
if (type == typeof(int))
val = EditorGUILayout.IntField(name, (int)val);
else if (type == typeof(uint))
val = (uint)EditorGUILayout.LongField(name, (long)((uint)val));
else if (type == typeof(short))
val = (short)EditorGUILayout.IntField(name, (int)((short)val));
else if (type == typeof(ushort))
val = (ushort)EditorGUILayout.IntField(name, (int)((ushort)val));
else if (type == typeof(sbyte))
val = (sbyte)EditorGUILayout.IntField(name, (int)((sbyte)val));
else if (type == typeof(byte))
val = (byte)EditorGUILayout.IntField(name, (int)((byte)val));
else if (type == typeof(long))
val = EditorGUILayout.LongField(name, (long)val);
else if (type == typeof(ulong))
val = (ulong)EditorGUILayout.LongField(name, (long)((ulong)val));
else if (type == typeof(bool))
val = EditorGUILayout.Toggle(name, (bool)val);
else if (type == typeof(string))
val = EditorGUILayout.TextField(name, (string)val);
else if (type.IsEnum)
val = EditorGUILayout.EnumPopup(name, (Enum) val);
else
EditorGUILayout.LabelField("Type not renderable");
var.Value = (T)val;
}
else
{
EditorGUILayout.LabelField(name, EditorStyles.wordWrappedLabel);
EditorGUILayout.SelectableLabel(val.ToString(), EditorStyles.wordWrappedLabel);
}
}
public override void OnInspectorGUI()
{
if (!initialized)
{
serializedObject.Update();
SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
if (scriptProperty == null)
return;
MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
Init(targetScript);
}
EditorGUI.BeginChangeCheck();
serializedObject.Update();
for (int i = 0; i < networkedVarNames.Count; i++)
RenderNetworkedVar(i);
SerializedProperty property = serializedObject.GetIterator();
bool expanded = true;
while (property.NextVisible(expanded))
{
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
if (property.name == "m_Script")
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(property, true);
if (property.name == "m_Script")
EditorGUI.EndDisabledGroup();
}
else
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(property, true);
EditorGUILayout.EndHorizontal();
}
expanded = false;
}
serializedObject.ApplyModifiedProperties();
EditorGUI.EndChangeCheck();
}
}
}