美文网首页Unity 学习分享
Unity 编辑器扩展之 MenuItem

Unity 编辑器扩展之 MenuItem

作者: 游戏创作者 | 来源:发表于2022-01-10 14:34 被阅读0次

    MenuItem菜单项

    说明:创建一个菜单项,并调用其后面的静态函数

    1.API和参数

    public MenuItem(string itemName, bool isValidateFunction, int priority);
    

    例子:

    public class RunGame : MonoBehaviour
      {
        [MenuItem("XRL/Run", priority = 0)]
        static void Run()
        {
          var scenes = EditorBuildSettings.scenes;
          if (scenes.Length > 0)
          {
            var mainScene = scenes[0];
            var isGo = GoToScene(mainScene.path);
            if (isGo)
            {
              EditorApplication.isPlaying = true;
            }
          }
        }
    
        [MenuItem("XRL/Run", true, priority = 0)]
        static bool IsRun()
        {
          return !Application.isPlaying;
        }
    
        static bool GoToScene(string path)
        {
          var activeScene = EditorSceneManager.GetActiveScene();
          if (!activeScene.path.Equals(path))
          {
            if (activeScene.isDirty)
            {
              int opration = EditorUtility.DisplayDialogComplex("Scene Have Been Modified", "Do you want to save the change", "Save", "Don't Save", "Cancel");
              switch (opration)
              {
                case 0:
                  if (EditorSceneManager.SaveScene(activeScene) == false)
                    return false;
                  break;
                case 1:
                  break;
                case 2:
                  return false;
              }
            }
    
            EditorSceneManager.OpenScene(path);
          }
          return true;
        }
      }
    

    上面代码的作用是运行游戏的第0个场景,注意:当Unity的模式为运行状态时,菜单栏不可点。

    isValidateFunction == true

    2.设置快捷键

    ItemName为菜单路径名称,可以添加菜单快捷键

    _w 单一快捷键;
    

    3. 给菜单栏添加菜单

    [MenuItem("XRL/AssetBundle/Build")]
    
    给菜单栏添加菜单

    4.给 Hierarchy 添加菜单

    [MenuItem("GameObject/XRL/TableView", false, 11)]
    
    给 Hierarchy 添加菜单

    5. 给 Project 添加菜单

    [MenuItem("Assets/AssetBundle/Set AssetBundle By Path")]
    
    给 Project 添加菜单

    6. 给组件添加菜单

    //[MenuItem("CONTEXT/组件名/方法名")]
    
    图片

    环境:
    Unity:2020.3.12f

    相关文章

      网友评论

        本文标题:Unity 编辑器扩展之 MenuItem

        本文链接:https://www.haomeiwen.com/subject/clvmcrtx.html