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. jsp&servlet——session监听

    session监听,需要实现HttpSessionAttributeListener接口 attributeAdded:监听添加session attributeRemoved:监听删除session ...

  2. python 循环和file操作实现用户密码输错三次将用户锁定

    一.需求编写登录接口1.输入用户名密码2.认证成功后显示欢迎信息3.输错三次后锁定 二.简单思路登录,三次密码输入错误锁定用户1.用户信息文件:存放用户名和密码2.黑名单文件:将输入三次错误的用户加入 ...

  3. solr界面

    1.1 界面功能介绍 1.1.1 Analysis

  4. ios开发之xcode环境介绍

    作为一个刚入门ios开发的人来说,对于ios开发,对于xcode一切都是那么的陌生,那么我们如何开始我们的第一步呢?首先对开发的ide是必须要了解的,其实要对开发的语言要慢慢熟悉起来,今天我们先来熟悉 ...

  5. 改进版getpass库

    编程伊始 正式实施 改进版 源码 以数字显示 以自定义分隔符delimiter显示 如何使用 下载及安装 在您的代码中使用 源码下载 总结 用过Linux的都知道,尤其是进行使用包管理软件类似于apt ...

  6. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  7. collection 中对类排序

    首先 写出 一个person类 让他继承Comparable 构造函数和get/set不用说 我们要覆盖父类中的comparto方法 代码如下 省略get/set package a; public ...

  8. 找不到BufferedImage这个Class的解决方法

    找不到BufferedImage这个Class的解决方法 环境: [1]RedHat AS5 64位      [2]WebSphere6.0 32位版本 正文:    发现原来在RedHat AS4 ...

  9. JAVA面向对象-----this的概述

    this关键字代表是对象的引用.也就是this在指向一个对象,所指向的对象就是调用该函数的对象引用. 1:没有this会出现什么问题 1:定义Person类 1:有姓名年龄成员变量,有说话的方法. 2 ...

  10. 集合框架之Collections静态工具类

    Collections类提供了一些列静态的方法,用以更方便地操作集合类 排序机制 一个List可以通过下面的方法进行排序: Collections.sort(list); 如果List包含的是字符串, ...