forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkingManagerEditor.cs
More file actions
167 lines (140 loc) · 7.45 KB
/
Copy pathNetworkingManagerEditor.cs
File metadata and controls
167 lines (140 loc) · 7.45 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
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
using MLAPI;
[CustomEditor(typeof(NetworkingManager), true)]
[CanEditMultipleObjects]
public class NetworkingManagerEditor : Editor
{
private SerializedProperty DontDestroyOnLoadProperty;
private SerializedProperty RunInBackgroundProperty;
private SerializedProperty LogLevelProperty;
private SerializedProperty NetworkConfigProperty;
private ReorderableList networkPrefabsList;
private ReorderableList channelsList;
private ReorderableList registeredScenesList;
private NetworkingManager networkingManager;
private bool initialized;
private void Init()
{
if (initialized)
return;
initialized = true;
networkingManager = (NetworkingManager)target;
DontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy");
RunInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
LogLevelProperty = serializedObject.FindProperty("LogLevel");
NetworkConfigProperty = serializedObject.FindProperty("NetworkConfig");
}
private void CheckNullProperties()
{
if (DontDestroyOnLoadProperty == null)
DontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy");
if (RunInBackgroundProperty == null)
RunInBackgroundProperty = serializedObject.FindProperty("RunInBackground");
if (LogLevelProperty == null)
LogLevelProperty = serializedObject.FindProperty("LogLevel");
if (NetworkConfigProperty == null)
NetworkConfigProperty = serializedObject.FindProperty("NetworkConfig");
}
private void OnEnable()
{
networkPrefabsList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("NetworkedPrefabs"), true, true, true, true);
networkPrefabsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
SerializedProperty element = networkPrefabsList.serializedProperty.GetArrayElementAtIndex(index);
int firstLabelWidth = 50;
int secondLabelWidth = 140;
float secondFieldWidth = 10;
int reduceFirstWidth = 45;
EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Prefab");
EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - secondLabelWidth - secondFieldWidth - reduceFirstWidth,
EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("prefab"), GUIContent.none);
EditorGUI.LabelField(new Rect(rect.width - secondLabelWidth - secondFieldWidth, rect.y, secondLabelWidth, EditorGUIUtility.singleLineHeight), "Default Player Prefab");
EditorGUI.PropertyField(new Rect(rect.width - secondFieldWidth, rect.y, secondFieldWidth,
EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("playerPrefab"), GUIContent.none);
};
networkPrefabsList.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "NetworkedPrefabs (Auto Sorted)");
};
channelsList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("Channels"), true, true, true, true);
channelsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
SerializedProperty element = channelsList.serializedProperty.GetArrayElementAtIndex(index);
int firstLabelWidth = 50;
int secondLabelWidth = 40;
int secondFieldWidth = 150;
int reduceFirstWidth = 45;
EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Name");
EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - secondLabelWidth - secondFieldWidth - reduceFirstWidth,
EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Name"), GUIContent.none);
EditorGUI.LabelField(new Rect(rect.width - secondLabelWidth - secondFieldWidth, rect.y, secondLabelWidth, EditorGUIUtility.singleLineHeight), "Type");
EditorGUI.PropertyField(new Rect(rect.width - secondFieldWidth, rect.y, secondFieldWidth,
EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Type"), GUIContent.none);
};
channelsList.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "Channels (Auto Sorted)");
};
registeredScenesList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("RegisteredScenes"), true, true, true, true);
registeredScenesList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
SerializedProperty element = registeredScenesList.serializedProperty.GetArrayElementAtIndex(index);
int firstLabelWidth = 50;
int padding = 20;
EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Name");
EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - padding,
EditorGUIUtility.singleLineHeight), element, GUIContent.none);
};
registeredScenesList.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "Registered Scene Names (Auto Sorted)");
};
}
public override void OnInspectorGUI()
{
Init();
CheckNullProperties();
if (!networkingManager.isServer && !networkingManager.isClient)
{
serializedObject.Update();
EditorGUILayout.PropertyField(DontDestroyOnLoadProperty);
EditorGUILayout.PropertyField(RunInBackgroundProperty);
EditorGUILayout.PropertyField(LogLevelProperty);
if (networkingManager.NetworkConfig.HandleObjectSpawning)
{
EditorGUILayout.Space();
networkPrefabsList.DoLayoutList();
}
EditorGUILayout.Space();
channelsList.DoLayoutList();
EditorGUILayout.Space();
if (networkingManager.NetworkConfig.EnableSceneSwitching)
{
registeredScenesList.DoLayoutList();
EditorGUILayout.Space();
}
serializedObject.ApplyModifiedProperties();
base.OnInspectorGUI();
}
else
{
string instanceType = "";
if (networkingManager.isHost)
instanceType = "Host";
else if (networkingManager.isServer)
instanceType = "Server";
else if (networkingManager.isClient)
instanceType = "Client";
EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running", UnityEditor.MessageType.Info);
if (GUILayout.Toggle(false, "Stop " + instanceType, EditorStyles.miniButtonMid))
{
if (networkingManager.isHost)
networkingManager.StopHost();
else if (networkingManager.isServer)
networkingManager.StopServer();
else if (networkingManager.isClient)
networkingManager.StopClient();
}
}
//Repaint();
}
}