|
1 |
| -using MLAPI.MonoBehaviours.Core; |
2 |
| -using UnityEditor; |
| 1 | +using UnityEditor; |
3 | 2 | using UnityEngine;
|
4 | 3 | using UnityEditorInternal;
|
| 4 | +using MLAPI; |
| 5 | +using MLAPI.MonoBehaviours.Core; |
5 | 6 |
|
6 |
| -namespace UnityEditor |
| 7 | +[CustomEditor(typeof(NetworkingManager), true)] |
| 8 | +[CanEditMultipleObjects] |
| 9 | +public class NetworkingManagerEditor : Editor |
7 | 10 | {
|
8 |
| - [CustomEditor(typeof(NetworkingManager), true)] |
9 |
| - [CanEditMultipleObjects] |
10 |
| - public class NetworkingManagerEditor : Editor |
| 11 | + private SerializedProperty DontDestroyOnLoadProperty; |
| 12 | + private SerializedProperty RunInBackgroundProperty; |
| 13 | + private SerializedProperty LogLevelProperty; |
| 14 | + private SerializedProperty NetworkConfigProperty; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + private ReorderableList networkPrefabsList; |
| 20 | + private ReorderableList channelsList; |
| 21 | + private ReorderableList messageTypesList; |
| 22 | + private ReorderableList registeredScenesList; |
| 23 | + |
| 24 | + private NetworkingManager networkingManager; |
| 25 | + private bool initialized; |
| 26 | + |
| 27 | + |
| 28 | + private void Init() |
11 | 29 | {
|
12 |
| - private ReorderableList networkedObjectList; |
| 30 | + if (initialized) |
| 31 | + return; |
| 32 | + |
| 33 | + initialized = true; |
| 34 | + networkingManager = (NetworkingManager)target; |
| 35 | + DontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy"); |
| 36 | + RunInBackgroundProperty = serializedObject.FindProperty("RunInBackground"); |
| 37 | + LogLevelProperty = serializedObject.FindProperty("LogLevel"); |
| 38 | + NetworkConfigProperty = serializedObject.FindProperty("NetworkConfig"); |
| 39 | + } |
13 | 40 |
|
14 |
| - private NetworkingManager networkingManager; |
15 |
| - private bool initialized; |
| 41 | + private void CheckNullProperties() |
| 42 | + { |
| 43 | + if (DontDestroyOnLoadProperty == null) |
| 44 | + DontDestroyOnLoadProperty = serializedObject.FindProperty("DontDestroy"); |
| 45 | + if (RunInBackgroundProperty == null) |
| 46 | + RunInBackgroundProperty = serializedObject.FindProperty("RunInBackground"); |
| 47 | + if (LogLevelProperty == null) |
| 48 | + LogLevelProperty = serializedObject.FindProperty("LogLevel"); |
| 49 | + if (NetworkConfigProperty == null) |
| 50 | + NetworkConfigProperty = serializedObject.FindProperty("NetworkConfig"); |
| 51 | + } |
16 | 52 |
|
17 |
| - private void Init() |
| 53 | + private void OnEnable() |
| 54 | + { |
| 55 | + networkPrefabsList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("NetworkedPrefabs"), true, true, true, true); |
| 56 | + networkPrefabsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => |
18 | 57 | {
|
19 |
| - if (initialized) |
20 |
| - return; |
21 |
| - initialized = true; |
22 |
| - networkingManager = (NetworkingManager)target; |
23 |
| - } |
| 58 | + /* |
| 59 | + SerializedProperty element = networkPrefabsList.serializedProperty.GetArrayElementAtIndex(index); |
| 60 | + rect.y += 2; |
| 61 | + EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width - 30, EditorGUIUtility.singleLineHeight), |
| 62 | + element.FindPropertyRelative("prefab"), GUIContent.none); |
| 63 | + EditorGUI.PropertyField(new Rect(rect.x + rect.width - 30, rect.y, 30, EditorGUIUtility.singleLineHeight), |
| 64 | + element.FindPropertyRelative("playerPrefab"), GUIContent.none); |
| 65 | +
|
| 66 | + */ |
| 67 | + |
| 68 | + SerializedProperty element = networkPrefabsList.serializedProperty.GetArrayElementAtIndex(index); |
| 69 | + int firstLabelWidth = 50; |
| 70 | + int secondLabelWidth = 140; |
| 71 | + float secondFieldWidth = 10; |
| 72 | + int reduceFirstWidth = 45; |
| 73 | + |
| 74 | + EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Prefab"); |
| 75 | + EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - secondLabelWidth - secondFieldWidth - reduceFirstWidth, |
| 76 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("prefab"), GUIContent.none); |
| 77 | + |
| 78 | + EditorGUI.LabelField(new Rect(rect.width - secondLabelWidth - secondFieldWidth, rect.y, secondLabelWidth, EditorGUIUtility.singleLineHeight), "Default Player Prefab"); |
| 79 | + EditorGUI.PropertyField(new Rect(rect.width - secondFieldWidth, rect.y, secondFieldWidth, |
| 80 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("playerPrefab"), GUIContent.none); |
| 81 | + }; |
| 82 | + |
| 83 | + networkPrefabsList.drawHeaderCallback = (Rect rect) => { |
| 84 | + EditorGUI.LabelField(rect, "NetworkedPrefabs (Auto Sorted)"); |
| 85 | + }; |
24 | 86 |
|
25 |
| - private void OnEnable() |
| 87 | + messageTypesList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("MessageTypes"), true, true, true, true); |
| 88 | + messageTypesList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => |
26 | 89 | {
|
27 |
| - networkedObjectList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("NetworkedPrefabs"), true, true, true, true); |
28 |
| - networkedObjectList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => |
29 |
| - { |
30 |
| - var element = networkedObjectList.serializedProperty.GetArrayElementAtIndex(index); |
31 |
| - rect.y += 2; |
32 |
| - EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width - 30, EditorGUIUtility.singleLineHeight), |
33 |
| - element.FindPropertyRelative("prefab"), GUIContent.none); |
34 |
| - EditorGUI.PropertyField(new Rect(rect.x + rect.width - 30, rect.y, 30, EditorGUIUtility.singleLineHeight), |
35 |
| - element.FindPropertyRelative("playerPrefab"), GUIContent.none); |
36 |
| - }; |
37 |
| - |
38 |
| - networkedObjectList.drawHeaderCallback = (Rect rect) => |
39 |
| - { |
40 |
| - EditorGUI.LabelField(rect, "Networked Prefabs"); |
41 |
| - }; |
42 |
| - } |
| 90 | + SerializedProperty element = messageTypesList.serializedProperty.GetArrayElementAtIndex(index); |
| 91 | + |
| 92 | + |
| 93 | + int firstLabelWidth = 50; |
| 94 | + int secondLabelWidth = 90; |
| 95 | + float secondFieldWidth = 10; |
| 96 | + int reduceFirstWidth = 45; |
43 | 97 |
|
44 |
| - public override void OnInspectorGUI() |
| 98 | + EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Name"); |
| 99 | + EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - secondLabelWidth - secondFieldWidth - reduceFirstWidth, |
| 100 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Name"), GUIContent.none); |
| 101 | + |
| 102 | + EditorGUI.LabelField(new Rect(rect.width - secondLabelWidth - secondFieldWidth, rect.y, secondLabelWidth, EditorGUIUtility.singleLineHeight), "Passthrough"); |
| 103 | + EditorGUI.PropertyField(new Rect(rect.width - secondFieldWidth, rect.y, secondFieldWidth, |
| 104 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Passthrough"), GUIContent.none); |
| 105 | + }; |
| 106 | + |
| 107 | + messageTypesList.drawHeaderCallback = (Rect rect) => { |
| 108 | + EditorGUI.LabelField(rect, "MessageTypes (Auto Sorted)"); |
| 109 | + }; |
| 110 | + |
| 111 | + |
| 112 | + channelsList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("Channels"), true, true, true, true); |
| 113 | + channelsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => |
45 | 114 | {
|
46 |
| - Init(); |
47 |
| - if (!networkingManager.isServer && !networkingManager.isClient) |
48 |
| - { |
49 |
| - EditorGUILayout.Space(); |
50 |
| - serializedObject.Update(); |
51 |
| - networkedObjectList.DoLayoutList(); |
52 |
| - serializedObject.ApplyModifiedProperties(); |
53 |
| - base.OnInspectorGUI(); //Only draw if we don't have a running client or server |
54 |
| - } |
55 |
| - else |
| 115 | + SerializedProperty element = channelsList.serializedProperty.GetArrayElementAtIndex(index); |
| 116 | + |
| 117 | + |
| 118 | + int firstLabelWidth = 50; |
| 119 | + int secondLabelWidth = 40; |
| 120 | + int secondFieldWidth = 150; |
| 121 | + int thirdLabelWidth = 70; |
| 122 | + int thirdFieldWidth = 10; |
| 123 | + int reduceFirstWidth = 45; |
| 124 | + int reduceSecondWidth = 10; |
| 125 | + |
| 126 | + EditorGUI.LabelField(new Rect(rect.x, rect.y, firstLabelWidth, EditorGUIUtility.singleLineHeight), "Name"); |
| 127 | + EditorGUI.PropertyField(new Rect(rect.x + firstLabelWidth, rect.y, rect.width - firstLabelWidth - secondLabelWidth - secondFieldWidth - thirdFieldWidth - thirdLabelWidth - reduceFirstWidth, |
| 128 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Name"), GUIContent.none); |
| 129 | + |
| 130 | + |
| 131 | + EditorGUI.LabelField(new Rect(rect.width - secondLabelWidth - secondFieldWidth - thirdFieldWidth - thirdLabelWidth, rect.y, secondLabelWidth, EditorGUIUtility.singleLineHeight), "Type"); |
| 132 | + EditorGUI.PropertyField(new Rect(rect.width - secondFieldWidth - thirdLabelWidth - thirdFieldWidth, rect.y, secondFieldWidth - reduceSecondWidth, |
| 133 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Type"), GUIContent.none); |
| 134 | + |
| 135 | + EditorGUI.LabelField(new Rect(rect.width - thirdFieldWidth - thirdLabelWidth, rect.y, thirdLabelWidth, EditorGUIUtility.singleLineHeight), "Encrypted"); |
| 136 | + EditorGUI.PropertyField(new Rect(rect.width - thirdFieldWidth, rect.y, secondFieldWidth, |
| 137 | + EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Encrypted"), GUIContent.none); |
| 138 | + }; |
| 139 | + |
| 140 | + channelsList.drawHeaderCallback = (Rect rect) => { |
| 141 | + EditorGUI.LabelField(rect, "Channels (Auto Sorted)"); |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + public override void OnInspectorGUI() |
| 146 | + { |
| 147 | + Init(); |
| 148 | + CheckNullProperties(); |
| 149 | + if (!networkingManager.isServer && !networkingManager.isClient) |
| 150 | + { |
| 151 | + serializedObject.Update(); |
| 152 | + EditorGUILayout.PropertyField(DontDestroyOnLoadProperty); |
| 153 | + EditorGUILayout.PropertyField(RunInBackgroundProperty); |
| 154 | + EditorGUILayout.PropertyField(LogLevelProperty); |
| 155 | + |
| 156 | + EditorGUILayout.Space(); |
| 157 | + networkPrefabsList.DoLayoutList(); |
| 158 | + EditorGUILayout.Space(); |
| 159 | + messageTypesList.DoLayoutList(); |
| 160 | + EditorGUILayout.Space(); |
| 161 | + channelsList.DoLayoutList(); |
| 162 | + EditorGUILayout.Space(); |
| 163 | + |
| 164 | + serializedObject.ApplyModifiedProperties(); |
| 165 | + base.OnInspectorGUI(); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + string instanceType = ""; |
| 170 | + if (networkingManager.isHost) |
| 171 | + instanceType = "Host"; |
| 172 | + else if (networkingManager.isServer) |
| 173 | + instanceType = "Server"; |
| 174 | + else if (networkingManager.isClient) |
| 175 | + instanceType = "Client"; |
| 176 | + |
| 177 | + EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running", UnityEditor.MessageType.Info); |
| 178 | + if (GUILayout.Toggle(false, "Stop " + instanceType, EditorStyles.miniButtonMid)) |
56 | 179 | {
|
57 |
| - string instanceType = ""; |
58 | 180 | if (networkingManager.isHost)
|
59 |
| - instanceType = "Host"; |
| 181 | + networkingManager.StopHost(); |
60 | 182 | else if (networkingManager.isServer)
|
61 |
| - instanceType = "Server"; |
| 183 | + networkingManager.StopServer(); |
62 | 184 | else if (networkingManager.isClient)
|
63 |
| - instanceType = "Client"; |
64 |
| - |
65 |
| - EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running", MessageType.Info); |
66 |
| - if (GUILayout.Toggle(false, "Stop " + instanceType, EditorStyles.miniButtonMid)) |
67 |
| - { |
68 |
| - if (networkingManager.isHost) |
69 |
| - networkingManager.StopHost(); |
70 |
| - else if (networkingManager.isServer) |
71 |
| - networkingManager.StopServer(); |
72 |
| - else if (networkingManager.isClient) |
73 |
| - networkingManager.StopClient(); |
74 |
| - } |
| 185 | + networkingManager.StopClient(); |
75 | 186 | }
|
76 |
| - Repaint(); |
77 | 187 | }
|
| 188 | + //Repaint(); |
78 | 189 | }
|
79 | 190 | }
|
0 commit comments