NetworkLobbyManagerEditor.cs
10.2 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#if ENABLE_UNET
using System;
using UnityEngine;
using UnityEngine.Networking;
using UnityObject = UnityEngine.Object;
#pragma warning disable 618
namespace UnityEditor
{
[CustomEditor(typeof(NetworkLobbyManager), true)]
[CanEditMultipleObjects]
class NetworkLobbyManagerEditor : NetworkManagerEditor
{
SerializedProperty m_ShowLobbyGUIProperty;
SerializedProperty m_MaxPlayersProperty;
SerializedProperty m_MaxPlayersPerConnectionProperty;
SerializedProperty m_MinPlayersProperty;
SerializedProperty m_LobbyPlayerPrefabProperty;
SerializedProperty m_GamePlayerPrefabProperty;
GUIContent m_LobbySceneLabel;
GUIContent m_PlaySceneLabel;
GUIContent m_MaxPlayersLabel;
GUIContent m_MaxPlayersPerConnectionLabel;
GUIContent m_MinPlayersLabel;
GUIContent m_ShowLobbyGUILabel;
GUIContent m_LobbyPlayerPrefabLabel;
GUIContent m_GamePlayerPrefabLabel;
bool ShowSlots;
void InitLobby()
{
if (!m_Initialized)
{
m_LobbySceneLabel = TextUtility.TextContent("Lobby Scene", "The scene loaded for the lobby.");
m_PlaySceneLabel = TextUtility.TextContent("Play Scene", "The scene loaded to play the game.");
m_MaxPlayersLabel = TextUtility.TextContent("Max Players", "The maximum number of players allowed in the lobby.");
m_MaxPlayersPerConnectionLabel = TextUtility.TextContent("Max Players Per Connection", "The maximum number of players that each connection/client can have in the lobby. Defaults to 1.");
m_MinPlayersLabel = TextUtility.TextContent("Minimum Players", "The minimum number of players required to be ready for the game to start. If this is zero then the game can start with any number of players.");
m_ShowLobbyGUILabel = TextUtility.TextContent("Show Lobby GUI", "Enable to display the default lobby UI.");
m_LobbyPlayerPrefabLabel = TextUtility.TextContent("Lobby Player Prefab", "The prefab to use for a player in the Lobby Scene.");
m_GamePlayerPrefabLabel = TextUtility.TextContent("Game Player Prefab", "The prefab to use for a player in the Play Scene.");
m_ShowLobbyGUIProperty = serializedObject.FindProperty("m_ShowLobbyGUI");
m_MaxPlayersProperty = serializedObject.FindProperty("m_MaxPlayers");
m_MaxPlayersPerConnectionProperty = serializedObject.FindProperty("m_MaxPlayersPerConnection");
m_MinPlayersProperty = serializedObject.FindProperty("m_MinPlayers");
m_LobbyPlayerPrefabProperty = serializedObject.FindProperty("m_LobbyPlayerPrefab");
m_GamePlayerPrefabProperty = serializedObject.FindProperty("m_GamePlayerPrefab");
var lobby = target as NetworkLobbyManager;
if (lobby == null)
return;
if (lobby.lobbyScene != "")
{
var offlineObj = GetSceneObject(lobby.lobbyScene);
if (offlineObj == null)
{
Debug.LogWarning("LobbyScene '" + lobby.lobbyScene + "' not found. You must repopulate the LobbyScene slot of the NetworkLobbyManager");
lobby.lobbyScene = "";
}
}
if (lobby.playScene != "")
{
var onlineObj = GetSceneObject(lobby.playScene);
if (onlineObj == null)
{
Debug.LogWarning("PlayScene '" + lobby.playScene + "' not found. You must repopulate the PlayScene slot of the NetworkLobbyManager");
lobby.playScene = "";
}
}
}
Init();
}
public override void OnInspectorGUI()
{
if (m_DontDestroyOnLoadProperty == null || m_DontDestroyOnLoadLabel == null)
m_Initialized = false;
InitLobby();
var lobby = target as NetworkLobbyManager;
if (lobby == null)
return;
serializedObject.Update();
EditorGUILayout.PropertyField(m_DontDestroyOnLoadProperty, m_DontDestroyOnLoadLabel);
EditorGUILayout.PropertyField(m_RunInBackgroundProperty , m_RunInBackgroundLabel);
if (EditorGUILayout.PropertyField(m_LogLevelProperty))
{
LogFilter.currentLogLevel = (int)m_NetworkManager.logLevel;
}
ShowLobbyScenes();
EditorGUILayout.PropertyField(m_ShowLobbyGUIProperty, m_ShowLobbyGUILabel);
EditorGUILayout.PropertyField(m_MaxPlayersProperty, m_MaxPlayersLabel);
EditorGUILayout.PropertyField(m_MaxPlayersPerConnectionProperty, m_MaxPlayersPerConnectionLabel);
EditorGUILayout.PropertyField(m_MinPlayersProperty, m_MinPlayersLabel);
EditorGUILayout.PropertyField(m_LobbyPlayerPrefabProperty, m_LobbyPlayerPrefabLabel);
EditorGUI.BeginChangeCheck();
var newGamPlayer = EditorGUILayout.ObjectField(m_GamePlayerPrefabLabel, lobby.gamePlayerPrefab, typeof(NetworkIdentity), false);
if (EditorGUI.EndChangeCheck())
{
if (newGamPlayer == null)
{
m_GamePlayerPrefabProperty.objectReferenceValue = null;
}
else
{
var newGamePlayerIdentity = newGamPlayer as NetworkIdentity;
if (newGamePlayerIdentity != null)
{
if (newGamePlayerIdentity.gameObject != lobby.gamePlayerPrefab)
{
m_GamePlayerPrefabProperty.objectReferenceValue = newGamePlayerIdentity.gameObject;
}
}
}
}
EditorGUILayout.Separator();
ShowNetworkInfo();
ShowSpawnInfo();
ShowConfigInfo();
ShowSimulatorInfo();
serializedObject.ApplyModifiedProperties();
ShowDerivedProperties(typeof(NetworkLobbyManager), typeof(NetworkManager));
if (!Application.isPlaying)
return;
EditorGUILayout.Separator();
ShowLobbySlots();
}
protected void ShowLobbySlots()
{
var lobby = target as NetworkLobbyManager;
if (lobby == null)
return;
ShowSlots = EditorGUILayout.Foldout(ShowSlots, "LobbySlots");
if (ShowSlots)
{
EditorGUI.indentLevel += 1;
foreach (var slot in lobby.lobbySlots)
{
if (slot == null)
continue;
EditorGUILayout.ObjectField("Slot " + slot.slot, slot.gameObject, typeof(UnityObject), true);
}
EditorGUI.indentLevel -= 1;
}
}
void SetLobbyScene(NetworkLobbyManager lobby, string sceneName)
{
var prop = serializedObject.FindProperty("m_LobbyScene");
prop.stringValue = sceneName;
var offlineProp = serializedObject.FindProperty("m_OfflineScene");
offlineProp.stringValue = sceneName;
EditorUtility.SetDirty(lobby);
}
void SetPlayScene(NetworkLobbyManager lobby, string sceneName)
{
var prop = serializedObject.FindProperty("m_PlayScene");
prop.stringValue = sceneName;
var onlineProp = serializedObject.FindProperty("m_OnlineScene");
onlineProp.stringValue = ""; // this is set to empty deliberately to prevent base class functionality from interfering with LobbyManager
EditorUtility.SetDirty(lobby);
}
protected void ShowLobbyScenes()
{
var lobby = target as NetworkLobbyManager;
if (lobby == null)
return;
var offlineObj = GetSceneObject(lobby.lobbyScene);
EditorGUI.BeginChangeCheck();
var newOfflineScene = EditorGUILayout.ObjectField(m_LobbySceneLabel, offlineObj, typeof(SceneAsset), false);
if (EditorGUI.EndChangeCheck())
{
if (newOfflineScene == null)
{
SetLobbyScene(lobby, "");
}
else
{
if (newOfflineScene.name != lobby.offlineScene)
{
var sceneObj = GetSceneObject(newOfflineScene.name);
if (sceneObj == null)
{
Debug.LogWarning("The scene " + newOfflineScene.name + " cannot be used. To use this scene add it to the build settings for the project");
}
else
{
SetLobbyScene(lobby, newOfflineScene.name);
}
}
}
}
var onlineObj = GetSceneObject(lobby.playScene);
EditorGUI.BeginChangeCheck();
var newOnlineScene = EditorGUILayout.ObjectField(m_PlaySceneLabel, onlineObj, typeof(SceneAsset), false);
if (EditorGUI.EndChangeCheck())
{
if (newOnlineScene == null)
{
SetPlayScene(lobby, "");
}
else
{
if (newOnlineScene.name != m_NetworkManager.onlineScene)
{
var sceneObj = GetSceneObject(newOnlineScene.name);
if (sceneObj == null)
{
Debug.LogWarning("The scene " + newOnlineScene.name + " cannot be used. To use this scene add it to the build settings for the project");
}
else
{
SetPlayScene(lobby, newOnlineScene.name);
}
}
}
}
}
}
}
#pragma warning restore 618
#endif // ENABLE_UNET