public static bool AssignRequiredComponentListReference(EntroPiBehaviour target, FieldInfo field, RequiredComponentListAttribute attribute)
        {
            bool isReferenceAssigned = false;

            Type fieldType          = field.FieldType;
            bool isFieldGenericList = EntroPiAttributesUtil.IsTypeofGenericList(fieldType);

            Debug.AssertFormat(isFieldGenericList, target, "[RequiredComponentList] Attribute is assigned to variable with incorrect type.\nVariable {0} in {1} is not a Generic List.", field.Name, target.GetType());

            if (isFieldGenericList)
            {
                Type itemType = fieldType.GetGenericArguments()[0];

                IList fieldList = field.GetValue(target) as IList;

                if (fieldList == null)
                {
                    Type listType = typeof(List <>).MakeGenericType(itemType);
                    fieldList = Activator.CreateInstance(listType) as IList;
                    field.SetValue(target, fieldList);
                }

                Component[] requiredComponents = null;

                switch (attribute.Relation)
                {
                case ComponentRelation.Self:
                    requiredComponents = target.GetComponents(itemType);
                    break;

                case ComponentRelation.Children:
                    requiredComponents = target.GetComponentsInChildren(itemType, attribute.IncludeInactive);
                    break;

                case ComponentRelation.Parent:
                    requiredComponents = target.GetComponentsInParent(itemType);
                    break;

                default:
                    Debug.LogError("[RequiredComponentList]\nInvalid component relation.");
                    break;
                }

                foreach (Component component in requiredComponents)
                {
                    fieldList.Add(component);
                }

                bool isListCountInRange = EntroPiAttributesUtil.CheckListCount(fieldList.Count, attribute.CountMin, attribute.CountMax);

                Debug.AssertFormat(isListCountInRange, target, "[RequiredComponentList]\nInvalid list item count for variable {0} in {1} ", field.Name, target.GetType());

                isReferenceAssigned = isListCountInRange;
            }

            return(isReferenceAssigned);
        }
        public static bool CheckRequiredReferenceList(UnityEngine.Object target, FieldInfo field, RequiredReferenceListAttribute attribute)
        {
            bool isReferenceAssigned = false;

            Type fieldType          = field.FieldType;
            bool isFieldGenericList = EntroPiAttributesUtil.IsTypeofGenericList(fieldType);

            Debug.AssertFormat(isFieldGenericList, target, "[RequiredReferenceList] Attribute is assigned to variable with incorrect type.\nVariable {0} in {1} is not a Generic List.", field.Name, target.GetType());

            if (isFieldGenericList)
            {
                Type itemType         = fieldType.GetGenericArguments()[0];
                bool isItemTypeObject = EntroPiAttributesUtil.IsSameOrSubclassOfType <UnityEngine.Object>(itemType);

                Debug.AssertFormat(isItemTypeObject, target, "[RequiredReferenceList] Attribute is assigned to List<> with incorrect type.\nType {0} is not a Unity Object.", itemType);

                if (isItemTypeObject)
                {
                    IList fieldList = field.GetValue(target) as IList;

                    bool isListInitialized = fieldList != null;

                    Debug.AssertFormat(isListInitialized, target, "[RequiredReferenceList]\nList<> variable {0} in {1} is not initialized.", field.Name, target.GetType());

                    if (isListInitialized)
                    {
                        bool areAllListReferencesAssigned = true;

                        for (int i = 0; i < fieldList.Count; ++i)
                        {
                            areAllListReferencesAssigned &= EntroPiAttributesUtil.CheckUnityObjectReference(fieldList[i]);
                        }

                        Debug.AssertFormat(areAllListReferencesAssigned, target, "[RequiredReferenceList]\nList<> variable {0} in {1} contains unassigned references.", field.Name, target.GetType());

                        if (areAllListReferencesAssigned)
                        {
                            bool isListCountInRange = EntroPiAttributesUtil.CheckListCount(fieldList.Count, attribute.CountMin, attribute.CountMax);

                            Debug.AssertFormat(isListCountInRange, target, "[RequiredReferenceList]\nInvalid list item count for variable {0} in {1} ", field.Name, target.GetType());

                            isReferenceAssigned = isListCountInRange;
                        }
                    }
                }
            }

            return(isReferenceAssigned);
        }
示例#3
0
        private bool AssignReferences()
        {
            bool areAllReferencesAssigned = true;

            Type behaviourType = this.GetType();

            FieldInfo[] fieldInfo = behaviourType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            foreach (FieldInfo field in fieldInfo)
            {
                object[] attributes = field.GetCustomAttributes(true);
                foreach (object attribute in attributes)
                {
                    if (attribute is RequiredComponentAttribute)
                    {
                        areAllReferencesAssigned &= false;
                        Debug.LogError("[EntroPiObject] RequiredComponentAttribute attribute is not supported");
                    }
                    else if (attribute is RequiredComponentListAttribute)
                    {
                        areAllReferencesAssigned &= false;
                        Debug.LogError("[EntroPiObject] RequiredComponentListAttribute attribute is not supported");
                    }
                    else if (attribute is RequiredReferenceAttribute)
                    {
                        areAllReferencesAssigned &= EntroPiAttributesUtil.CheckRequiredReference(this, field, attribute as RequiredReferenceAttribute);
                    }
                    else if (attribute is RequiredReferenceListAttribute)
                    {
                        areAllReferencesAssigned &= EntroPiAttributesUtil.CheckRequiredReferenceList(this, field, attribute as RequiredReferenceListAttribute);
                    }
                    else if (attribute is RequiredSceneObjectAttribute)
                    {
                        areAllReferencesAssigned &= false;
                        Debug.LogError("[EntroPiObject] RequiredSceneObjectAttribute attribute is not supported");
                    }
                }
            }

            return(areAllReferencesAssigned);
        }
        public static bool AssignRequiredSceneObjectReference(EntroPiBehaviour target, FieldInfo field, RequiredSceneObjectAttribute attribute)
        {
            bool isReferenceAssigned = false;

            Type fieldType         = field.FieldType;
            bool isObjectReference = EntroPiAttributesUtil.IsSameOrSubclassOfType <UnityEngine.Object>(fieldType);

            Debug.AssertFormat(isObjectReference, target, "[RequiredSceneObject] Attribute is assigned to variable with incorrect type.\nType {0} is not a Unity Object reference.", fieldType);

            if (isObjectReference)
            {
                UnityEngine.Object fieldObject = UnityEngine.Object.FindObjectOfType(field.FieldType);

                isReferenceAssigned = fieldObject != null;

                Debug.AssertFormat(isReferenceAssigned, target, "[RequiredSceneObject]\nFailed to find object of type {0} for variable {1} in script {2}", field.FieldType, field.Name, target.GetType());

                field.SetValue(target, fieldObject);
            }

            return(isReferenceAssigned);
        }