NetworkManagerHUDEditor.cs 18.4 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
#if ENABLE_UNET
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityObject = UnityEngine.Object;

namespace UnityEditor
{
    [CustomEditor(typeof(NetworkManagerHUD), true)]
    [CanEditMultipleObjects]
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class NetworkManagerHUDEditor : Editor
    {
        SerializedProperty m_ShowGUIProperty;
        SerializedProperty m_OffsetXProperty;
        SerializedProperty m_OffsetYProperty;

        protected GUIContent m_ShowNetworkLabel;
        protected GUIContent m_ShowServerLabel;
        protected GUIContent m_ShowServerConnectionsLabel;
        protected GUIContent m_ShowServerObjectsLabel;
        protected GUIContent m_ShowClientLabel;
        protected GUIContent m_ShowClientObjectsLabel;
        protected GUIContent m_ShowMatchMakerLabel;
        protected GUIContent m_ShowControlsLabel;
        protected GUIContent m_ShowRuntimeGuiLabel;
        protected GUIContent m_OffsetXLabel;
        protected GUIContent m_OffsetYLabel;

        bool m_ShowServer;
        bool m_ShowServerConnections;
        bool m_ShowServerObjects;
        bool m_ShowClient;
        bool m_ShowClientObjects;
        bool m_ShowMatchMaker;

        bool m_ShowControls;

        bool m_Initialized;


        NetworkManagerHUD m_ManagerHud;
        NetworkManager m_Manager;

        void Init()
        {
            if (m_Initialized)
            {
                if (m_ShowGUIProperty == null)
                {
                    // initialize again.. something got broken
                }
                else
                {
                    return;
                }
            }
            m_Initialized = true;
            m_ManagerHud = target as NetworkManagerHUD;
            if (m_ManagerHud != null)
            {
                m_Manager = m_ManagerHud.manager;
            }

            m_ShowGUIProperty = serializedObject.FindProperty("showGUI");
            m_OffsetXProperty = serializedObject.FindProperty("offsetX");
            m_OffsetYProperty = serializedObject.FindProperty("offsetY");

            m_ShowServerLabel = TextUtility.TextContent("Server Info", "Details of internal server state");
            m_ShowServerConnectionsLabel = TextUtility.TextContent("Server Connections", "List of local and remote network connections to the server");
            m_ShowServerObjectsLabel = TextUtility.TextContent("Server Objects", "Networked objects spawned by the server");
            m_ShowClientLabel = TextUtility.TextContent("Client Info", "Details of internal client state");
            m_ShowClientObjectsLabel = TextUtility.TextContent("Client Objects", "Networked objects created on the client");
            m_ShowMatchMakerLabel = TextUtility.TextContent("MatchMaker Info", "Details about the matchmaker state");
            m_ShowControlsLabel = TextUtility.TextContent("Runtime Controls", "Buttons for controlling network state at runtime");
            m_ShowRuntimeGuiLabel = TextUtility.TextContent("Show Runtime GUI", "Show the default network control GUI when the game is running");
            m_OffsetXLabel = TextUtility.TextContent("GUI Horizontal Offset", "Horizontal offset of runtime GUI");
            m_OffsetYLabel = TextUtility.TextContent("GUI Vertical Offset", "Vertical offset of runtime GUI");
        }

        List<bool> m_ShowDetailForConnections;
        List<bool> m_ShowPlayersForConnections;
        List<bool> m_ShowVisibleForConnections;
        List<bool> m_ShowOwnedForConnections;

        void ShowServerConnections()
        {
            m_ShowServerConnections = EditorGUILayout.Foldout(m_ShowServerConnections, m_ShowServerConnectionsLabel);
            if (m_ShowServerConnections)
            {
                EditorGUI.indentLevel += 1;

                // ensure arrays of bools exists and are large enough
                if (m_ShowDetailForConnections == null)
                {
                    m_ShowDetailForConnections = new List<bool>();
                    m_ShowPlayersForConnections = new List<bool>();
                    m_ShowVisibleForConnections = new List<bool>();
                    m_ShowOwnedForConnections = new List<bool>();
                }
                while (m_ShowDetailForConnections.Count < NetworkServer.connections.Count)
                {
                    m_ShowDetailForConnections.Add(false);
                    m_ShowPlayersForConnections.Add(false);
                    m_ShowVisibleForConnections.Add(false);
                    m_ShowOwnedForConnections.Add(false);
                }

                // all connections
                int index = 0;
                foreach (var con in NetworkServer.connections)
                {
                    if (con == null)
                    {
                        index += 1;
                        continue;
                    }

                    m_ShowDetailForConnections[index] = EditorGUILayout.Foldout(m_ShowDetailForConnections[index], "Conn: " + con.connectionId + " (" + con.address + ")");
                    if (m_ShowDetailForConnections[index])
                    {
                        EditorGUI.indentLevel += 1;

                        m_ShowPlayersForConnections[index] = EditorGUILayout.Foldout(m_ShowPlayersForConnections[index], "Players");
                        if (m_ShowPlayersForConnections[index])
                        {
                            EditorGUI.indentLevel += 1;
                            foreach (var player in con.playerControllers)
                            {
                                EditorGUILayout.ObjectField("Player: " + player.playerControllerId, player.gameObject, typeof(GameObject), true);
                            }
                            EditorGUI.indentLevel -= 1;
                        }

                        m_ShowVisibleForConnections[index] = EditorGUILayout.Foldout(m_ShowVisibleForConnections[index], "Visible Objects");
                        if (m_ShowVisibleForConnections[index])
                        {
                            EditorGUI.indentLevel += 1;
                            foreach (var v in con.visList)
                            {
                                EditorGUILayout.ObjectField("NetId: " + v.netId, v, typeof(NetworkIdentity), true);
                            }
                            EditorGUI.indentLevel -= 1;
                        }

                        if (con.clientOwnedObjects != null)
                        {
                            m_ShowOwnedForConnections[index] = EditorGUILayout.Foldout(m_ShowOwnedForConnections[index], "Owned Objects");
                            if (m_ShowOwnedForConnections[index])
                            {
                                EditorGUI.indentLevel += 1;
                                foreach (var netId in con.clientOwnedObjects)
                                {
                                    var obj = NetworkServer.FindLocalObject(netId);
                                    EditorGUILayout.ObjectField("Owned: " + netId, obj, typeof(NetworkIdentity), true);
                                }
                                EditorGUI.indentLevel -= 1;
                            }
                        }
                        EditorGUI.indentLevel -= 1;
                    }
                    index += 1;
                }
                EditorGUI.indentLevel -= 1;
            }
        }

        void ShowServerObjects()
        {
            m_ShowServerObjects = EditorGUILayout.Foldout(m_ShowServerObjects, m_ShowServerObjectsLabel);
            if (m_ShowServerObjects)
            {
                EditorGUI.indentLevel += 1;

                foreach (var obj in NetworkServer.objects)
                {
                    string first = "NetId:" + obj.Key;
                    GameObject value = null;
                    if (obj.Value != null)
                    {
                        NetworkIdentity uv = obj.Value.GetComponent<NetworkIdentity>();
                        first += " SceneId:" + uv.sceneId;
                        value = obj.Value.gameObject;
                    }
                    EditorGUILayout.ObjectField(first, value, typeof(GameObject), true);
                }
                EditorGUI.indentLevel -= 1;
            }
        }

        void ShowServerInfo()
        {
            if (!NetworkServer.active)
            {
                return;
            }

            m_ShowServer = EditorGUILayout.Foldout(m_ShowServer, m_ShowServerLabel);
            if (!m_ShowServer)
            {
                return;
            }

            EditorGUI.indentLevel += 1;
            EditorGUILayout.BeginVertical();
            ShowServerConnections();
            ShowServerObjects();
            EditorGUILayout.EndVertical();
            EditorGUI.indentLevel -= 1;
        }

        void ShowClientObjects()
        {
            m_ShowClientObjects = EditorGUILayout.Foldout(m_ShowClientObjects, m_ShowClientObjectsLabel);
            if (m_ShowClientObjects)
            {
                EditorGUI.indentLevel += 1;
                foreach (var obj in ClientScene.objects)
                {
                    string first = "NetId:" + obj.Key;
                    GameObject value = null;
                    if (obj.Value != null)
                    {
                        NetworkIdentity id = obj.Value.GetComponent<NetworkIdentity>();
                        first += " SceneId:" + id.sceneId;
                        value = obj.Value.gameObject;
                    }
                    EditorGUILayout.ObjectField(first, value, typeof(GameObject), true);
                }
                EditorGUI.indentLevel -= 1;
            }
        }

        void ShowClientInfo()
        {
            if (!NetworkClient.active)
            {
                return;
            }

            m_ShowClient = EditorGUILayout.Foldout(m_ShowClient, m_ShowClientLabel);
            if (!m_ShowClient)
            {
                return;
            }

            EditorGUI.indentLevel += 1;
            EditorGUILayout.BeginVertical();

            int count = 0;
            foreach (var cl in NetworkClient.allClients)
            {
                if (cl.connection == null)
                {
                    EditorGUILayout.TextField("client " + count + ": ", cl.GetType().Name + " Conn: null");
                }
                else
                {
                    EditorGUILayout.TextField("client " + count + ":" ,  cl.GetType().Name + " Conn: " + cl.connection);
                    EditorGUI.indentLevel += 1;
                    foreach (var p in cl.connection.playerControllers)
                    {
                        EditorGUILayout.LabelField("Player", p.ToString());
                    }
                    EditorGUI.indentLevel -= 1;
                }
                count++;
            }

            ShowClientObjects();
            EditorGUILayout.EndVertical();
            EditorGUI.indentLevel -= 1;
        }

        void ShowMatchMakerInfo()
        {
            if (m_Manager == null || m_Manager.matchMaker == null)
            {
                return;
            }

            m_ShowMatchMaker = EditorGUILayout.Foldout(m_ShowMatchMaker, m_ShowMatchMakerLabel);
            if (!m_ShowMatchMaker)
            {
                return;
            }

            EditorGUI.indentLevel += 1;
            EditorGUILayout.BeginVertical();

            EditorGUILayout.LabelField("Match Information", m_Manager.matchInfo == null ? "None" : m_Manager.matchInfo.ToString());

            EditorGUILayout.EndVertical();
            EditorGUI.indentLevel -= 1;
        }

        static UnityObject GetSceneObject(string sceneObjectName)
        {
            if (string.IsNullOrEmpty(sceneObjectName))
            {
                return null;
            }

            foreach (var editorScene in EditorBuildSettings.scenes)
            {
                if (editorScene.path.IndexOf(sceneObjectName) != -1)
                {
                    return AssetDatabase.LoadAssetAtPath(editorScene.path, typeof(UnityObject));
                }
            }
            return null;
        }

        static Rect GetButtonRect()
        {
            Rect rect = EditorGUILayout.GetControlRect();
            float endcap = rect.width / 6;
            Rect newRect = new Rect(rect.xMin + endcap, rect.yMin, rect.width - (endcap * 2), rect.height);
            return newRect;
        }

        void ShowControls()
        {
            m_ShowControls = EditorGUILayout.Foldout(m_ShowControls, m_ShowControlsLabel);
            if (!m_ShowControls)
            {
                return;
            }

            if (!string.IsNullOrEmpty(NetworkManager.networkSceneName))
            {
                EditorGUILayout.ObjectField("Current Scene:", GetSceneObject(NetworkManager.networkSceneName), typeof(UnityObject), true);
            }
            EditorGUILayout.Separator();

            if (!NetworkClient.active && !NetworkServer.active && m_Manager.matchMaker == null)
            {
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Toggle(false, "LAN Host", EditorStyles.miniButton))
                {
                    m_Manager.StartHost();
                }
                if (GUILayout.Toggle(false, "LAN Server", EditorStyles.miniButton))
                {
                    m_Manager.StartServer();
                }
                if (GUILayout.Toggle(false, "LAN Client", EditorStyles.miniButton))
                {
                    m_Manager.StartClient();
                }
                if (GUILayout.Toggle(false, "Start Matchmaker", EditorStyles.miniButton))
                {
                    m_Manager.StartMatchMaker();
                    m_ShowMatchMaker = true;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (NetworkClient.active && !ClientScene.ready)
            {
                if (GUI.Button(GetButtonRect(), "Client Ready"))
                {
                    ClientScene.Ready(m_Manager.client.connection);

                    if (ClientScene.localPlayers.Count == 0)
                    {
                        ClientScene.AddPlayer(0);
                    }
                }
            }

            if (NetworkServer.active || NetworkClient.active)
            {
                if (GUI.Button(GetButtonRect(), "Stop"))
                {
                    m_Manager.StopServer();
                    m_Manager.StopClient();
                }
            }
            if (!NetworkServer.active && !NetworkClient.active)
            {
                EditorGUILayout.Separator();
                if (m_Manager.matchMaker != null)
                {
                    if (m_Manager.matchInfo == null)
                    {
                        if (m_Manager.matches == null)
                        {
                            EditorGUILayout.BeginHorizontal();
                            if (GUILayout.Toggle(false, "Create Internet Match", EditorStyles.miniButton))
                            {
                                m_Manager.matchMaker.CreateMatch(m_Manager.matchName, m_Manager.matchSize, true, "", "", "", 0, 0, m_Manager.OnMatchCreate);
                            }
                            if (GUILayout.Toggle(false, "Find Internet Match", EditorStyles.miniButton))
                            {
                                m_Manager.matchMaker.ListMatches(0, 20, "", false, 0, 0, m_Manager.OnMatchList);
                            }
                            if (GUILayout.Toggle(false, "Stop MatchMaker", EditorStyles.miniButton))
                            {
                                m_Manager.StopMatchMaker();
                            }
                            EditorGUILayout.EndHorizontal();
                            m_Manager.matchName = EditorGUILayout.TextField("Room Name:", m_Manager.matchName);
                            m_Manager.matchSize = (uint)EditorGUILayout.IntField("Room Size:", (int)m_Manager.matchSize);

                            EditorGUILayout.BeginHorizontal();
                            if (GUILayout.Toggle(false, "Use Local Relay", EditorStyles.miniButton))
                            {
                                m_Manager.SetMatchHost("localhost", 1337, false);
                            }
                            if (GUILayout.Toggle(false, "Use Internet Relay", EditorStyles.miniButton))
                            {
                                m_Manager.SetMatchHost("mm.unet.unity3d.com", 443, true);
                            }
                            EditorGUILayout.EndHorizontal();

                            EditorGUILayout.Separator();
                        }
                        else
                        {
                            foreach (var match in m_Manager.matches)
                            {
                                if (GUI.Button(GetButtonRect(), "Join Match:" + match.name))
                                {
                                    m_Manager.matchName = match.name;
                                    m_Manager.matchSize = (uint)match.currentSize;
                                    m_Manager.matchMaker.JoinMatch(match.networkId, "", "", "", 0, 0, m_Manager.OnMatchJoined);
                                }
                            }
                            if (GUI.Button(GetButtonRect(), "Stop MatchMaker"))
                            {
                                m_Manager.StopMatchMaker();
                            }
                        }
                    }
                }
            }

            EditorGUILayout.Separator();
        }

        public override void OnInspectorGUI()
        {
            Init();

            serializedObject.Update();
            EditorGUILayout.PropertyField(m_ShowGUIProperty, m_ShowRuntimeGuiLabel);

            if (m_ManagerHud.showGUI)
            {
                EditorGUI.indentLevel += 1;
                EditorGUILayout.PropertyField(m_OffsetXProperty, m_OffsetXLabel);
                EditorGUILayout.PropertyField(m_OffsetYProperty, m_OffsetYLabel);
                EditorGUI.indentLevel -= 1;
            }
            serializedObject.ApplyModifiedProperties();

            if (!Application.isPlaying)
            {
                return;
            }

            ShowControls();
            ShowServerInfo();
            ShowClientInfo();
            ShowMatchMakerInfo();
        }
    }
}
#endif //ENABLE_UNET