WXPlayerPrefsWindow.cs 4.3 KB
Newer Older
J
junkunzhang 已提交
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
using UnityEngine;
using UnityEditor;

namespace WeChatWASM
{
    public class WXPlayerPrefsWindow : EditorWindow
    {

        public WXEditorScriptObject config;
        public string tempKey = "";


        
        [MenuItem("微信小游戏 / PlayerPrefs优化")]
        public static void Open()
        {

            var win = GetWindow(typeof(WXPlayerPrefsWindow), false, "配置PlayerPrefs用到的KeyName", true);//创建窗口
            win.minSize = new Vector2(700, 300);
            //win.maxSize = new Vector2(400, 200);
            win.Show();

        }

        public void LoadData()
        {

            config = UnityUtil.GetEditorConf();

        }

        public void OnEnable()
        {
            LoadData();
        }

        private void OnFocus()
        {
            LoadData();
        }


        private void OnLostFocus()
        {

            EditorUtility.SetDirty(config);
            AssetDatabase.SaveAssets();
        }


        private void OnGUI()
        {
            var labelStyle = new GUIStyle(EditorStyles.boldLabel);
            labelStyle.fontSize = 14;

            labelStyle.margin.left = 20;
            labelStyle.margin.top = 10;
            labelStyle.margin.bottom = 10;

            GUILayout.Label("设置PlayerPrefs用到的KeyName,能加快第一次的查询速度", labelStyle);

            GUIStyle pathButtonStyle = new GUIStyle(GUI.skin.button);
            pathButtonStyle.fontSize = 12;
            pathButtonStyle.margin.left = 20;

            var inputStyle = new GUIStyle(EditorStyles.textField);
            inputStyle.fontSize = 14;
            inputStyle.margin.left = 20;
            inputStyle.margin.bottom = 10;
            inputStyle.margin.right = 20;

            var intPopupStyle = new GUIStyle(EditorStyles.popup);
            intPopupStyle.fontSize = 14;
            intPopupStyle.margin.left = 20;
            intPopupStyle.margin.bottom = 15;
            intPopupStyle.margin.right = 20;


            int pathButtonHeight = 28;
            GUIStyle pathLabelStyle = new GUIStyle(GUI.skin.textField);

            pathLabelStyle.fontSize = 12;
            pathLabelStyle.alignment = TextAnchor.MiddleLeft;
            pathLabelStyle.margin.top = 6;
            pathLabelStyle.margin.bottom = 6;
            pathLabelStyle.margin.left = 20;


            tempKey = EditorGUILayout.TextField("KeyName", tempKey, inputStyle);


            EditorGUILayout.Space();
            EditorGUILayout.Space();

            var clickAdd = GUILayout.Button("添加", pathButtonStyle, GUILayout.Height(30), GUILayout.Width(200));

            if (clickAdd)
            {

                if (string.IsNullOrEmpty(tempKey))
                {
                    Debug.LogError("Key不能为空!");
                    return;
                }
                if (config.PlayerPrefsKeys.Contains(tempKey)) {
                    Debug.LogError("目录已经添加!");
                    return;
                }

                config.PlayerPrefsKeys.Add(tempKey);
                OnLostFocus();
                tempKey = "";
            }

            EditorGUILayout.Space();
            EditorGUILayout.Space();

            GUILayout.Label("已添加Key:", labelStyle);

            if (config.PlayerPrefsKeys != null && config.PlayerPrefsKeys.Count > 0)
            {
                for (int i = 0; i < config.PlayerPrefsKeys.Count; i++)
                {
                    string options = config.PlayerPrefsKeys[i];

                    GUILayout.BeginHorizontal();
                    // 路径框
                    GUILayout.Label(options, pathLabelStyle, GUILayout.Height(pathButtonHeight - 6), GUILayout.ExpandWidth(true), GUILayout.MaxWidth(EditorGUIUtility.currentViewWidth - 190));

                    var isDelete = GUILayout.Button("删除", GUILayout.Height(pathButtonHeight), GUILayout.Width(40));
                    if (isDelete)
                    {
                        config.PlayerPrefsKeys.Remove(options);
                        OnLostFocus();
                    }
                    GUILayout.EndHorizontal();
                }

            }




        }



    }
}