• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

码起来

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

加载 unity的布局文件

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 });
    }
}

  

posted on 2020-03-07 08:37  码起来  阅读(520)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3