拓展Project视图

image.png

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class TestProject : MonoBehaviour
  6. {
  7. [MenuItem("Assets/Test1",false,0)]
  8. static void Test1() { Debug.Log("成功"); }
  9. [InitializeOnLoadMethod]
  10. static void IntiallizeOnLoad()
  11. {
  12. //project资源右侧出现按键操作
  13. EditorApplication.projectWindowItemOnGUI += (guid, rect) =>
  14. {
  15. //GUI.Button(new Rect(100, 100, 100, 100), "Test1");
  16. if (Selection.activeObject != null )
  17. {
  18. string active_guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject));
  19. if (active_guid == guid && !string.IsNullOrEmpty(active_guid))
  20. {
  21. rect.x = rect.width - 100;
  22. rect.width = 100;
  23. if(GUI.Button(rect, "delete"))
  24. {
  25. Debug.Log("删除文件:" + AssetDatabase.GetAssetPath(Selection.activeObject));
  26. }
  27. }
  28. }
  29. };
  30. }
  31. }

事件监听

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class TestProjectEvent : UnityEditor.AssetModificationProcessor
  6. {//回调函数
  7. //监听创建事件
  8. public static void OnWillCreateAsset(string path)
  9. {
  10. Debug.LogFormat("创建资源:{0}", path);
  11. }
  12. //监听(将要被保存的时候,触发)
  13. public static string[] OnWillSaveAssets(string[] paths)
  14. {
  15. for (int i = 0; i < paths.Length; i++)
  16. {
  17. Debug.LogFormat("输出保存资源:{0}", paths[i]);
  18. }
  19. return paths;
  20. }
  21. //监听移动事件
  22. public static AssetMoveResult OnWillMoveAsset(string oldPath,string newPath)
  23. {
  24. Debug.LogFormat("移动资源:{0}移动到{1}", oldPath,newPath);
  25. return AssetMoveResult.DidNotMove;//默认可移动
  26. //DidNotMov不可移动 FailedMove无法移动
  27. }
  28. //监听删除事件
  29. public static AssetDeleteResult OnWillDeleteAsset(string path,RemoveAssetOptions options)
  30. {
  31. Debug.LogFormat("删除资源:{0} Option:{1}", path, options);
  32. return AssetDeleteResult.DidNotDelete;//默认可删除
  33. //DidDelete不可删除 FailedDelete无法删除
  34. }
  35. [InitializeOnLoadMethod]
  36. static void InitOnLoad() //监听资源变动
  37. {
  38. EditorApplication.projectChanged += () => {
  39. Debug.Log("Asset Changes!");
  40. };
  41. }
  42. }