加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:10:22 所属栏目:百科 来源:网络整理
导读:最近在找Unity上使用的依赖注入框架,然后找到了一个叫PowerInjectUnity的插件,看下来,还行。 PowerInjectUnity在Unity商场里有,也将源码托管在了GitHub。使用说明也在Github。 https://github.com/mlp1802/PowerInjectUnity 使用简单总结 1、根节点必须

最近在找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]的对象。



插件安装

将下载下来的内容导入,就可以了,其中还有一个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 ();
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读