拓展编辑器(五)_监听事件
生活随笔
收集整理的這篇文章主要介紹了
拓展编辑器(五)_监听事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
監聽事件
這個是拓展Project視圖的最后一節,這里將會介紹一下如何借助程序來約束project視圖中的資源。例如用程序來判斷資源的原始位置以及將要移動的位置是否合法,從而決定是否能阻止本次移動。Unity為此提供了監聽的基類。
using UnityEngine; using UnityEditor;public class Script_04: UnityEditor.AssetModificationProcessor {[InitializeOnLoadMethod]static void InitializeOnLoadMethod(){//全局監聽Project視圖下的資源是否發生變化(添加,刪除和移動)EditorApplication.projectChanged += delegate (){Debug.Log("change");};}//監聽"雙擊鼠標左鍵,打開資源"事件public static bool IsOpenForEdit(string assetPath, out string message){message = null;Debug.LogFormat("assetPath:{0}", assetPath);//true標識該資源可以打開,false標識不允許在Unity中打開該資源return true;}//監聽"資源即將被創建"事件public static void OnWillCreateAsset(string path){Debug.LogFormat("path:{0}", path);}//監聽"資源即將被保存"事件public static string[] OnWillSaveAsset(string[] paths){if (paths != null){Debug.LogFormat("path:{0}", string.Join(",", paths));}return paths;}//監聽"資源即將被移動"事件public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath){Debug.LogFormat("from:{0} to:{1}", oldPath, newPath);//AsserMoveResult.DidMovereturn AssetMoveResult.DidMove;}//監聽"資源即將被刪除"事件public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions opint){Debug.LogFormat("delete:{0}", assetPath);//AssetDeleteResult.DidNotDelete表示該資源可以被刪除return AssetDeleteResult.DidNotDelete;} }這個類需要繼承UnityEditor.AssetModificationProcessor,然后重寫監聽資源創建,移動,保存和刪除的方法,處理自己的特殊邏輯。
轉載于:https://www.cnblogs.com/llllllvty/p/9857758.html
總結
以上是生活随笔為你收集整理的拓展编辑器(五)_监听事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++入门之浅入浅出类——分享给很多想形
- 下一篇: docker 配置阿里云镜像加速