11.Unity Zenject高级编程(DiContainer方法集)


前言

在正常的绑定基础之上,也有一些其他的方法你需要联系到一块使用DiContainer;
例如你自定义一个工厂,你有可能直接调用DiContainer.Instantiate方法来实例化对象或者其他类库(比如网络库),
你需要调用DiContainer.Inject手动注入
DiContainer总是会添加自己到类里面,你可以在任何类里面调用到它;

DiContainer.Instantiate

这个实例化的方法在自定义工厂里面非常有作用,在多数情况下,
你可能选择普通的工厂来替代不需要直接引用DiContainer;
当你直接实例化对象,你可以使用DiContainer或者使用IInstantiator,都是DiContainer的内嵌;
IInstantiator存在是因为使用频率高,在自定义工厂里面,你只要关心实例化的操作,
不需要Bind、Resolve,etc;

  1. Instantiate< T> - 使用new操作来创建指定的类并且注入相关的信息;
    注意这个方法不能用在Components/MonoBehaviours上;
    然而,这个方法可以使用到ScriptableObject的派生类,代码示例:
Foo foo = Container.Instantiate<Foo>();

当你需要传递额外参数的时候

Foo foo = Container.Instantiate<Foo>(new object[] {
    "foo", 5 });

等同操作方式

Foo foo = (Foo)Container.Instantiate(typeof(Foo));
Foo foo = (Foo)Container.Instantiate(typeof(Foo), new object[] {
    "foo", 5 });
  1. InstantiatePrefab - 实例化指定的prefab并且注入上面所有的Mono脚本
GameObject gameObject = Container.InstantiatePrefab(MyPrefab);

等同代码

var gameObject = GameObject.Instantiate(MyPrefab)
DiContainer.Inject(gameObject)

MyPrefab可以是一个GameObject,或者从GameObject直接引用的一个组件,类似于GameObject.Instantiate

GameObject gameObject = Container.InstantiatePrefab(MyPrefab, MyParentTransform);
  1. InstantiatePrefabResource - 和InstantiatePrefab相同,可以通过Resources文件夹来索引
GameObject gameObject = Container.InstantiatePrefabResource("path/to/myprefab");

等同代码

Container.InstantiatePrefab(Resources.Load("path/to/myprefab"));
  1. InstantiatePrefabForComponent< T> - 实例化指定的Prefab,注入相关信息,返回prefab上拥有的指定类型的组件
var foo = Container.InstantiatePrefabForComponent<Foo>(FooPrefab)

不像InstantiatePrefab方法,这个方法允许传递参数

var foo = Container.InstantiatePrefabForComponent<Foo>(FooPrefab, 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DIContainers is a collection of more than 100 individual container classes for Delphi (Embarcadero, CodeGear, Borland). Four different container structures are available: Hash containers Doubly-linked lists N-ary trees Vector containers DIContainers is designed with easy customization in mind. Unlike other libraries, it strictly separates a containers' data and structure: Data describes an individual item in the container, for example an Integer number. The data layer is mostly responsible to manage the item's memory, but also for copying, comparing, and streaming of items. Data items in DIContainers are made up of memory efficient Pascal records. Special item handlers take care to initialize and finalize items automatically as required. Structure describes the arrangement of items within the container, like linked list, linear vector, etc. The structure determines how quickly items can be added, manipulated and retrieved from the container. Using different item handlers, the same structure can provide for quite different containers (see hierarchy on the right). To create a new container for some type of data, it is often sufficient to reuse an already existing item handler or to create a new item handler for the new type of data. Type. On top of the general container classes, there are many ready-made containers which interface typed access to their items like strings (WideStrings and AnsiStrings), different Number types, Objects, Pointer, and various combinations of the above. More than 100 of these containers are ready to use straight out of the box. Advanced container operations include cross-container assignment (i.e. from lists to vectors) and cascading streaming. The graphic to the right shows the class hierarchy of containers contained in DIContainers. Bold font marks important classes like item handlers and structure containers. Their descendant classes in regular font provide typed access to their items. The graphic was automatically created from the DIContainers library by one of the demo applications.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值