编辑器扩展,用于生成在属性设置之前执行的事件,例如 Transform-position、Transform-rotation 和 Transform-parent,这使得可以编写 mo
编辑器扩展,用于生成在属性设置之前执行的事件,例如 Transform-position、Transform-rotation 和 Transform-parent,这使得可以编写 mo
If you were ever looking for a way to write more event driven code in Unity you probably found that it's not so easy at least not with some very basic structures like Transforms.
To get it working you probably needed to abstract some parts of API and provide events there which would require buy-in from other devs as well as your own self-discipline to be properly used.
Events directly in property setters come in handy in some situations:
Transform that changes position/rotation/scale and you have no idea whyUnfortunately those are simply not there, if you want to respond to some changes you'll likely going to use Update loop of some game object.
Making This Tool Better - Visual Transform Changes Debugger
I've went further with this concept and created a tool that focuses on tracking ANY changes made to ANY transforms which is then neatly laid out in friendly GUI. Check it out at: https://immersiveVRTools.com/projects/transform-changes-debugger
This tool can add those events to Unity DLLs enabling you to use them as you would any other events. It's very straight forward to use and works globally, which means once you've added them they'll stay there.
Tool uses IL Weaving and will add specified Events directly to Unity DLLs, both for Editor and Builds. These are standard events, nothing different from writing them out by hand. Usage and performance will be in line with what you'd expect from manually created events.
Since DLL IL code is modified you want to check with your Unity License if that's allowed. Tool and docs are provided for educational purposes. Tool can be found at https://github.com/handzlikchris/AddingEventsToCompiled3rdPartyLibrary
Go to Window -> Package Manager and 'Add package from hit URL...'
use url : 'https://github.com/handzlikchris/Unity.MissingUnityEvents.git'
Click on the Missing Unity Events menu bar followed by Run
Pick weaver executable in via Pick next to Plugin Executable Path - it should alredy open in correct folder (if not it'll be located int the Plugin~ folder, eg /MissingUnityEvents/Assets/Plugins/MissingUnityEvents/Editor/Plugins~/EventILWeaver.Console.exe)
Specify types that you want to add events to
PropertyName dropdown will allow you to choose from likely supported propertiesClick Weave Events to DLL
It's best to abstract usage of weaved events. This way if you're using clean dll you won't have to change your existing code, instead it'll just log warning if specified.
Pick next to Helper Class File Path and select the file that'll be used as a target for generated codeNamespace ideally this will be root namespace for your project, with that extensions method will be easily picked upCustom Code When no Build Symbols - this will be injected to method body when there build symbol is not defined (eg. DLLs were reverted and using new fields would result in error)#if ILWeavedEventsOn
public static class BoxColliderExtensions
{
public static void BindSetSizeExecuting(this BoxCollider obj, EventHandler handler)
{
obj.SetSizeExecuting += handler;
}
}
#else
public static class BoxColliderExtensions
{
public static void BindSetSizeExecuting(this BoxCollider obj, EventHandler handler)
{
}
}
#endifGenerate Helper ClassesFrom now on instead of using weaved Events eg.
BoxCollider.SetSizeExecuting += (sender, args) => { };
You can use helper methods that'll not break your code with not modified DLLs
BoxCollider.BindSetSizeExecuting((sender, args) => { });
If you want to use helper classes with fallback code tick Force no build symbol (in editor) - this will turn the build symbol off (effectively turning off your binding). The DLLs still has event fields in them.
If you want to completely remove changes made by plugin click 'Revert DLL to original' this will restore backups.
In some cases changes made to DLLs will cause editor to crash. As example if you try to add event to Object.name. Potentially more types / properties will cause this issue, if that happens there's a rollback script created revert-last-weaving.bat in the same folder as plugin. Running it will revert to backups.
暂无开放 Issues,或尚未同步最近议题。