Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

A different RequireComponent

Discussion in 'Scripting' started by Rodolfo-Rubens, Apr 29, 2017.

  1. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,019
    Hey guys, I made a simple script that adds a useful functionality for component fields in inspector, instead of the "RequireComponent (typeof(CapsuleCollider))" attribute in the class I do this inside the class:

    Code (csharp):
    1. [RequiredComponent]
    2. public CapsuleCollider col;
    So, it will automatically get/add the required components to the col field when you add the monobehavior to an object, just like RequireComponent does but this also fills the field with the attribute.

    Here is the code:
    RequiredComponentDrawer.cs
    Goes in an Editor folder.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(RequiredComponentAttribute))]
    5. public class RequiredComponentDrawer : PropertyDrawer
    6. {
    7.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    8.     {
    9.         EditorGUI.PropertyField(position, property);
    10.  
    11.         MonoBehaviour mono = property.serializedObject.targetObject as MonoBehaviour;
    12.         //if(fieldInfo.FieldType.IsSubclassOf(typeof(Component)) )
    13.         if (typeof(Component).IsAssignableFrom(fieldInfo.FieldType))
    14.         {
    15.             if(property.objectReferenceValue == null)
    16.             {
    17.                 Component comp = mono.GetComponent(fieldInfo.FieldType);
    18.              
    19.                 if(comp == null)
    20.                 {
    21.                     comp = mono.gameObject.AddComponent(fieldInfo.FieldType);
    22.                 }
    23.              
    24.                 property.objectReferenceValue = comp;
    25.                 //property.serializedObject.ApplyModifiedProperties(); // not sure if this is really needed
    26.             }
    27.         }
    28.         else
    29.         {
    30.             Debug.LogError("Field <b>" + fieldInfo.Name + "</b> of " + mono.GetType() + " is not a component!", mono);
    31.         }
    32.     }
    33. }
    RequiredComponentAttribute.cs
    Goes outside the editor folder.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. public class RequiredComponentAttribute : PropertyAttribute
    4. {
    5. }
    6.  
    I hope this helps someone!

    Forgot to mention, this was based on this feedback: https://feedback.unity3d.com/suggestions/automatically-inject-requirecomponent-fields
     
    Last edited: Apr 29, 2017
  2. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    550
    You are awesome Rodolfo! that is exactly what I wanted in my feedback.