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