PowerInjectUnity,一个Unity的依赖注入框架说明
来源:互联网 发布:python 定时提醒 编辑:程序博客网 时间:2024/05/22 12:17
最近在找Unity上使用的依赖注入框架,然后找到了一个叫PowerInjectUnity的插件,看下来,还行。
PowerInjectUnity在Unity商场里有,也将源码托管在了GitHub。使用说明也在Github。
https://github.com/mlp1802/PowerInjectUnity
使用简单总结
1、根节点必须有PowerPipeline组件
2、[Insert]:注解允许类被注入、[Power]:不允许类被注入
3、[Inject]:注入对象、[Produce]:注入方法(这个注解我没看懂)
4、[OnInjected]:使注解的方法在Start()事件后运行。
5、 [NewInstance]:在当前类里新建对象,该对象可以不是[Insert]的对象。
6、instantiate创建出来的注入无效(至少unity5.3里是这样的)
插件安装
将下载下来的内容导入,就可以了,其中还有一个demo
使用
首先,需要一个PowerPipeline的组件作为根节点,所有需要注入或者被注入的对象必须在这个节点下面
任何MonoBehaviour对象,可以用[Insert]注解来标记为可注入的对象
using UnityEngine;using PowerInject;[Insert]public class One : MonoBehaviour {<span style="white-space:pre"></span>// Use this for initialization<span style="white-space:pre"></span>void Start () {<span style="white-space:pre"></span>Debug.Log ("one start.");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void Output(){<span style="white-space:pre"></span>Debug.Log ("one out");<span style="white-space:pre"></span>}}
using UnityEngine;using PowerInject;[Insert]public class Two : MonoBehaviour {<span style="white-space:pre"></span>[Inject]<span style="white-space:pre"></span>public One one;<span style="white-space:pre"></span>void Start () {<span style="white-space:pre"></span>Debug.Log ("two start.");<span style="white-space:pre"></span>one.Output ();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>void Update () {<span style="white-space:pre"></span>one.Output ();<span style="white-space:pre"></span>}}
带有[Insert]标记的MonoBehaviour对象都可以继续注入。
using UnityEngine;using PowerInject;[Insert]public class Three : MonoBehaviour {[Inject]public One one { get; set; }// Use this for initializationvoid Start () {Debug.Log ("three start");}}
using UnityEngine;using PowerInject;[Power]public class Four : MonoBehaviour {[Inject]Three three;// Use this for initializationvoid Start () {Debug.Log ("four start");three.one.Output ();}// Update is called once per framevoid Update () {three.one.Output ();}}
如果只想使用注入对象而不想被其他对象注入,就使用[Power]注解代替[Insert]注解。
using UnityEngine;using PowerInject;[Power]public class Three : MonoBehaviour {[Inject]public One one { get; set; }// Use this for initializationvoid Start () {Debug.Log ("three start");}void Update () {Debug.Log ("three update");one.Output ();}}
using UnityEngine;using PowerInject;[Power]public class Four : MonoBehaviour {[Inject]Three three;// Use this for initializationvoid Start () {Debug.Log ("four start");three.one.Output ();}// Update is called once per framevoid Update () {Debug.Log ("three update");three.one.Output ();}}
因为注入生效是在Start()事件以后,所以,提供了[OnInjected]注解,使一个自定义的函数可以替代Start()。
using UnityEngine;using PowerInject;[Insert]public class Two : MonoBehaviour {[Inject]public One one;void Start () {Debug.Log ("two start.");one.Output ();}[OnInjected]void init(){Debug.Log ("two init");one.Output ();}void Update () {Debug.Log ("two update");one.Output ();}}
0 0
- PowerInjectUnity,一个Unity的依赖注入框架说明
- 一个简单的例子说明java中spring框架的依赖注入
- 一个简单的例子说明java中spring框架的依赖注入
- 一个简单的例子说明java中spring框架的依赖注入
- Spring框架依赖注入的一个简单应用
- Objection 一个轻量级的Objective-C依赖注入框架
- Unity 应用程序块(Unity)是一个轻量级、可扩展的依赖注入容器
- 自己动手写一个依赖注入框架
- 依赖注入容器Unity的基础知识
- 微软的依赖注入容器,Unity
- 一个简单的小程序演示Unity的三种依赖注入方式
- 一个简单的小程序演示Unity的三种依赖注入方式
- 一个简单的小程序演示Unity的三种依赖注入方式
- 一个简单的小程序演示Unity的三种依赖注入方式
- Zenject——轻量级依赖注入框架 for Unity
- unity实现依赖注入
- 微软依赖注入Unity
- C# Unity依赖注入
- STM32 TIM1 做PWM输出问题
- windows下安装python的C扩展编译环境(解决“Unable to find vcvarsall.bat”)
- Caffe学习(八)运行py-faster-RCNN
- 带复杂类的list,前后台交互
- CodeForces 632B Alice, Bob, Two Teams
- PowerInjectUnity,一个Unity的依赖注入框架说明
- HDU Problem 5615 Jam's math problem 【十字交叉,暴力】
- 机器学习(2.1)数据知识积累——向量
- Linux中的ls命令详细使用
- 整理 iOS 9 适配中出现的坑(图文)
- 【HDU】-5671-Matrix(思维,好)
- 手机号正则表达式详解和演示html页面代码 js、html
- HTML Iframe窗口在页面跳转
- poj---1064