Get Start StrangeIOC for Unity3D
好久没有发blog了,因为只发原创内容,而去年发布的那几篇后来发现随便百度到处都是转载的或者各种网站自动扒的,我觉得既然大家都不尊重这种东西就没必要发上来了!不过由于工作原因最近在看Unity的一个IOC框架:StrangeIOC,官方的文档都不是很好理解,找到了一篇比较好的GetStart文章,顺手翻译一下,一来方便自己加深理解,二来还是想共享出来,没事,随意转吧,拜托注明下出处!原文在这里(不太清楚有没有被墙)
译文:
Strange是一个Unity3D中用于控制反转的第三方框架,控制反转(IOC-Inversion of Control)思想是类间解耦的一个重要方法,对于我来说,任何解耦技术都值得去学习。什么是IOC?这里有详细解答。IOC框架已经在企业级开发和其他非游戏软件的开发中成为了主流,并且可以说已经非常成熟。我觉得它可以帮助游戏开发变得更加容易测试,更好的进行协作开发。我非常想尝试它看看到底可以在游戏开发过程中起到多大的帮助程度。
- 在阅读本篇文章之前,最好先去上面提到的官方说明页面了解一下Strange框架的架构(看看它的每个部分的功能以及怎么整合到一块工作的)。
- 这篇文档使用的是signal(消息)而非event(事件)(因为相比event我更喜欢signal)
- 我不会把文档中的Unity项目提供出来,因为我希望大家自己动手去做,这样肯定会学到更多:)
- 这个Hello World示例只是简单的提供注入绑定(injection binding)、命令绑定(command binding)、调解绑定(mediation binding)的示例。
Signal
Assets
StrangeIoC
scripts
在Assets文件夹下创建"Game"文件夹,即用来创建Hello World示例的文件夹。文件夹的的结构应该是这样的:
Assets
Game
Scenes
Scripts
在Scripts文件夹下新建名为HelloWorldSignals.cs的c#脚本,这个类将包含所有用到的signal,让我们coding起来:
using System;
using strange.extensions.signal.impl;
namespace Game {
public class StartSignal : Signal {}
}
在Strange中,这个signal的概念非常像观察者模式(observer pattern)中的事件(events)。在这里,它以命名类的方式实现了继承Strange的Signal类.别急,我们马上会看到怎么去使用它。
using System; using UnityEngine; using strange.extensions.context.impl;
using strange.extensions.command.api;
using strange.extensions.command.impl;
using strange.extensions.signal.impl; namespace Game {
public class SignalContext : MVCSContext { /**
* Constructor
*/
public SignalContext (MonoBehaviour contextView) : base(contextView) {
} protected override void addCoreComponents() {
base.addCoreComponents(); // bind signal command binder
injectionBinder.Unbind<ICommandBinder>();
injectionBinder.Bind<ICommandBinder>().To<SignalCommandBinder>().ToSingleton();
} public override void Launch() {
base.Launch();
Signal startSignal = injectionBinder.GetInstance<StartSignal>();
startSignal.Dispatch();
} }
}
在"Scripts"文件夹下创建一个新文件夹"Controller",到这里有了一点MVC模式的特征。Strange作者建议我们应该以指令类(Command Class)的形式实现各个Controller接口,这个文件夹将包含所有的Command类,现在我们创建一个在StartSignal指令调用时执行的指令。在Controller文件夹下创建名为HelloWorldStartCommand.cs的类:
using System; using UnityEngine; using strange.extensions.context.api;
using strange.extensions.command.impl; namespace Game {
public class HelloWorldStartCommand : Command { public override void Execute() {
// perform all game start setup here
Debug.Log("Hello World");
} }
}
using System;
using UnityEngine;
using strange.extensions.context.impl;
namespace Game {
public class HelloWorldContext : SignalContext {
/**
* Constructor
*/
public HelloWorldContext(MonoBehaviour contextView) : base(contextView) {
}
protected override void mapBindings() {
base.mapBindings();
// we bind a command to StartSignal since it is invoked by SignalContext (the parent class) on Launch()
commandBinder.Bind<StartSignal>().To<HelloWorldStartCommand>().Once();
}
}
}
在这里,我们把StartSignal类绑定(bind)给了HelloWorldStartCommand类。这样在StartSignal的实例被调用时,HelloWorldStartCommand会进行实例化(instantiated)和执行(executed),注意在我们的示例中StartSignal信号会在SignalContext.Launch()方法中调用发出。
using System;
using UnityEngine;
using strange.extensions.context.impl;
namespace Game {
public class HelloWorldBootstrap : ContextView {
void Awake() {
this.context = new HelloWorldContext(this);
}
}
}

