using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace TFramework { public class OnAssetsEvent : UnityEditor.AssetModificationProcessor { /// /// 资源删除事件 /// public static TAction DeleteAssetEvent; /// /// 资源移动事件 /// public static TAction MoveAssetEvent; public static TAction CreateAssetEvent; [InitializeOnLoadMethod] static void EditorApplication_projectChanged() { //--全局监听Project视图下的资源是否发生变化(添加 删除 移动等) //EditorApplication.projectChanged += delegate () //{ //Debug.Log("资源状态发生变化!"); //}; } //--监听“双击鼠标左键,打开资源”事件 public static bool IsOpenForEdit(string assetPath, out string message) { message = null; return true; } //--监听“资源即将被创建”事件 public static void OnWillCreateAsset(string path) { CreateAssetEvent?.Invoke(path); } //--监听“资源即将被保存”事件 public static string[] OnWillSaveAssets(string[] paths) { if (paths != null) { //Debug.Log("资源即将被保存 path :" + string.Join(",", paths)); } return paths; } //--监听“资源即将被移动”事件 public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath) { int id = GetInstanceID(oldPath); MoveAssetEvent?.Invoke(id); return AssetMoveResult.DidNotMove; } /// /// --监听 资源即将被删除事件 /// /// /// /// public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option) { int id = GetInstanceID(assetPath); DeleteAssetEvent?.Invoke(id); return AssetDeleteResult.DidNotDelete; } /// /// 获取资源ID /// /// 资源路径 /// static int GetInstanceID(string assetPath) { Object obj = AssetDatabase.LoadMainAssetAtPath(assetPath); return obj == null ? -1 : obj.GetInstanceID(); } /// /// 资源保护类型 /// public enum ProtectType { /// /// 删除 /// Delete, /// /// 移动 /// Move, } } }