(一)Unity编辑器扩展之Project视图面板

1、前言

研究一下Unity编辑器的扩展,并记录一下,防止以后以往,也是一种交流吧。Unity编辑器有五大面板,Project、Game、Scene、Hierarchy、Inspector,先来说一下Project面板。

2、拓展右键菜单

主要用到[MenuIteam(“Assets/名字”,bool(为真则禁用),int(排序,值小在最上面)],直接上代码

using UnityEngine;
using UnityEditor;

public class ControllerEditor {
  
    //------------------扩展Project面板--------------------
    //-----------------拓展右键菜单-----------------
    //---------[MenuIteam("Assets/名字",bool(为真则禁用),int(排序,值小在最上面)]
    [MenuItem("Assets/MyTools/Tools1",false,2)]
    static void MyTools1()
    {
        Debug.Log(Selection.activeObject.name);
    }
    [MenuItem("Assets/MyTools/Tools2",false,9)]
    static void MyTools2()
    {
        Debug.Log(Selection.activeObject.name);
    }
    [MenuItem("Assets/MyTools/Tools3", false, 1)]
    static void MyTools3()
    {
        Debug.Log(Selection.activeObject.name);
    }
   
    //--------------------拓展 Create创建菜单-----------------------------------
    [MenuItem("Assets/Create/My Create/cube",false,2)]
    static void CreateCube()
    {
        GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
    [MenuItem("Assets/Create/My Create/sphere",false,1)]
    static void CreateSphere()
    {
        GameObject.CreatePrimitive(PrimitiveType.Sphere);
    }
   }

效果:
(一)Unity编辑器扩展之Project视图面板_第1张图片
(一)Unity编辑器扩展之Project视图面板_第2张图片
3、拓展布局
在Project视图中对象的右侧拓展自定义按钮,选中资源后,右边将出现拓展后的click按钮,点击按钮触发时间,这里是输出资源名字。

using UnityEngine;
using UnityEditor;

public class ControllerEditor {

    [InitializeOnLoadMethod]//这个特性作用:C#编译完成后首先执行该方法
    static void InitializeOnLoadMethod()
    {
        EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect)
          {
              if (Selection.activeObject &&
                  guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject)))
              {
                  //设置按钮区域
                  float width = 50f;
                  selectionRect.x += (selectionRect.width = width);
                  selectionRect.y += 2f;
                  selectionRect.width = width;
                  GUI.color = Color.red;
                  //点击按钮
                  if (GUI.Button(selectionRect, "click"))
                  {
                      Debug.LogFormat("click:{0}", Selection.activeObject.name);

                  }
                  GUI.color = Color.white;
  
              }
          };
    }
}

效果:
(一)Unity编辑器扩展之Project视图面板_第3张图片

4、监听面板

监听面板资源的创建删除移动和保存,Unity提供了监听的基类,继承UnityEditor.AssetModificationProcessor,接着重新监听资源创建、删除、一定和保存方法,上代码:

using UnityEngine;
using UnityEditor;

public class ControllerProjectLisener:UnityEditor.AssetModificationProcessor  {
    
    [InitializeOnLoadMethod]//编译完成后首先执行该方法
    static void InitializeOnLoadMethod()
    {
        //全局监听Project视图下的资源是否发生变化
        EditorApplication.projectChanged+= delegate ()
          {
              Debug.Log("Change");
          };
    }
    /// 
    /// 监听“双击鼠标左键打开资源”事件
    /// 
    /// 
    /// 
    /// 
    public static bool IsOpenForEdit(string assetPath,out string message)
    {
        message = null;
        Debug.LogFormat("assetPath:{0}", assetPath);
        return true;
    }
    /// 
    /// 监听资源即将被创建事件
    /// 
    /// 
    public static void OnWillCreateAsset(string path)
    {
        Debug.LogFormat("path:{0}", path);
    }
    /// 
    /// 监听资源即将被保存
    /// 
    /// 
    /// 
    public static string[] OnWillSaveAssets(string[] paths)
    {
        if(paths!=null)
        {
            Debug.LogFormat("path:{0}", string.Join(",", paths));
        }
        return paths;
    }
    /// 
    /// 监听资源被移动事件
    /// 
    /// 
    /// 
    /// 
    public static AssetMoveResult OnWillMoveAsset(string oldpath,string newpath)
    {
        Debug.LogFormat("form:{0}   to:   {1}", oldpath, newpath);
        return AssetMoveResult.DidMove;
    }
    /// 
    /// 监听资源即将被删除
    /// 
    /// 
    /// 
    /// 
    public static AssetDeleteResult OnWillDeleteAsset(string assetPath,RemoveAssetOptions options)
    {
        Debug.LogFormat("delete:{0}", assetPath);
        return AssetDeleteResult.DidNotDelete;
    }

}

下篇Hierarchy视图面板扩展,加油!,欢迎交流。

你可能感兴趣的:(编辑器扩展)