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的更多相关文章

  1. Unity StrangeIoC框架

    Unity StrangeIoC框架  http://blog.csdn.net/y1196645376/article/details/52746251    

  2. unity StrangeIoc

    已经很久没有写博客,主要原因还是自我荒废了太久,在学习上失去了动力.最近来新的公司实习,以前都是做项目的开发,现在被调到框架组,主要从事的是框架维护还有开发.学习了许多新的知识还有优秀的框架,今天就写 ...

  3. Unity StrangeIoc框架 (一)

    最近想项目中需要使用这个架构  因此 上网看了很多资料摸索   但是对于初学者来说大多数的资料不是那么容易理解 而且文档也是英文的阅读起来有点吃力  所以记录一下自己阅读的过程  方便以后翻阅和跟我一 ...

  4. 【拥抱元宇宙】创建你的第一个Unity程序HelloWorld,并发布

    第一个Unity程序--Hello World. 1.需要先下载一个Unity Hub,以及安装Unity编辑器.Unity Hub需要登陆,激活码可以选择个人用户,免费的.免费的无法改变启动画面,其 ...

  5. Unity StrangeIoc框架 (二)

    MVCSContex :the big picture 1.应用程序的入口是一个类成为ContextView,这是一个Monobehavior实例化MVCSContext 2.用MVCSContext ...

  6. Unity StrangeIoc框架 (三)signal信号方式

    先创建TestRoot using UnityEngine; using System.Collections; using strange.extensions.context.impl; publ ...

  7. Bootstrapper.cs

    using System.Windows; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Un ...

  8. 0 quickstart

    说明 使用的安装包有: Prism 6.3 Unity 4.0.1 基于Prism框架的应用程序都包含一个主项目和若干功能模块,主项目负责启动时初始化工作,包括依赖注入容器,定义Shell等等.功能模 ...

  9. Unity MVC框架 StrangeIoC

    StrangeIoC是一个超轻量级和高度可扩展的控制反转(IoC)框架,专门为C#和Unity编写. 项目地址:https://github.com/strangeioc/strangeioc 文档地 ...

随机推荐

  1. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Red Hat Enterprise Linux7的安装与oracle 12c的安装

    Red Hat Enterprise Linux7的安装与oracle 12c的安装 本文档中用到的所有参数均位于文末附录 Red Hat Enterprise Linux7的安装 新建完虚拟机后,挂 ...

  3. DOS/Windows 文本格式与 Unix 文本各式转换

    命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2 ...

  4. 一小时入门PHP

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:[http://blog.csdn.net/sdksdk0/article/details/52332296](http://blog.csdn ...

  5. [LaTeX] box algorithm adjustbox subfigures, textwidth

    algorithm \begin{figure}[!t] \vspace*{\dimexpr\fboxsep+\fboxrule} \fbox{% \parbox{\dimexpr\linewidth ...

  6. Bootstrap3 代码-内联代码

    通过 <code> 标签包裹内联样式的代码片段. For example, <section> should be wrapped as inline. For example ...

  7. Netty 4源码解析:请求处理

    Netty 4源码解析:请求处理 通过之前<Netty 4源码解析:服务端启动>的分析,我们知道在最前端"扛压力"的是NioEventLoop.run()方法.我们指定 ...

  8. How to work with my desktop and laptop

    introduction Two years ago, I got a laptop Lenovo Y500, I am still using it now. Recently, I bought ...

  9. Swift3的playground中对UI直接测试支持的改变

    我们知道在Xcode的playground中不仅可以测试console代码,还可以测试UI代码,甚至我们可以测试SpriteKit中的场景,有兴趣的童鞋可以看我之前写的这一篇blog: Xcode的p ...

  10. Dynamics CRM 通过Odata创建及更新记录各类型字段的赋值方式

    CRM中通过Odata方式去创建或者更新记录时,各种类型的字段的赋值方式各不相同,这里转载一篇博文很详细的列出了各类型字段赋值方式,以供后期如有遗忘再次查询使用. http://luoyong0201 ...