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