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

前端之家收集整理的这篇文章主要介绍了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]的对象。



插件安装

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

@H_502_73@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 ();
	}
}

原文链接:https://www.f2er.com/javaschema/283689.html

猜你在找的设计模式相关文章