Example #1
0
 private static void RequiresComponentsAttribute(GameObject gameObject, FieldInfo[] fieldInfos, List <Component> components, Component component, Type componentType)
 {
     if (componentType.GetCustomAttribute <RequiresComponents>() != null)
     {
         foreach (FieldInfo fieldInfo in fieldInfos)
         {
             if (fieldInfo.GetCustomAttribute <FieldRequiresComponent>() != null)
             {
                 Component matchingComponent = FindComponentOfType(components, fieldInfo.FieldType);
                 if (matchingComponent != null)
                 {
                     fieldInfo.SetValue(component, matchingComponent);
                 }
                 else
                 {
                     if (EditorDialogs.RequiresComponentDialog(fieldInfo.FieldType.Name))
                     {
                         fieldInfo.SetValue(component, gameObject.AddComponent(fieldInfo.FieldType));
                     }
                     else
                     {
                         gameObject.AddComponent <DeleteComponent>().componentReference = component;
                         return;
                     }
                 }
             }
             else
             {
                 FieldListRequiresComponent attribute = fieldInfo.GetCustomAttribute <FieldListRequiresComponent>();
                 if (attribute != null)
                 {
                     Type fieldType  = fieldInfo.FieldType.GetGenericArguments()[0];
                     var  listOfType = CreateListOfType(fieldType);
                     ForEachTypeComponent(components, fieldType, (Component eachComponent) => listOfType.Add(eachComponent));
                     if (attribute.RequireAtLeast > listOfType.Count)
                     {
                         if (EditorDialogs.RequiresMultipleComponentsDialog(fieldInfo.FieldType.Name,
                                                                            attribute.RequireAtLeast - listOfType.Count, attribute.RequireAtLeast))
                         {
                             for (int i = 0; i < attribute.RequireAtLeast; i++)
                             {
                                 listOfType.Add(gameObject.AddComponent(fieldInfo.FieldType));
                             }
                         }
                         else
                         {
                             gameObject.AddComponent <DeleteComponent>().componentReference = component;
                             return;
                         }
                     }
                     fieldInfo.SetValue(component, listOfType);
                 }
             }
         }
     }
 }
Example #2
0
        private static void ComponentRequiresComponentAttribute(GameObject gameObject, Component component, List <Component> components, Type componentType)
        {
            List <ComponentRequiresComponent> attributes = new List <ComponentRequiresComponent>();

            attributes.AddRange(componentType.GetCustomAttributes <ComponentRequiresComponent>());
            foreach (ComponentRequiresComponent attribute in attributes)
            {
                var       requiredComponentType = attribute.requiredComponentType;
                Component matchingComponent     = FindComponentOfType(components, requiredComponentType);
                if (matchingComponent == null)
                {
                    if (EditorDialogs.RequiresComponentDialog(requiredComponentType.Name))
                    {
                        gameObject.AddComponent(requiredComponentType);
                    }
                    else
                    {
                        gameObject.AddComponent <DeleteComponent>().componentReference = component;
                        return;
                    }
                }
            }
        }