AutoReference

// Adds an inline button to the attribute that when clicked // will parse through the hierarchy of the gameObject and find // a component matching the type auto filling out the value reference to it // Usage.cs using UnityEngine; public class Usage   : MonoBehaviour {   [AutoReference] public Rigidbody m_Body; } // AutoReferenceAttribute.cs using System; [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] public class AutoReferenceAttribute  : Attribute {   public AutoReferenceAttribute { } } // AutoReferenceAttributeDrawer.cs using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEditor; using Sirenix.OdinInspector.Editor; using Sirenix.Utilities; using Sirenix.Utilities.Editor; [OdinDrawer] [DrawerPriority(DrawerPriorityLevel.AttributePriority)] public class AutoReferenceAttributeDrawer   : OdinAttributeDrawer {      protected override void DrawPropertyLayout (InspectorProperty property, AutoReferenceAttribute attribute, GUIContent label) {       if (property.Tree.WeakTargets.Count != 1) {           SirenixEditorGUI.ErrorMessageBox("AutoReferenceAttribute doesn't support editing more then 1 at a time!"); return; }       if ((property.Tree.WeakTargets[0] as Component) == null) {           SirenixEditorGUI.ErrorMessageBox("AutoReferenceAttribute doesn't support being on anything but a MonoBehaviour!"); return; }       EditorGUILayout.BeginHorizontal; EditorGUILayout.BeginVertical; this.CallNextDrawer(property, label); EditorGUILayout.EndVertical; if (GUILayout.Button("Auto Populate", EditorStyles.miniButton, GUILayoutOptions.ExpandWidth(false).MinWidth(20))) {           ConnectReference(property); }       /* TODO: Fix multi options available if button clicked { var components = GetComponentsInChildren; if components.length > 1 { var selector = GenericSelector(false, components, x => x.gameObject.name); // (something like that) selector.Confirmed += x => property.BaseValueEntry.WeakSmartValue = x.First; selector.ShowInPopup(optionalRectToMakeItLineUpWithTheProperty) } }        */        EditorGUILayout.EndHorizontal; }   private void ConnectReference(InspectorProperty property) {       Component obj = property.Tree.WeakTargets[0] as Component; property.BaseValueEntry.WeakSmartValue = obj.gameObject.GetComponentInChildren(property.ValueEntry.BaseValueType); } }
 * 1) if UNITY_EDITOR
 * 1) endif