コード例 #1
0
    private void OnGUI()
    {
        // Поиск обработчика аргументов и запуск в редакторе Unity в качестве сервера
        if (runEditorAsServer && EditorApplication.isPlaying)
        {
            if (isServerEnabled)
            {
                if (FindArgHandlerObj())
                {
                    handler.EditorEvent("server", ip, port);
                    isServerEnabled = false;
                }
            }
        }

        // Поиск обработчика аргументов и запуск в редакторе Unity в качестве клиента
        if (runEditorAsClient && EditorApplication.isPlaying)
        {
            if (FindArgHandlerObj())
            {
                runEditorAsClient = false;
                handler.EditorEvent("client", ip, port);
            }
        }

        scrollPos = EditorGUILayout.BeginScrollView(scrollPos); // Память для вертикального скролла

        EditorGUILayout.Space();
        countOfInstances = EditorGUILayout.IntField(CountOfInstancesLabel, countOfInstances);
        runServer        = EditorGUILayout.Toggle(ServerRunLabel, runServer);

        // Логика поведения чекбоксов
        if (countOfInstances != 1 && runServer)
        {
            // чекбокс на автозапуск клиента
            runClients = EditorGUILayout.Toggle(ClientRunLabel, runClients);
        }
        else
        {
            runClients = false;
        }

        // чекбокс на автозапуск приложения в редакторе
        runInEditor = EditorGUILayout.Toggle(EditorRunLabel, runInEditor);

        // проверка на принудительный запуск сервера в редакторе, когда выбран один экземпляр приложения и запуск выполняется в unity
        if ((countOfInstances == 1) && runServer && runInEditor)
        {
            runEditorAsServer = true;
        }

        // отображение чекбокса "runEditorAsServer", когда это возможно
        if (runInEditor && runServer)
        {
            EditorGUI.indentLevel = 1;
            runEditorAsServer     = EditorGUILayout.Toggle(EditorIsServerLabel, runEditorAsServer);
            EditorGUI.indentLevel = 0;
        }
        else
        {
            runEditorAsServer = false;
        }
        // конец логики чекбоксов

        EditorGUILayout.Space();

        // Отображение прочих опций
        showOtherConfigs = EditorGUILayout.Toggle(OtherConfigLabel, showOtherConfigs);
        if (showOtherConfigs)
        {
            EditorGUI.indentLevel = 1;
            argHandlerTag         = EditorGUILayout.TextField(TagNameLabel, argHandlerTag);
            ip   = EditorGUILayout.TextField(IpLabel, ip);
            port = EditorGUILayout.IntField(PortLabel, port);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField(BuildConfigLabel);

            countOfScenes = EditorGUILayout.IntField(CountOfScenesLabel, countOfScenes);
            if (countOfScenes > MaxCountOfScenes)
            {
                countOfScenes = MaxCountOfScenes;
            }
            else if (countOfScenes < MinCountOfScenes)
            {
                countOfScenes = MinCountOfScenes;
            }
            else
            {
                if ((countOfScenes != oldCountOfScenes))          // Проверка состояния изменения сцены
                {
                    sceneNames       = new string[countOfScenes]; // Выделить новый массив с новым количеством сцен
                    scenes           = new SceneAsset[countOfScenes];
                    oldCountOfScenes = countOfScenes;             // Указываем новое значение для дальнейших проверок
                }

                for (int i = 0; i < countOfScenes; i++) // Отрисовываем и записываем значения полей в массив scenes
                {
                    scenes[i] = EditorGUILayout.ObjectField(SceneLabel + i, scenes[i], typeof(SceneAsset), false) as SceneAsset;
                }
            }

            system    = (OperatingSystem)EditorGUILayout.EnumPopup(OsLabel, system); // Отрисовка селектора операционной системы
            buildPath = EditorGUILayout.TextField(BuildPathLabel, buildPath);

            EditorGUI.indentLevel = 0;
        }

        if (countOfInstances < MinCountOfInstances)
        {
            countOfInstances = MinCountOfInstances;
        }
        else if (countOfInstances > MaxCountOfInstances)
        {
            countOfInstances = MaxCountOfInstances;
        }

        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button(RunButtonLabel))
        {
            Run();
        }

        if (GUILayout.Button(BuildRunButtonLabel))
        {
            BuildAndRun();

            // Костыль! После билда юнити забывает о том, что разметка перешла в горизонталь
            EditorGUILayout.BeginHorizontal();
        }

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndScrollView();
    }