namespace Game {
public interface ISomeManager {
/**
* Perform some management
*/
void DoManagement();
}
}
这就是我们示例当中的manager接口,注意:Strange的作者建议我们总是使用一个接口然后通过injectionBinder将它映射到一个真正的实现类,当然,你也可以使用多对多的映射。接下来我们创建一个具体实现类,在Scripts文件夹下创建ManagerAsNormalClass.cs脚本:
using System;
using UnityEngine;
namespace Game {
public class ManagerAsNormalClass : ISomeManager {
public ManagerAsNormalClass() {
}
#region ISomeManager implementation
public void DoManagement() {
Debug.Log("Manager implemented as a normal class");
}
#endregion
}
}
如果你仔细在看你可能会发现这是一个没有MonoBehaviour的manager,别急,一会再介绍怎么bind有MonoBehaviour的
现在我们来创建一个简单的交互场景,效果是当一个Button按下时,ISomeManager的DoManagement函数执行,这里我们有一个要求:用MVC思想---对controll层(ISomeManager)和view层(控制Button触发事件的脚本)完全解耦,view层只需要通知controll层:"hey!button被点击了",至于接下来发生什么交由controll层进行逻辑处理。
现在缺一个view层,把它创建出来吧---在Game文件夹下创建"View"文件夹,创建HelloWorldView.cs脚本:
using System; using UnityEngine; using strange.extensions.mediation.impl;
using strange.extensions.signal.impl; namespace Game {
public class HelloWorldView : View { public Signal buttonClicked = new Signal(); private Rect buttonRect = new Rect(, , , ); public void OnGUI() {
if(GUI.Button(buttonRect, "Manage")) {
buttonClicked.Dispatch();
}
} }
}
这里继承的Strange框架中的View类已经包含了MonoBehaviour。所有使用Strange context的View层类都必须继承这个Strange的View类,我们刚刚创建的View类只有一个交互功能:在点击名为"Manage"的Button后,调用一个 generic signal(通用信号) 。
Strange作者建议对每个View创建对应的Mediator。Mediator是一个薄层,他的作用是让与之对应的View和整个程序进行交互。mediation binder的作用是把View映射到它对应的mediator上。所以接下来为View层创建对应的mediator---在"view"文件夹下创建HelloWorldMediator.cs脚本:
using System;
using UnityEngine;
using strange.extensions.mediation.impl;
namespace Game {
public class HelloWorldMediator : Mediator {
[Inject]
public HelloWorldView view {get; set;}
[Inject]
public ISomeManager manager {get; set;}
public override void OnRegister() {
view.buttonClicked.AddListener(delegate() {
manager.DoManagement();
});
}
}
}
在这段代码里我们可以看到神奇的"Inject"标注(Inject attribute)。这个"Inject"标注只能和变量搭配使用,当一个变量上面有"Inject"标注时,意味着Strange会把这个变量的一个实例自动注入到它对应映射的context中。据此从我们上面的代码来分析,在这里我们获取到了"view"和"manager"的实例,并且不用去关心这些个实例是怎么来的。
OnRegister()是一个可以被重写的方法,它用来标记实例注入完成已经可以使用了,它的意义主要是进行初始化,或者说做准备。在上面的类中,OnRegister方法中为HellowWorldView.buttonClicked signal添加了一个监听器,这个监听器的逻辑是按下就执行manager.DoManagement方法。
protected override void mapBindings() {
base.mapBindings();
// we bind a command to StartSignal since it is invoked by SignalContext (the parent class) during on Launch()
commandBinder.Bind<StartSignal>().To<HelloWorldStartCommand>().Once();
// bind our view to its mediator
mediationBinder.Bind<HelloWorldView>().To<HelloWorldMediator>();
// bind our interface to a concrete implementation
injectionBinder.Bind<ISomeManager>().To<ManagerAsNormalClass>().ToSingleton();
}


