拓展Project视图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestProject : MonoBehaviour
{
[MenuItem("Assets/Test1",false,0)]
static void Test1() { Debug.Log("成功"); }
[InitializeOnLoadMethod]
static void IntiallizeOnLoad()
{
//project资源右侧出现按键操作
EditorApplication.projectWindowItemOnGUI += (guid, rect) =>
{
//GUI.Button(new Rect(100, 100, 100, 100), "Test1");
if (Selection.activeObject != null )
{
string active_guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject));
if (active_guid == guid && !string.IsNullOrEmpty(active_guid))
{
rect.x = rect.width - 100;
rect.width = 100;
if(GUI.Button(rect, "delete"))
{
Debug.Log("删除文件:" + AssetDatabase.GetAssetPath(Selection.activeObject));
}
}
}
};
}
}
事件监听
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TestProjectEvent : UnityEditor.AssetModificationProcessor
{//回调函数
//监听创建事件
public static void OnWillCreateAsset(string path)
{
Debug.LogFormat("创建资源:{0}", path);
}
//监听(将要被保存的时候,触发)
public static string[] OnWillSaveAssets(string[] paths)
{
for (int i = 0; i < paths.Length; i++)
{
Debug.LogFormat("输出保存资源:{0}", paths[i]);
}
return paths;
}
//监听移动事件
public static AssetMoveResult OnWillMoveAsset(string oldPath,string newPath)
{
Debug.LogFormat("移动资源:{0}移动到{1}", oldPath,newPath);
return AssetMoveResult.DidNotMove;//默认可移动
//DidNotMov不可移动 FailedMove无法移动
}
//监听删除事件
public static AssetDeleteResult OnWillDeleteAsset(string path,RemoveAssetOptions options)
{
Debug.LogFormat("删除资源:{0} Option:{1}", path, options);
return AssetDeleteResult.DidNotDelete;//默认可删除
//DidDelete不可删除 FailedDelete无法删除
}
[InitializeOnLoadMethod]
static void InitOnLoad() //监听资源变动
{
EditorApplication.projectChanged += () => {
Debug.Log("Asset Changes!");
};
}
}