有没有一个很好的方法来使用Unity的事件?

编程入门 行业动态 更新时间:2024-10-15 20:21:34
本文介绍了有没有一个很好的方法来使用Unity的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 programAction = UnityContainer.Resolve<LoaderDriver>(); (programAction as LoaderDriver).LoadComplete += new EventHandler(Program_LoadComplete);

有没有一个配置让我解决已经连接到事件的对象?

Is there a configuration that let's me resolve my objects having already been wired to an event?

或者,是否有一个首选的方法来获得相同的结果?我注意到,有时候我看不到功能,因为我不了解的模式是首选。

Alternately, is there a preferred way to achive the same result? I've noticed that sometimes when I don't see a "feature" its because a pattern I don't know about is preferred.

推荐答案

是的,有一种方法。您将必须编写一个扩展,将自定义BuilderStrategy添加到Unity BuildPipeline的PostInitialization阶段。

Yes there is a way. You would have to write an extension that adds a custom BuilderStrategy to the PostInitialization stage of the Unity BuildPipeline.

扩展和策略的代码应类似于以下内容: p>

The code for extension and strategy should look similar to this:

public class SubscriptionExtension : UnityContainerExtension { protected override void Initialize() { var strategy = new SubscriptionStrategy(); Context.Strategies.Add(strategy, UnityBuildStage.PostInitialization); } } public class SubscriptionStrategy : BuilderStrategy { public override void PostBuildUp(IBuilderContext context) { if (context.Existing != null) { LoaderDriver ld = context.Existing as LoaderDriver; if(ld != null) { ld.LoadComplete += Program_LoadComplete; } } } }

然后您将扩展名添加到容器

Then you add the extension to the container

container.AddNewExtension<SubscriptionExtension>();

当您解析LoaderDriver实例时,它将自动订阅EventHandler。

And when you resolve your LoaderDriver instance it will automatically subscribe to the EventHandler.

您可以在 TecX 项目中找到一个将类订阅到EventAggregator的工作示例。源代码位于TecX.Event.Unity 项目中。

You can find a working sample that subscribes classes to an EventAggregator in the TecX project. The source code is located in the TecX.Event.Unity project.

更多推荐

有没有一个很好的方法来使用Unity的事件?

本文发布于:2023-11-23 18:52:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622513.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很好   方法来   事件   Unity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!