예제 #1
0
        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);
        }
예제 #2
0
        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);
        }