1、拓展右键菜单
主要用到[MenuIteam(“Assets/名字”,bool(为真则禁用),int(排序,值小在最上面)]
代码示例
using UnityEditor;
using UnityEngine;
public class Test7_Project : MonoBehaviour
{
[MenuItem("Assets/MyTools/Tool1", false, 10)]
public static void MyTool1()
{
Debug.Log("MyTool1" + Selection.activeObject.name);
}
[MenuItem("Assets/MyTools/Tool2", false, 11)]
public static void MyTool2()
{
Debug.Log("MyTool2" + Selection.activeObject.name);
}
[MenuItem("Assets/MyTools/Tool3", false, 100)]
public static void MyTool3()
{
Debug.Log("MyTool2" + Selection.activeObject.name);
}
}
效果展示
2、拓展 Create创建菜单
主要用到[MenuIteam(“Assets/Create/名字”,bool(为真则禁用),int(排序,值小在最上面)]
代码示例
using UnityEditor;
using UnityEngine;
public class Test7_Project : MonoBehaviour
{
[MenuItem("Assets/Create/MyCreate/Test1", false, 10)]
public static void MyCreate()
{
Debug.Log("MyCreate1" + Selection.activeObject.name);
}
[MenuItem("Assets/Create/MyCreate/Test2", false, 11)]
public static void MyCreate2()
{
Debug.Log("MyCreate2" + Selection.activeObject.name);
}
}
效果展示
3、拓展布局
需要使用到事件EditorApplication.projectWindowItemOnGUI
在Project视图中对象的右侧拓展自定义按钮
打印选中文件的name
代码示例
using Codice.Client.BaseCommands;
using UnityEditor;
using UnityEngine;
public class Test7_ControllerEditor
{
static Rect rect;
[InitializeOnLoadMethod]
static void InitOnLoadMethod()
{
EditorApplication.projectWindowItemOnGUI += (string guid, Rect selectionRect) =>
{
if(Selection.activeObject
&& guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
if(Selection.assetGUIDs.Length > 0)
{
if (Selection.assetGUIDs[0] != guid)
return;
if (selectionRect.height > 20)
{
rect = new Rect(selectionRect)
{
y = selectionRect.y + selectionRect.height,
height = 20
};
}
else
{
rect = new Rect(selectionRect)
{
x = selectionRect.x + selectionRect.width - 50,
width = 50
};
}
GUI.color = Color.yellow;
//点击按钮
if (GUI.Button(rect, "Print"))
{
Debug.LogFormat("Print:{0}", Selection.activeObject.name);
}
GUI.color = Color.white;
}
}
};
}
}
效果展示
4、监听Project面板事件
监听面板资源的创建删除移动和保存,Unity提供了监听的基类,继承UnityEditor.AssetModificationProcessor,接着重新监听资源创建、删除、一定和保存方法
代码示例
using UnityEditor;
using UnityEngine;
public class Test7_ControllerProjectLisener : UnityEditor.AssetModificationProcessor
{
//双击打开、资源被创建、资源被保存、资源被移动、资源被删除
[InitializeOnLoadMethod]//编译完成后首先执行该方法
static void InitializeOnLoadMethod()
{
//全局监听Project视图下的资源是否发生变化
EditorApplication.projectChanged += delegate ()
{
Debug.Log("Change");
};
}
/// <summary>
/// 监听“双击鼠标左键打开资源”事件
/// </summary>
/// <param name="assetPath"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool IsOpenForEdit(string assetPath, out string message)
{
message = null;
Debug.LogFormat("assetPath:{0}", assetPath);
return true;
}
/// <summary>
/// 监听资源即将被创建事件
/// </summary>
/// <param name="path"></param>
public static void OnWillCreateAsset(string path)
{
Debug.LogFormat("path:{0}", path);
}
/// <summary>
/// 监听资源即将被保存
/// </summary>
/// <param name="paths"></param>
/// <returns></returns>
public static string[] OnWillSaveAssets(string[] paths)
{
if (paths != null)
{
Debug.LogFormat("path:{0}", string.Join(",", paths));
}
return paths;
}
/// <summary>
/// 监听资源被移动事件
/// </summary>
/// <param name="oldpath"></param>
/// <param name="newpath"></param>
/// <returns></returns>
public static AssetMoveResult OnWillMoveAsset(string oldpath, string newpath)
{
Debug.LogFormat("form:{0} to: {1}", oldpath, newpath);
return AssetMoveResult.DidMove;
}
/// <summary>
/// 监听资源即将被删除
/// </summary>
/// <param name="assetPath"></param>
/// <param name="options"></param>
/// <returns></returns>
public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
{
Debug.LogFormat("delete:{0}", assetPath);
return AssetDeleteResult.DidNotDelete;
}
}
效果展示