using UnityEngine; using UnityEngine.Splines; namespace UnityEditor.Splines { /// /// A PropertyDrawer used to show a popup menu with available spline indices relative to a . /// Add to a serialized integer type to use. /// [CustomPropertyDrawer(typeof(SplineIndexAttribute))] public class SplineIndexPropertyDrawer : PropertyDrawer { static readonly int[] k_MissingContainerValues = new int[] { 0 }; static readonly GUIContent[] k_MissingContainerContent = new GUIContent[] { new GUIContent("") }; string GetWarningMessage(SplineIndexAttribute attrib) => $"SplineIndex property attribute does not reference a valid SplineContainer: \"{attrib.SplineContainerProperty}\""; /// /// Returns the height of a SerializedProperty in pixels. /// /// The SerializedProperty to calculate height for. /// The label of the SerializedProperty. /// Returns the height of a SerializedProperty in pixels. public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return EditorStyles.popup.CalcSize(label).y; } /// /// Creates an interface for a SerializedProperty with an integer property type. /// /// Rectangle on the screen to use for the property GUI. /// The SerializedProperty to make the custom GUI for. /// The label of this property. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.propertyType != SerializedPropertyType.Integer || attribute is not SplineIndexAttribute attrib) return; var path = property.propertyPath.Replace(property.name, attrib.SplineContainerProperty); var container = property.serializedObject.FindProperty(path); if (container == null || !(container.objectReferenceValue is ISplineContainer res)) { new EditorGUI.DisabledScope(true); EditorGUI.IntPopup(position, label, 0, k_MissingContainerContent, k_MissingContainerValues); } else SplineGUI.SplineIndexField(position, property, label, res.Splines.Count); } } }