Skip to content

Commit b4bfd9d

Browse files
committed
Improved lists in inspector
1 parent 1134e5b commit b4bfd9d

File tree

3 files changed

+175
-60
lines changed

3 files changed

+175
-60
lines changed
Lines changed: 170 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,190 @@
1-
using MLAPI.MonoBehaviours.Core;
2-
using UnityEditor;
1+
using UnityEditor;
32
using UnityEngine;
43
using UnityEditorInternal;
4+
using MLAPI;
5+
using MLAPI.MonoBehaviours.Core;
56

6-
namespace UnityEditor
7+
[CustomEditor(typeof(NetworkingManager), true)]
8+
[CanEditMultipleObjects]
9+
public class NetworkingManagerEditor : Editor
710
{
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()
1129
{
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+
}
1340

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+
}
1652

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) =>
1857
{
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+
};
2486

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) =>
2689
{
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;
4397

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) =>
45114
{
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))
56179
{
57-
string instanceType = "";
58180
if (networkingManager.isHost)
59-
instanceType = "Host";
181+
networkingManager.StopHost();
60182
else if (networkingManager.isServer)
61-
instanceType = "Server";
183+
networkingManager.StopServer();
62184
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();
75186
}
76-
Repaint();
77187
}
188+
//Repaint();
78189
}
79190
}

MLAPI/Data/NetworkConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ public class NetworkConfig
4242
/// <summary>
4343
/// Channels used by the NetworkedTransport
4444
/// </summary>
45+
[HideInInspector]
4546
public List<Channel> Channels = new List<Channel>();
4647
/// <summary>
4748
/// Registered MessageTypes
4849
/// </summary>
50+
[HideInInspector]
4951
public List<MessageType> MessageTypes = new List<MessageType>();
5052
internal HashSet<ushort> PassthroughMessageHashSet = new HashSet<ushort>();
5153
internal HashSet<string> EncryptedChannelsHashSet = new HashSet<string>();
@@ -65,7 +67,6 @@ public class NetworkConfig
6567
/// The default player prefab
6668
/// </summary>
6769
[SerializeField]
68-
[HideInInspector]
6970
internal string PlayerPrefabName;
7071
/// <summary>
7172
/// The size of the receive message buffer. This is the max message size.

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ public class NetworkingManager : MonoBehaviour
2828
/// <summary>
2929
/// Gets or sets if the NetworkingManager should be marked as DontDestroyOnLoad
3030
/// </summary>
31+
[HideInInspector]
3132
public bool DontDestroy = true;
3233
/// <summary>
3334
/// Gets or sets if the application should be set to run in background
3435
/// </summary>
36+
[HideInInspector]
3537
public bool RunInBackground = true;
3638
/// <summary>
3739
/// The log level to use
3840
/// </summary>
41+
[HideInInspector]
3942
public LogLevel LogLevel = LogLevel.Normal;
4043
/// <summary>
4144
/// The singleton instance of the NetworkingManager

0 commit comments

Comments
 (0)