LycianthesConfigWindow.cs 3.5 KB
Newer Older
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
/*
 *Copyright(C) 2023 by Chief All rights reserved.
 *Unity版本:2020.3.41f1c1 
 *作者:Chief  
 *创建日期: 2022-12-06 
 *模块说明:
 *版本: 1.0
*/

using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;

namespace Lycianthes
{
    /// <summary>
    /// 配置窗口
    /// </summary>
    public class LycianthesConfigWindow : OdinEditorWindow
    {
        /// <summary>
        /// 上次选择的默认脚本模板文件夹
        /// </summary>
        public const string KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER = "KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER";

        [MenuItem("Tools/Lycianthes/本地设置")]
        public static void ShowWindow()
        {
            var window = GetWindow<LycianthesConfigWindow>();
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(500, 375);
            window.titleContent = new GUIContent("本地配置");

36
            window.LocalConfig = LycianthesConfig.GetInstance();
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

            if (PlayerPrefs.HasKey(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER))
                window.ScriptTemplateFolder = PlayerPrefs.GetString(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER);
        }

        [Title("")]
        [PropertyOrder(10)]
        [ShowInInspector, HideLabel]
        public LycianthesConfig LocalConfig;

        [Title("")]
        [Button("确认并保存", ButtonSizes.Medium)]
        [PropertyOrder(20)]
        public void Save()
        {
            if (LocalConfig == null) return;
53
            PlayerPrefs.SetString(LycianthesConfig.KEY_LYCIANTHES_CONFIG, LocalConfig.GetJsonString());
54 55 56 57 58 59 60 61 62 63 64 65
        }

        [Title("脚本模板创建")]
        [FolderPath]
        [PropertyOrder(30)]
        [PropertySpace(25)]
        [LabelText("脚本模板文件夹")]
        public string ScriptTemplateFolder;

        /// <summary>
        /// 是否有合法的脚本模板文件夹
        /// </summary>
66
        private bool HasScriptTemplateFolder { get { return !string.IsNullOrEmpty(ScriptTemplateFolder); } }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

        [LabelText("脚本模板文件名")]
        [PropertyOrder(30)]
        [ShowIf("HasScriptTemplateFolder")]
        public string ScriptTemplateAssetName;

        /// <summary>
        /// 创建脚本模板
        /// </summary>
        [PropertyOrder(30)]
        [ShowIf("HasScriptTemplateFolder")]
        [Button("创建脚本模板", ButtonSizes.Medium)]
        public void CreateScriptTemplate()
        {
            if (!HasScriptTemplateFolder) return;

            if (string.IsNullOrEmpty(ScriptTemplateAssetName))
            {
                EditorUtility.DisplayDialog("Lycianthes配置", "脚本模板文件名不能为空!", "确定");
                return;
            }

89 90 91 92 93 94 95 96
            string path = $"{ScriptTemplateFolder}/{ScriptTemplateAssetName}.asset";
            //先加载一下是否有当前文件;
            var template = AssetDatabase.LoadAssetAtPath<LycianthesTemplateGroup>(path);
            if (template != null)
            {
                EditorUtility.DisplayDialog("Lycianthes配置", $"已经有此模板:{ScriptTemplateAssetName}\n不能重复创建!", "确定");
                return;
            }
97

98 99 100 101 102
            template = CreateInstance<LycianthesTemplateGroup>();
            AssetDatabase.CreateAsset(template, path);
            Debug.Log($"已生成模板:{path} : {template.name}");
            //缓存本次保存的文件夹
            PlayerPrefs.SetString(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER, ScriptTemplateFolder);
103 104 105 106
        }

    }
}