using System.IO; using System.Reflection; using Type = System.Type; //加载 unity的布局文件 public static class LayoutUnity { private static MethodInfo _miLoadWindowLayout; private static MethodInfo _miSaveWindowLayout; private static MethodInfo _miReloadWindowLayoutMenu; private static bool _available; private static string _layoutsPath; static LayoutUnity() { Type tyWindowLayout = Type.GetType("UnityEditor.WindowLayout,UnityEditor"); Type tyEditorUtility = Type.GetType("UnityEditor.EditorUtility,UnityEditor"); Type tyInternalEditorUtility = Type.GetType("UnityEditorInternal.InternalEditorUtility,UnityEditor"); if (tyWindowLayout != null && tyEditorUtility != null && tyInternalEditorUtility != null) { MethodInfo miGetLayoutsPath = tyWindowLayout.GetMethod("GetLayoutsPath", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); _miLoadWindowLayout = tyWindowLayout.GetMethod("LoadWindowLayout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(bool) }, null); _miSaveWindowLayout = tyWindowLayout.GetMethod("SaveWindowLayout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string) }, null); _miReloadWindowLayoutMenu = tyInternalEditorUtility.GetMethod("ReloadWindowLayoutMenu", BindingFlags.Public | BindingFlags.Static); if (miGetLayoutsPath == null || _miLoadWindowLayout == null || _miSaveWindowLayout == null || _miReloadWindowLayoutMenu == null) return; _layoutsPath = (string)miGetLayoutsPath.Invoke(null, null); if (string.IsNullOrEmpty(_layoutsPath)) return; _available = true; } } // Gets a value indicating whether all required Unity API functionality is available for usage. public static bool IsAvailable { get { return _available; } } // Gets absolute path of layouts directory. Returns `null` when not available. public static string LayoutsPath { get { return _layoutsPath; } } // Save current window layout to asset file. `assetPath` must be relative to project directory. // 将当前窗口布局保存到资产文件中。“assetPath”必须相对于项目目录。 public static void SaveLayoutToAsset(string assetPath) { SaveLayout(Path.Combine(Directory.GetCurrentDirectory(), assetPath)); } // 导入布局文件 public static void LoadLayoutFromAsset(string assetPath) { if (_miLoadWindowLayout != null) { string path = Path.Combine(Directory.GetCurrentDirectory(), assetPath); _miLoadWindowLayout.Invoke(null, new object[] { path, true }); } } // Save current window layout to file. `path` must be absolute.//将当前窗口布局保存到文件中。“路径”必须是绝对的 public static void SaveLayout(string path) { if (_miSaveWindowLayout != null) _miSaveWindowLayout.Invoke(null, new object[] { path }); } }