NetworkBehaviourInspector.cs
6.83 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
176
177
#if ENABLE_UNET
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Networking;
namespace UnityEditor
{
[CustomEditor(typeof(NetworkBehaviour), true)]
[CanEditMultipleObjects]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkBehaviourInspector : Editor
{
bool m_Initialized;
protected List<string> m_SyncVarNames = new List<string>();
Type m_ScriptClass;
bool m_HasOnSerialize;
bool[] m_ShowSyncLists;
GUIContent m_SyncVarIndicatorContent;
protected GUIContent m_NetworkChannelLabel;
protected GUIContent m_NetworkSendIntervalLabel;
internal virtual bool hideScriptField
{
get { return false; }
}
void Init(MonoScript script)
{
m_Initialized = true;
m_ScriptClass = script.GetClass();
m_SyncVarIndicatorContent = TextUtility.TextContent("SyncVar", "This variable has been marked with the [SyncVar] attribute.");
m_NetworkChannelLabel = TextUtility.TextContent("Network Channel", "QoS channel used for updates. Use the [NetworkSettings] class attribute to change this.");
m_NetworkSendIntervalLabel = TextUtility.TextContent("Network Send Interval", "Maximum update rate in seconds. Use the [NetworkSettings] class attribute to change this, or implement GetNetworkSendInterval");
foreach (var field in m_ScriptClass.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
Attribute[] fieldMarkers = (Attribute[])field.GetCustomAttributes(typeof(SyncVarAttribute), true);
if (fieldMarkers.Length > 0)
{
m_SyncVarNames.Add(field.Name);
}
}
var meth = script.GetClass().GetMethod("OnSerialize");
if (meth != null)
{
if (meth.DeclaringType != typeof(NetworkBehaviour))
{
m_HasOnSerialize = true;
}
}
int numSyncLists = 0;
foreach (var f in serializedObject.targetObject.GetType().GetFields())
{
if (f.FieldType.BaseType != null && f.FieldType.BaseType.Name.Contains("SyncList"))
{
numSyncLists += 1;
}
}
if (numSyncLists > 0)
{
m_ShowSyncLists = new bool[numSyncLists];
}
}
public override void OnInspectorGUI()
{
if (!m_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();
// Loop through properties and create one field (including children) for each top level property.
SerializedProperty property = serializedObject.GetIterator();
bool expanded = true;
while (property.NextVisible(expanded))
{
bool isSyncVar = m_SyncVarNames.Contains(property.name);
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
if (property.name == "m_Script")
{
if (hideScriptField)
{
continue;
}
EditorGUI.BeginDisabledGroup(true);
}
EditorGUILayout.PropertyField(property, true);
if (isSyncVar)
{
GUILayout.Label(m_SyncVarIndicatorContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(m_SyncVarIndicatorContent).x));
}
if (property.name == "m_Script")
{
EditorGUI.EndDisabledGroup();
}
}
else
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(property, true);
if (isSyncVar)
{
GUILayout.Label(m_SyncVarIndicatorContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(m_SyncVarIndicatorContent).x));
}
EditorGUILayout.EndHorizontal();
}
expanded = false;
}
serializedObject.ApplyModifiedProperties();
EditorGUI.EndChangeCheck();
// find SyncLists.. they are not properties.
int syncListIndex = 0;
foreach (var field in serializedObject.targetObject.GetType().GetFields())
{
if (field.FieldType.BaseType != null && field.FieldType.BaseType.Name.Contains("SyncList"))
{
m_ShowSyncLists[syncListIndex] = EditorGUILayout.Foldout(m_ShowSyncLists[syncListIndex], "SyncList " + field.Name + " [" + field.FieldType.Name + "]");
if (m_ShowSyncLists[syncListIndex])
{
EditorGUI.indentLevel += 1;
var synclist = field.GetValue(serializedObject.targetObject) as IEnumerable;
if (synclist != null)
{
int index = 0;
var enu = synclist.GetEnumerator();
while (enu.MoveNext())
{
if (enu.Current != null)
{
EditorGUILayout.LabelField("Item:" + index, enu.Current.ToString());
}
index += 1;
}
}
EditorGUI.indentLevel -= 1;
}
syncListIndex += 1;
}
}
if (m_HasOnSerialize)
{
var beh = target as NetworkBehaviour;
if (beh != null)
{
EditorGUILayout.LabelField(m_NetworkChannelLabel, new GUIContent(beh.GetNetworkChannel().ToString()));
EditorGUILayout.LabelField(m_NetworkSendIntervalLabel, new GUIContent(beh.GetNetworkSendInterval().ToString()));
}
}
}
}
} //namespace
#endif //ENABLE_UNET