// Add a monobehaviour to an existing game object, using Type rather than a generic // Note: gameobject here is not a prefab prototype, it is an instance public Component AddMonobehaviour( Type behaviourType, GameObject gameObject, params object[] args) { Assert.That(behaviourType.DerivesFrom <Component>()); var monoBehaviour = (Component)gameObject.AddComponent(behaviourType); InjectionHelper.InjectMonoBehaviour(_container, monoBehaviour, args); return(monoBehaviour); }
public object Instantiate(Type type, string name) { var gameObj = new GameObject(name); gameObj.transform.parent = _rootTransform; var component = gameObj.AddComponent(type); if (type.DerivesFrom(typeof(Component))) { InjectionHelper.InjectMonoBehaviour(_container, (Component)component); } return(component); }
public override object GetInstance(Type contractType, InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(contractType)); if (_instance == null) { Assert.That(!_container.AllowNullBindings, "Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph()); _instance = _gameObject.AddComponent(_componentType); Assert.That(_instance != null); InjectionHelper.InjectMonoBehaviour(_container, _instance); } return(_instance); }
// Create from prefab // Return specific monobehaviour public T Instantiate <T>( GameObject template, params object[] args) where T : Component { Assert.That(template != null, "Null template found when instantiating game object"); var gameObj = (GameObject)GameObject.Instantiate(template); // By default parent to comp root // This is good so that the entire object graph is // contained underneath it, which is useful for cases // where you need to delete the entire object graph gameObj.transform.parent = _rootTransform; gameObj.SetActive(true); T requestedScript = null; foreach (var component in gameObj.GetComponentsInChildren <Component>()) { var extraArgs = Enumerable.Empty <object>(); if (component.GetType() == typeof(T)) { Assert.IsNull(requestedScript, "Found multiple matches with type '{0}' when instantiating new game object", typeof(T)); requestedScript = (T)component; extraArgs = args; } InjectionHelper.InjectMonoBehaviour(_container, component, extraArgs); } if (requestedScript == null) { throw new ZenjectResolveException( "Could not find component with type '{0}' when instantiating new game object".With(typeof(T))); } return(requestedScript); }