using System;
using UnityEngine;
namespace Game {
public class ManagerAsMonoBehaviour : MonoBehaviour, ISomeManager {
#region ISomeManager implementation
public void DoManagement() {
Debug.Log("Manager implemented as MonoBehaviour");
}
#endregion
}
}
在HelloStrangeScene中,创建一个新的GameObject名为"Manager",add 上面创建好的 ManagerAsMonobehaviour脚本
protected override void mapBindings() {
base.mapBindings();
// we bind a command to StartSignal since it is invoked by SignalContext (the parent class) during on Launch()
commandBinder.Bind<StartSignal>().To<HelloWorldStartCommand>().Once();
// bind our view to its mediator
mediationBinder.Bind<HelloWorldView>().To<HelloWorldMediator>();
// REMOVED!!!
//injectionBinder.Bind<ISomeManager>().To<ManagerAsNormalClass>().ToSingleton();
// bind the manager implemented as a MonoBehaviour
ManagerAsMonoBehaviour manager = GameObject.Find("Manager").GetComponent<ManagerAsMonoBehaviour>();
injectionBinder.Bind<ISomeManager>().ToValue(manager);
}
与把ISomeManager映射为一个类型相反,我们把这个ManagerAsMonobehaviour映射为一个实例值(instance value)。

using System;
using strange.extensions.signal.impl;
namespace Game {
public class StartSignal : Signal {}
public class DoManagementSignal : Signal {} // A new signal!
}
我们创建command映射到signal:在Controller文件夹下创建一个脚本DoManagementCommand.cs
using System; using UnityEngine; using strange.extensions.context.api;
using strange.extensions.command.impl; namespace Game {
public class DoManagementCommand : Command { [Inject]
public ISomeManager manager {get; set;} public override void Execute() {
manager.DoManagement();
} }
}
在这个类,我们把ISomeManager注入到command类,并且在Execute方法中让它的DoManagement方法执行。
using System;
using UnityEngine;
using strange.extensions.mediation.impl;
namespace Game {
public class HelloWorldMediator : Mediator {
[Inject]
public HelloWorldView view {get; set;}
[Inject]
public DoManagementSignal doManagement {get; set;}
public override void OnRegister() {
view.buttonClicked.AddListener(doManagement.Dispatch);
}
}
}
现在我们的mediator类中已经没有任何对ISomeManager接口的调用了。取而代之的是要在mediator类获取到DoManagementSignal的实例,当button点击时,这个类会发出DoManagementSignal。mediator层不需要知道任何manager的事情,它只管发送信号(signal)出去。
protected override void mapBindings() {
base.mapBindings();
// we bind a command to StartSignal since it is invoked by SignalContext (the parent class) during on Launch()
commandBinder.Bind<StartSignal>().To<HelloWorldStartCommand>().Once();
commandBinder.Bind<DoManagementSignal>().To<DoManagementCommand>().Pooled(); // THIS IS THE NEW MAPPING!!!
// bind our view to its mediator
mediationBinder.Bind<HelloWorldView>().To<HelloWorldMediator>();
// bind the manager implemented as a MonoBehaviour
ManagerAsMonoBehaviour manager = GameObject.Find("Manager").GetComponent<ManagerAsMonoBehaviour>();
injectionBinder.Bind<ISomeManager>().ToValue(manager);
}
运行场景,效果和之前一样,但是我们在代码层面把这块代码重构了。
Get Start StrangeIOC for Unity3D的更多相关文章
- 框架学习笔记:Unity3D的MVC框架——StrangeIoC
作为从AS3页游走过来的人,看见StrangeIoC会额外亲切,因为StrangeIoC的设计和RobotLegs几乎一致,作为一款依赖注入/控制反转(IoC)的MVC框架,StrangeIoC除了使 ...
- 关于StrangeIOC框架
在Unity上进行开发,请先看对其开发模式应用的讨论: http://www.reddit.com/r/Unity3D/comments/1nb06h/unity_design_patterns_an ...
- Unity3D手游开发实践
<腾讯桌球:客户端总结> 本次分享总结,起源于腾讯桌球项目,但是不仅仅限于项目本身.虽然基于Unity3D,很多东西同样适用于Cocos.本文从以下10大点进行阐述: 架构设计 原生插件/ ...
- StrangeIoc框架学习
StrangeIoc是一款基于MVCS的一种框架,是对MVC思想的扩展,是专门针对Unity3D开发的一款框架,非常好用. 一.MVCS分别代表什么 MVCS框架是一种模块的分离,一种写代码的规则,目 ...
- (转)Unity3D手游开发实践
作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). (转)& ...
- Unity3d学习 预设体(prefab)的一些理解
之前一直在想如果要在Unity3d上创建很多个具有相同结构的对象,是如何做的,后来查了相关资料发现预设体可以解决这个问题! 预设体的概念: 组件的集合体 , 预制物体可以实例化成游戏对象. 创建预设体 ...
- Unity3d入门 - 关于unity工具的熟悉
上周由于工作内容较多,花在unity上学习的时间不多,但总归还是学习了一些东西,内容如下: .1 根据相关的教程在mac上安装了unity. .2 学习了unity的主要的工具分布和对应工具的相关的功 ...
- TDD在Unity3D游戏项目开发中的实践
0x00 前言 关于TDD测试驱动开发的文章已经有很多了,但是在游戏开发尤其是使用Unity3D开发游戏时,却听不到特别多关于TDD的声音.那么本文就来简单聊一聊TDD如何在U3D项目中使用以及如何使 ...
- warensoft unity3d 更新说明
warensoft unity3d 组件的Alpha版本已经发布了将近一年,很多网友发送了改进的Email,感谢大家的支持. Warensoft Unity3D组件将继续更新,将改进的功能如下: 1. ...
随机推荐
- 【android】Android检查是否已经连接到网络
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE); boolea ...
- python产生随机名字
用到random.choice(序列) 在一个序列中随机选取一个值 import random as r a1=['张','金','李','王','赵'] a2=['玉','明','龙','芳','军 ...
- EF查询
public ActionResult AllSettings(DataSourceRequest command, Framework.Kendoui.Filter filter = null, S ...
- Linux怎样修改系统时间
修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...
- 重启电脑提示Error:no such partition grub rescue
我的系统是Win7,在使用Ubuntu12.04自带的Wubi.exe安装双系统时,系统提示重新启动计算机,重启后电脑就停留在了黑屏界面并提示: error:no such partition gru ...
- 使用FastReport的UserDataSet时候,遇到TfrxMemoView内容过多而打印不全的问题
解决方案很简单,就是把Memo所在的Band勾选Stretch就行了.另外还可勾选StartNewPage. 至于UserDataSet本身,猜测就是人为的构造一个类似数据库的集合,大致使用代码如下: ...
- (转载):() { :|:& }; : # <-- 打开终端,输入这个,回车.你看到了什么??
代码::() { :|:& }; : 为什么这个东西会让你的系统死掉???有人执行了然后问我 让我们来分析一下这段代码,我改一下格式,但内容是一样的 代码::() # 定义一个叫“:”的过程 ...
- 2015第15周六Java线程池
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 比较重要的几个类: Ex ...
- CF 579A Raising Bacteria
题意:细菌爱好者在盒子里面培养细菌.最初盒子是空的,每天可以往盒子里面添加任意数目的细菌,而且每天每个细菌会分裂成两个.在某一时刻要想获得某一数量的细菌,要求最初放置的最少的细菌数目. 思路: 求出 ...
- Lowest Common Ancestor of a Binary Search Tree 解答
Question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes ...