Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    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,013
    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
    djfunkey, StarManta and hippocoder like this.