1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- namespace TFramework
- {
- public class OnAssetsEvent : UnityEditor.AssetModificationProcessor
- {
- /// <summary>
- /// 资源删除事件
- /// </summary>
- public static TAction<int> DeleteAssetEvent;
- /// <summary>
- /// 资源移动事件
- /// </summary>
- public static TAction<int> MoveAssetEvent;
- public static TAction<string> 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;
- }
- /// <summary>
- /// --监听 资源即将被删除事件
- /// </summary>
- /// <param name="assetPath"></param>
- /// <param name="option"></param>
- /// <returns></returns>
- public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
- {
- int id = GetInstanceID(assetPath);
- DeleteAssetEvent?.Invoke(id);
- return AssetDeleteResult.DidNotDelete;
- }
- /// <summary>
- /// 获取资源ID
- /// </summary>
- /// <param name="assetPath">资源路径</param>
- /// <returns></returns>
- static int GetInstanceID(string assetPath)
- {
- Object obj = AssetDatabase.LoadMainAssetAtPath(assetPath);
- return obj == null ? -1 : obj.GetInstanceID();
- }
- /// <summary>
- /// 资源保护类型
- /// </summary>
- public enum ProtectType
- {
- /// <summary>
- /// 删除
- /// </summary>
- Delete,
- /// <summary>
- /// 移动
- /// </summary>
- Move,
- }
- }
- }
|