Unity StrangeIoC HelloWorld
unity有一个叫StrangeIoC的框架插件,这里写了一个使用StrangeIoC的HelloWorld,比他自带的demo更为简单,方便理解
1.插件下载,在Asset Store直接搜索StrangeIoC
2.在Asset文件夹中,新建一个文件夹Game_D,用来放我们编写的脚本,在Game_D文件下创建controller文件夹,用来存放控制脚本,在controller新建一个脚本DGameCommand,继承自EventCommand
using strange.extensions.command.impl;
using strange.extensions.context.api;
using UnityEngine; public class DGameCommand : EventCommand { public override void Execute()
{
Debug.Log("Start DGame");
}
}
3.在Game_D文件夹下创建一个脚本DGameContext继承自MVCSContext,主要用接口绑定、命令绑定、view绑定
using UnityEngine;
using System.Collections;
using strange.extensions.context.impl;
using strange.extensions.command.impl;
using strange.extensions.context.api; public class DGameContext : MVCSContext {
public DGameContext(MonoBehaviour view)
: base(view)
{
} protected override void mapBindings()
{
base.mapBindings();
commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once();
}
}
4.在Game_D文件夹下创建一个脚本DGameRoot继承自ContextView,这里就是框架程序的入口,把DGameRoot拖在unity场景的一个物体上,运行即可执行,其实这里就是最小的demo,但是后面还是会写到接口的实现调用,view的实现调用
using UnityEngine;
using System.Collections;
using strange.extensions.context.impl; public class DGameRoot : ContextView { void Awake()
{
context = new DGameContext(this);
}
}
5.在Game_D文件夹下创建一个文件夹interface,并在interface文件夹下新建一个接口IDoSomething,再新建一个接口实现类DDoSomething,接口实现可用于一些manager脚本
接口:
public interface IDoSomething {
void DoSomeFunction();
}
接口实现:
using UnityEngine;
using System.Collections; public class DDoSomething : IDoSomething {
public void DoSomeFunction()
{
Debug.Log("interface do something function");
}
}
6.在controller文件夹下新建一个脚本DDoSomethingCommand,用来执行实现的接口功能
using UnityEngine;
using System.Collections;
using strange.extensions.command.impl; public class DDoSomethingCommand : EventCommand {
[Inject]
public IDoSomething ds { get; set; }
public override void Execute()
{
//执行其脚本
ds.DoSomeFunction();
}
}
7.这里使用Event,所以需要在controller文件夹下新建一个脚本DGameEvent
public enum DGameEvent {
DoSomething,
}
8.打开DGameContext脚本,添加以下内容
using UnityEngine;
using System.Collections;
using strange.extensions.context.impl;
using strange.extensions.command.impl;
using strange.extensions.context.api; public class DGameContext : MVCSContext {
public DGameContext(MonoBehaviour view)
: base(view)
{
} protected override void mapBindings()
{
base.mapBindings();
injectionBinder.Bind<IDoSomething>().To<DDoSomething>().ToSingleton();
commandBinder.Bind(DGameEvent.DoSomething).To<DDoSomethingCommand>();
commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once();
}
}
9.打开DGameCommand脚本,添加以下内容,这里就是接口函数的调用,可以再度运行调试
using strange.extensions.command.impl;
using strange.extensions.context.api;
using UnityEngine; public class DGameCommand : EventCommand { public override void Execute()
{
Debug.Log("Start DGame");
dispatcher.Dispatch(DGameEvent.DoSomething);
}
}
10.在Game_D文件下新建一个文件夹view文件夹,并在view文件夹下新建一个脚本DButtonView继承自EventView
using UnityEngine;
using System.Collections;
using strange.extensions.mediation.impl;
public class DButtonView : EventView {
internal const string CLICK_BUTTON = "CLICK_BUTTON";
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 30), "Click"))
{
dispatcher.Dispatch(CLICK_BUTTON);
}
}
}
11.在view文件夹下新建一个脚本DButtonMediator,继承自EventMediator
using UnityEngine;
using System.Collections;
using strange.extensions.mediation.impl; public class DButtonMediator :EventMediator{
[Inject]
public DButtonView btnView{get;set;} public override void OnRegister()
{
base.OnRegister();
btnView.dispatcher.AddListener(DButtonView.CLICK_BUTTON, OnBtnViewClick);
}
void OnBtnViewClick()
{
Debug.Log("click view button");
}
}
12.再改一次DGameContext脚本:
using UnityEngine;
using System.Collections;
using strange.extensions.context.impl;
using strange.extensions.command.impl;
using strange.extensions.context.api; public class DGameContext : MVCSContext {
public DGameContext(MonoBehaviour view)
: base(view)
{
} protected override void mapBindings()
{
base.mapBindings();
injectionBinder.Bind<IDoSomething>().To<DDoSomething>().ToSingleton(); mediationBinder.Bind<DButtonView>().To<DButtonMediator>();
commandBinder.Bind(DGameEvent.DoSomething).To<DDoSomethingCommand>();
commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once();
}
}
13.最后再在场景中新建一个物体,将DButtonView拖上去,然后运行
14.最后都建议,自己手动写一遍代码,方便理解
Unity StrangeIoC HelloWorld的更多相关文章
- Unity StrangeIoC框架
Unity StrangeIoC框架 http://blog.csdn.net/y1196645376/article/details/52746251
- unity StrangeIoc
已经很久没有写博客,主要原因还是自我荒废了太久,在学习上失去了动力.最近来新的公司实习,以前都是做项目的开发,现在被调到框架组,主要从事的是框架维护还有开发.学习了许多新的知识还有优秀的框架,今天就写 ...
- Unity StrangeIoc框架 (一)
最近想项目中需要使用这个架构 因此 上网看了很多资料摸索 但是对于初学者来说大多数的资料不是那么容易理解 而且文档也是英文的阅读起来有点吃力 所以记录一下自己阅读的过程 方便以后翻阅和跟我一 ...
- 【拥抱元宇宙】创建你的第一个Unity程序HelloWorld,并发布
第一个Unity程序--Hello World. 1.需要先下载一个Unity Hub,以及安装Unity编辑器.Unity Hub需要登陆,激活码可以选择个人用户,免费的.免费的无法改变启动画面,其 ...
- Unity StrangeIoc框架 (二)
MVCSContex :the big picture 1.应用程序的入口是一个类成为ContextView,这是一个Monobehavior实例化MVCSContext 2.用MVCSContext ...
- Unity StrangeIoc框架 (三)signal信号方式
先创建TestRoot using UnityEngine; using System.Collections; using strange.extensions.context.impl; publ ...
- Bootstrapper.cs
using System.Windows; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Un ...
- 0 quickstart
说明 使用的安装包有: Prism 6.3 Unity 4.0.1 基于Prism框架的应用程序都包含一个主项目和若干功能模块,主项目负责启动时初始化工作,包括依赖注入容器,定义Shell等等.功能模 ...
- Unity MVC框架 StrangeIoC
StrangeIoC是一个超轻量级和高度可扩展的控制反转(IoC)框架,专门为C#和Unity编写. 项目地址:https://github.com/strangeioc/strangeioc 文档地 ...
随机推荐
- Linux 查看CPU温度
安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...
- Python中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格)lstrip:用来去除开头字符.空白符 ...
- iOS控制反转(IoC)与依赖注入(DI)的实现
背景 最近接触了一段时间的SpringMVC,对其控制反转(IoC)和依赖注入(DI)印象深刻,此后便一直在思考如何使用OC语言较好的实现这两个功能.Java语言自带的注解特性为IoC和DI带来了极大 ...
- Programming In Scala笔记-第五章、Scala中的变量类型和操作
这一章的一些基础性的东西,主要包括Scala中的基本变量类型,以及相关的一些操作符. 一.简单类型 下表中列出Scala语言中的基本类型,以及其字节长度,其中Byte, Short, Int, Lon ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- VMware中的桥接模式、NAT(网络地址转换模式)、Host-only(主机模式):转自:http://blog.chinaunix.net/uid-11798538-id-3061551.html
其中VMnet1是虚拟机Host-only模式的网络接口,VMnet8是NAT模式的网络接口,这些后面会详细介绍.在个虚拟交换机,分别是-个虚拟机交换机,而在VMware Workstation 5以 ...
- 第三方开源动画库EasyAnimation中一个小bug的修复
看过iOS动画之旅的都知道,其中在最后提到一个作者写的开源动画库EasyAnimation(以下简称EA). EA对CoreAnimation中的view和layer动画做了更高层次的包装和抽象,使得 ...
- 一张图带你看懂SpriteKit中Update Loop究竟做了神马!
1首先Scene中只有开始一点时间用来回调其中的update方法 ;] 2然后是Scene中所有动作的模拟 3接下来是上一步完成之后,给你一个机会执行一些代码 4然后是Scene模拟其中的物理世界 5 ...
- Eclipse打jar包,资源文件的读取
最近的工作中需要将java程序打一个jar包,然后在Linux中供调用.程序中需要读取一个配置文件.遇到了三个问题.第一个是依赖的第三方Jar包打成Jar包后找不到:第二个问题是资源文件所在的文件夹打 ...
- 向Github提交代码时遇到的一些问题
今天分享一下我的一些小经验,那就是向github提交我们的代码.之前一直是直接使用的浏览器完成的代码的下载任务,没有使用过客户端,为了让自己在工作之前熟练使用GitHub,所以就有了下面的这篇博文了. ...