本文介绍了为什么选择UnityEvent而非本机C#事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,UnityEvents比本机C#事件慢,并且它们仍然存储着对接收者的强烈引用.因此,我发现在本机C#事件上使用UnityEvent的唯一有效理由是它们与编辑器的集成. 我可以俯瞰某些地方吗?

I mean, UnityEvents are slower than the native C# events and they still store a strong reference to the receivers. So, the only valid reason I can find to use UnityEvents over native C# events is their integration with the editor. Am I overlooking something?

推荐答案

不,您没有忽略任何内容.使用UnityEvent的唯一优点和原因是它允许您在编辑器中使用事件.这是供拖放人员或制作Editor插件的人员使用的.

Nope, you are not overlooking anything. The only advantage and reason to use UnityEvent is that it allows you to use events in the Editor. That's for drag and drop people or those making Editor plugins.

UnityEvent的另一个优点是,它可以防止由于滥用委托或将匿名委托与Unity Objects一起使用而导致Unity Object无法释放的问题.尽管当持有它们的主脚本被破坏时它们被释放了.这样做的原因是因为UnityEvent是用弱引用实现的,因此可以消除/最小化此问题.在本地C#事件上使用UnityEvent仍然不值得这两个事情.

Another advantage of UnityEvent is that it prevents the problem of Unity Object not being freed due to the misuse of delegates or using anonymous delegates with Unity Objects. Although they get freed when the main script that's holding them is destroyed. The reason for this is because UnityEvent is implemented with weak references therefore removing/minimizing this problem. These two things are still not worth it to use UnityEvent over native C# events.

如果您不制作Editor插件,则应始终使用本机事件并通过UnityEvent进行委托,这是因为该插件性能好且占用的内存少.参见帖子以获取更多信息.

You should always use native event and delegate over UnityEvent if you are not making an Editor plugin because of its fast performance and small memory usage. See this and this post post for more information.

这篇关于为什么选择UnityEvent而非本机C#事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:09