/// <summary> /// Binds the key type to singletons <see cref="UnityEngine.Component"/> /// of itself on game objects of a given <paramref name="tag"/>. /// /// If <paramref name="type"/> is <see cref="UnityEngine.GameObject"/>, binds the /// key to the GameObject itself. /// /// If <paramref name="type"/> is see cref="UnityEngine.Component"/>, binds the key /// to the the instance of the component. /// /// If the <see cref="UnityEngine.Component"/> is not found on the GameObject, it will be added. /// </summary> /// <remarks> /// To prevent references to destroyed objects, only bind to game objects that won't /// be destroyed in the scene. /// </remarks> /// <param name="bindingFactory">The original binding factory.</param> /// <param name="type">The component type.</param> /// <param name="tag">The GameObject tag.</param> /// <returns>The binding condition object related to this binding.</returns> public static IBindingConditionFactory ToGameObjectsWithTag(this IBindingFactory bindingFactory, Type type, string tag) { if (!TypeUtils.IsAssignable(bindingFactory.bindingType, type)) { throw new BindingException(BindingException.TYPE_NOT_ASSIGNABLE); } var isGameObject = TypeUtils.IsAssignable(TYPE_GAME_OBJECT, type); var isComponent = TypeUtils.IsAssignable(TYPE_COMPONENT, type); if (!isGameObject && !isComponent) { throw new BindingException(TYPE_NOT_COMPONENT); } var gameObjects = GameObject.FindGameObjectsWithTag(tag); var bindingFactories = new IBindingConditionFactory[gameObjects.Length]; for (int gameObjectIndex = 0; gameObjectIndex < gameObjects.Length; gameObjectIndex++) { bindingFactories[gameObjectIndex] = CreateSingletonBinding(bindingFactory, gameObjects[gameObjectIndex], type, isGameObject); } return(new MultipleBindingConditionFactory(bindingFactories, bindingFactory.binder)); }
/// <summary> /// Binds the key type to all assignable types in a given <paramref name="namespaceName"/> /// as singleton bindings. /// </summary> /// <param name="namespaceName">Namespace name.</param> /// <param name="bindingInstance">Binding instance type.</param>. /// <param name="includeChildren">Indicates whether child namespaces should be included.</param> /// <returns>The binding condition object related to this binding.</returns> protected IBindingConditionFactory ToNamespace(string namespaceName, BindingInstance bindingInstance, bool includeChildren) { var types = TypeUtils.GetAssignableTypes(this.bindingType, namespaceName, includeChildren); IBindingConditionFactory[] bindingConditionFactories = new IBindingConditionFactory[types.Length]; for (int typeIndex = 0; typeIndex < types.Length; typeIndex++) { bindingConditionFactories[typeIndex] = this.AddBinding(types[typeIndex], bindingInstance); } return(this.CreateBindingConditionFactoryProvider(bindingConditionFactories)); }
/// <summary> /// Initializes a new instance of the <see cref="Adic.UnityBindingConditionFactory"/> class. /// </summary> /// <param name="bindingConditionFactory">Original binding condition factory.</param> /// <param name="objectName">Related Unity Object name.</param> public UnityBindingConditionFactory(IBindingConditionFactory bindingConditionFactory, string objectName) { this.bindingConditionFactory = bindingConditionFactory; this.objectName = objectName; }