-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathNetworkBehaviourEditor.cs
More file actions
175 lines (154 loc) · 7.63 KB
/
NetworkBehaviourEditor.cs
File metadata and controls
175 lines (154 loc) · 7.63 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.NetworkVariable;
using UnityEngine;
namespace UnityEditor
{
[CustomEditor(typeof(NetworkBehaviour), true)]
[CanEditMultipleObjects]
public class NetworkBehaviourEditor : Editor
{
private bool initialized;
private readonly List<string> networkVariableNames = new List<string>();
private readonly Dictionary<string, FieldInfo> networkVariableFields = new Dictionary<string, FieldInfo>();
private readonly Dictionary<string, object> networkVariableObjects = new Dictionary<string, object>();
private GUIContent networkVariableLabelGuiContent;
private void Init(MonoScript script)
{
initialized = true;
networkVariableNames.Clear();
networkVariableFields.Clear();
networkVariableObjects.Clear();
networkVariableLabelGuiContent = new GUIContent("NetworkVariable", "This variable is a NetworkVariable. It can not be serialized and can only be changed during runtime.");
FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
for (int i = 0; i < fields.Length; i++)
{
Type ft = fields[i].FieldType;
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true))
{
networkVariableNames.Add(fields[i].Name);
networkVariableFields.Add(fields[i].Name, fields[i]);
}
}
}
void RenderNetworkVariable(int index)
{
if (!networkVariableFields.ContainsKey(networkVariableNames[index]))
{
serializedObject.Update();
SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
if (scriptProperty == null)
return;
MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
Init(targetScript);
}
object value = networkVariableFields[networkVariableNames[index]].GetValue(target);
if (value == null)
{
Type fieldType = networkVariableFields[networkVariableNames[index]].FieldType;
INetworkVariable var = (INetworkVariable) Activator.CreateInstance(fieldType, true);
networkVariableFields[networkVariableNames[index]].SetValue(target, var);
}
Type type = networkVariableFields[networkVariableNames[index]].GetValue(target).GetType();
Type genericType = type.GetGenericArguments()[0];
EditorGUILayout.BeginHorizontal();
if (genericType == typeof(string))
{
NetworkVariable<string> var = (NetworkVariable<string>)networkVariableFields[networkVariableNames[index]].GetValue(target);
var.Value = EditorGUILayout.TextField(networkVariableNames[index], var.Value);
}
else if (genericType.IsValueType)
{
MethodInfo method = typeof(NetworkBehaviourEditor).GetMethod("RenderNetworkVariableValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
MethodInfo genericMethod = method.MakeGenericMethod(genericType);
genericMethod.Invoke(this, new[] { (object)index });
}
else
{
EditorGUILayout.LabelField("Type not renderable");
}
GUILayout.Label(networkVariableLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(networkVariableLabelGuiContent).x));
EditorGUILayout.EndHorizontal();
}
void RenderNetworkVariableValueType<T>(int index) where T : struct
{
NetworkVariable<T> var = (NetworkVariable<T>)networkVariableFields[networkVariableNames[index]].GetValue(target);
Type type = typeof(T);
object val = var.Value;
string name = networkVariableNames[index];
if (NetworkManager.Singleton != null && NetworkManager.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 < networkVariableNames.Count; i++)
RenderNetworkVariable(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();
}
}
}