Unity 创建EditorWindow时在同级目录下创建.asset配置文件

public class Window:EditorWindow
{
    static WindowConfig windowCfg;
    static string applicationPath;
    static string scriptFolderPath;
    static Window window;    
    [MenuItem("Tools/Window")]
    static void ShowWindow()
    {
        window =EditorWindow.GetWindow("Window", true) as Window;
        MonoScript script = MonoScript.FromScriptableObject(window);
        string scriptPath = AssetDatabase.GetAssetPath(script);
        scriptFolderPath = scriptPath.Substring(0, scriptPath.Length - script.name.Length - 4);
        string windowCfgPath = scriptFolderPath + "/windowConfig.asset";
        if (!File.Exists(windowCfgPath))
        {
            windowCfg = WindowConfig.CreateInstance();
            AssetDatabase.CreateAsset(windowCfg, windowCfgPath);
        }
        else
        {
            windowCfg = AssetDatabase.LoadAssetAtPath(windowCfgPath);
        }
        applicationPath = Application.dataPath;

        window.Show();
        }
}

Note:继承自ScriptableObject的类必须有一个与它同名的脚本,否者Unity重启之后.asset文件将会报错。

你可能感兴趣的:(UnityEditor)