PlayPrefs 값을 편집하는 편집기 확장

8244 단어 Unity

코드


다음 코드는 Assets/Editor 하위 등에 저장됩니다.
EditPlayerPrefs.cs
using UnityEngine;
using UnityEditor;
using System.Collections;

public class EditPlayerPrefs : EditorWindow {

    class PrefInfo {
        public string Key;
        public string Type;
    }

    // 表示対象のキーとタイプ
    PrefInfo[] keys = new PrefInfo[] {
        new PrefInfo { Key = "hoge", Type = "string" },
        new PrefInfo { Key = "fuga", Type = "int" },
    };

    [MenuItem("Window/Edit PlayerPrefs")]
    public static void ShowWindow() {
        EditorWindow win = EditorWindow.GetWindow<EditPlayerPrefs>();
        win.title = "PlayerPrefs";
    }

    void OnGUI() {
        foreach(var keyInfo in keys) {
            switch(keyInfo.Type) {
            case "string":
                var originalString = PlayerPrefs.GetString(keyInfo.Key);
                var currentString = EditorGUILayout.TextField(keyInfo.Key, originalString);
                if (currentString != originalString) {
                    PlayerPrefs.SetString(keyInfo.Key, currentString);
                    Debug.Log ("*** update " + originalString + " => " + currentString);
                }
                break;

            case "int":
                var originalInteger = PlayerPrefs.GetInt (keyInfo.Key);
                var currentInteger = System.Convert.ToInt32(EditorGUILayout.TextField(keyInfo.Key, originalInteger.ToString()));
                if (currentInteger != originalInteger) {
                    PlayerPrefs.SetInt(keyInfo.Key, currentInteger);
                    Debug.Log ("*** update " + originalInteger.ToString() + " => " + currentInteger.ToString());
                }
                break;

            default:
                throw new Claris.System.Exception.ApplicationError("Unsupported pref type : " + keyInfo.Type);
            }
        }
    }
}

사용법


PrefInfo[]keys 값을 적절하게 변경합니다.
[Window] - [Edit PlayProefs]에서 시작

좋은 웹페이지 즐겨찾기