Example #1
0
        public static UnityBindingConditionFactory ToPrefab(this IBindingFactory bindingFactory, Type type, string name)
        {
            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 prefab = Resources.Load(name);

            if (prefab == null)
            {
                throw new BindingException(string.Format(PREFAB_NAME_TYPE_IS_NULL, name, type.ToString()));
            }

            var prefabBinding = new PrefabBinding(prefab, type);

            return(new UnityBindingConditionFactory(bindingFactory.AddBinding(prefabBinding, BindingInstance.Transient), prefab.name));
        }
        /// <summary>
        /// Binds the key type to a transient <see cref="UnityEngine.Component"/>
        /// of <paramref name="type"/> on the prefab.
        ///
        /// If the <see cref="UnityEngine.Component"/> is not found on the prefab
        /// at the moment of the instantiation, it will be added.
        /// </summary>
        /// <remarks>
        /// Every resolution of a transient prefab will generate a new instance. So, even
        /// if the component resolved from the prefab is destroyed, it won't generate any
        /// missing references in the container.
        /// </remarks>
        /// <param name="bindingFactory">The original binding factory.</param>
        /// <param name="type">The component type.</param>
        /// <param name="name">Prefab name. It will be loaded using <c>Resources.Load<c/>.</param>
        /// <returns>The binding condition object related to this binding.</returns>
        public static IBindingConditionFactory ToPrefab(this IBindingFactory bindingFactory, Type type, string name)
        {
            if (!TypeUtils.IsAssignable(bindingFactory.bindingType, type))
            {
                throw new BindingException(BindingException.TYPE_NOT_ASSIGNABLE);
            }

            var isGameObject = TypeUtils.IsAssignable(typeof(GameObject), type);
            var isComponent  = TypeUtils.IsAssignable(typeof(Component), type);

            if (!isGameObject && !isComponent)
            {
                throw new BindingException(TYPE_NOT_COMPONENT);
            }

            var prefab = Resources.Load(name);

            if (prefab == null)
            {
                throw new BindingException(PREFAB_IS_NULL);
            }

            var prefabBinding = new PrefabBinding(prefab, type);

            return(bindingFactory.AddBinding(prefabBinding, BindingInstance.Transient));
        }