public static bool AssignRequiredComponentReference(EntroPiBehaviour target, FieldInfo field, RequiredComponentAttribute attribute) { bool isReferenceAssigned = false; Type fieldType = field.FieldType; Component requiredComponent = null; switch (attribute.Relation) { case ComponentRelation.Self: requiredComponent = target.GetComponent(fieldType); break; case ComponentRelation.Children: requiredComponent = target.GetComponentInChildren(fieldType, attribute.IncludeInactive); break; case ComponentRelation.Parent: requiredComponent = target.GetComponentInParent(fieldType); break; default: Debug.LogError("[RequiredComponent]\nInvalid component relation."); break; } isReferenceAssigned = requiredComponent != null; Debug.AssertFormat(isReferenceAssigned, target, "[RequiredComponent]\nFailed to get component for variable {1} (Type: {0}) in script {2}", field.FieldType, field.Name, target.GetType()); field.SetValue(target, requiredComponent); return(isReferenceAssigned); }
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 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); }