Unity 적용 설정 파일 생성

8456 단어 UnityC#
참가이 이벤트할 때 Editor에서 Settings반의 어떤 파일을 다시 쓰는 것을 들었을 때 아마도 이런 느낌일 거라고 생각해서 썼다.나는 내가 기본적으로 유닛을 거의 써 본 적이 없는 사람인지 모르겠다.
프로젝트 디렉토리 구조에는 다음과 같은 구조가 있습니다.
ProjectDirectory
ProjectName
|
|-- Assets
    |-- Editor
        |-- SettingsEditor.cs
    |-- Sctipts
        |-- Settings.cs
Editor에는 pull downmenu(Local, Develop, Production)를 통해 APIHost를 선택하고 Create 버튼을 누르면 Settings가 표시됩니다.cs 파일이 생성되었습니다.다음은 Editor의 코드 및 설정 화면입니다.
Editor/SettingsEditor.cs
using UnityEditor;
using UnityEngine;

public class SettingsEditor : EditorWindow
{

    public enum APIHost
    {
        Local,
        Develop,
        Production
    }

    APIHost _host;

    [MenuItem("Window/SettingsEditor")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(SettingsEditor));
    }

    void OnGUI()
    {
        GUILayout.Label ("Host", EditorStyles.boldLabel);
        _host = (APIHost)EditorGUILayout.EnumPopup ("API Host", _host);
        if (GUILayout.Button ("Create")) {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Directory.GetCurrentDirectory() + "/Assets/Scripts/Settings.cs"))
            {
                var host = "";
                switch (_host)
                {
                case APIHost.Local:
                    host = "http://127.0.0.1:8000";
                    break;
                case APIHost.Develop:
                    host = "http://develop.com";
                    break;
                case APIHost.Production:
                    host = "http://production.com";
                    break;
                }

                file.WriteLine ("using System;");
                file.WriteLine ("using System.Collections;");
                file.WriteLine ("using System.Collections.Generic;");
                file.WriteLine ("using UnityEngine;");
                file.WriteLine ("");
                file.WriteLine ("public static class Settings");
                file.WriteLine ("{");
                file.WriteLine (string.Format("    public static string Host = \"{0}\";", host));
                file.WriteLine ("}");
        }
        }
    }
}

Create 버튼을 누르면 다음과 같은 코드가 생성되고 설정값이 변경됩니다.
Scripts/Settings.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class Settings

{
    public static string Host = "http://develop.com";
}
이렇게 하면 코드를 일일이 다시 쓰지 않고 GUI 설정 화면에서 설정을 쉽게 다시 쓸 수 있다.

좋은 웹페이지 즐겨찾기