PowerInjectUnity,一个Unity的依赖注入框架说明

最近在找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>}
}


用[Inject]注解来实现注入对象
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 initialization
	void Start () {
		Debug.Log ("three start");
	}
}

using UnityEngine;
using PowerInject;

[Power]
public class Four : MonoBehaviour {

	[Inject]
	Three three;

	// Use this for initialization
	void Start () {
		Debug.Log ("four start");
		three.one.Output ();
	}
	
	// Update is called once per frame
	void Update () {
		three.one.Output ();
	}
}





如果只想使用注入对象而不想被其他对象注入,就使用[Power]注解代替[Insert]注解。

using UnityEngine;
using PowerInject;

[Power]
public class Three : MonoBehaviour {

	[Inject]
	public One one { get; set; }

	// Use this for initialization
	void 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 initialization
	void Start () {
		Debug.Log ("four start");
		three.one.Output ();
	}
	
	// Update is called once per frame
	void 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 ();
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值