Listen to events
This is the last section to expand the project view. Here we will introduce how to use programs to constrain Resources in the project view. For example, you can use a program to determine whether the original location of the resource and the location to be moved are legal, so as to determine whether the current move can be blocked. Unity provides a base class for listening.
Using unityengine; using unityeditor; public class script_04: unityeditor. assetmodificationprocessor {[initializeonloadmethod] Static void initializeonloadmethod () {// globally monitors whether resources in the project view change (add, delete, and move) editorapplication. projectchanged + = delegate () {debug. log ("change") ;};}// listener "double-click the left mouse button to open the resource" event public static bool isopenforedit (string assetpath, out string message) {message = NULL; debug. logformat ("Assetpath: {0}", assetpath); // true indicates that the resource can be opened. False indicates that the resource cannot be opened in unity and return true ;} // listen to "resource is about to be created" event public static void onwillcreateasset (string path) {debug. logformat ("Path: {0}", PATH) ;}// listener "resource to be saved soon" event public static string [] onwillsaveasset (string [] paths) {If (paths! = NULL) {debug. logformat ("Path: {0}", String. join (",", paths);} return paths;} // listen to "resource to be moved soon" event public static assetmoveresult onwillmoveasset (string oldpath, string newpath) {debug. logformat ("from: {0} to: {1}", oldpath, newpath); // assermoveresult. didmove return assetmoveresult. didmove;} // listen to "resource to be deleted soon" event public static assetdeleteresult onwilldeleteasset (string assetpath, removeassetoptions opint) {debug. logformat ("delete: {0}", assetpath); // assetdeleteresult. didnotdelete indicates that the resource can be deleted. Return assetdeleteresult. didnotdelete ;}}
This class must inherit unityeditor. assetmodificationprocessor, and then rewrite the method for creating, moving, saving, and deleting listening resources to process their own special logic.
Extended Editor (5)