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的框架,除了各大公司自己内部使用的,开源并好用的实际并不是很多,我会慢慢挖掘,依次写出自己的一点见解,错误的地方,望各路大神指正。 一、基本概念 控制反转(Inversion of Control,英文缩写为IOC),我的理解就是,原本A类要获取B类的对象,需要你在A类中自己New一个对象,那么是由A来获取并控制B的对象,IOC就是把对象获取的这个过程交给容器和依赖注入来处理,A类并不知道B的对象是哪里来的,对B对象的控制,由自己变成了其他类,官方一点的概念可以百度,这个还是蛮多的。 二、StrangeIOC基础类型 实际要理解一个框架的类型,还是要自己看源码,这里我只说一下几个重要类型的作用,这个看源码的时候有个印象,也方便理解,而且说这部分的帖子也很多,我就不再赘述了。 1.Context 上下文组件定义程序边界,也就是可以把一个程序定义成多上下文,让代码更加模块化 它提供了程序入口,也算是框架中耦合度最高的地方 2.Binder和Binding 这两个类是这个框架最重要的组成部分 Binding存储了对象的绑定关系,而Binder存储了Binding的对象 3.View和Mediator MVCS中的View层,View只用于显示,也就是View只负责管理UI,Mediator负责界面逻辑,事件响应等 4.Model MVCS中的Model层,负责数据部分 5.Command MVCS中的Control层,负责执行逻辑代码 6.Service MVCS中的Service层,负责与第三方交互,这个Service我理解的,并不是一定指代服务器,也可以是其他的软件,什么都可以,它就是我们程序对外的接口 7.Dispatcher 派发器是框架内通信主线的其中一种,用来派发消息,触发命令,从而进一步解耦 8.Signal 信号是框架内另外一种通信主线,它采用强类型,来绑定信号和命令之间的关系,实现消息响应的触发 9.ReflectionBinder 反射部分,通过binding来获取类的信息,存储在ReflectedClass中 10.injector 注入器,通过反射获取的信息,来实例化请求的对象 --------------------- 作者:蓝天小僧 来源:CSDN 原文:https://blog.csdn.net/zcaixzy5211314/article/details/80876228 版权声明:本文为博主原创文章,转载请附上博文链接!
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神码编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值