Unity 框架

OnAssetsEvent.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace TFramework
  6. {
  7. public class OnAssetsEvent : UnityEditor.AssetModificationProcessor
  8. {
  9. /// <summary>
  10. /// 资源删除事件
  11. /// </summary>
  12. public static TAction<int> DeleteAssetEvent;
  13. /// <summary>
  14. /// 资源移动事件
  15. /// </summary>
  16. public static TAction<int> MoveAssetEvent;
  17. public static TAction<string> CreateAssetEvent;
  18. [InitializeOnLoadMethod]
  19. static void EditorApplication_projectChanged()
  20. {
  21. //--全局监听Project视图下的资源是否发生变化(添加 删除 移动等)
  22. //EditorApplication.projectChanged += delegate ()
  23. //{
  24. //Debug.Log("资源状态发生变化!");
  25. //};
  26. }
  27. //--监听“双击鼠标左键,打开资源”事件
  28. public static bool IsOpenForEdit(string assetPath, out string message)
  29. {
  30. message = null;
  31. return true;
  32. }
  33. //--监听“资源即将被创建”事件
  34. public static void OnWillCreateAsset(string path)
  35. {
  36. CreateAssetEvent?.Invoke(path);
  37. }
  38. //--监听“资源即将被保存”事件
  39. public static string[] OnWillSaveAssets(string[] paths)
  40. {
  41. if (paths != null)
  42. {
  43. //Debug.Log("资源即将被保存 path :" + string.Join(",", paths));
  44. }
  45. return paths;
  46. }
  47. //--监听“资源即将被移动”事件
  48. public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
  49. {
  50. int id = GetInstanceID(oldPath);
  51. MoveAssetEvent?.Invoke(id);
  52. return AssetMoveResult.DidNotMove;
  53. }
  54. /// <summary>
  55. /// --监听 资源即将被删除事件
  56. /// </summary>
  57. /// <param name="assetPath"></param>
  58. /// <param name="option"></param>
  59. /// <returns></returns>
  60. public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
  61. {
  62. int id = GetInstanceID(assetPath);
  63. DeleteAssetEvent?.Invoke(id);
  64. return AssetDeleteResult.DidNotDelete;
  65. }
  66. /// <summary>
  67. /// 获取资源ID
  68. /// </summary>
  69. /// <param name="assetPath">资源路径</param>
  70. /// <returns></returns>
  71. static int GetInstanceID(string assetPath)
  72. {
  73. Object obj = AssetDatabase.LoadMainAssetAtPath(assetPath);
  74. return obj == null ? -1 : obj.GetInstanceID();
  75. }
  76. /// <summary>
  77. /// 资源保护类型
  78. /// </summary>
  79. public enum ProtectType
  80. {
  81. /// <summary>
  82. /// 删除
  83. /// </summary>
  84. Delete,
  85. /// <summary>
  86. /// 移动
  87. /// </summary>
  88. Move,
  89. }
  90. }
  91. }