using UnityEditor; using UnityEngine; namespace CreatureControl { public class Leg_Editor { private static bool showfield = false; public static bool Inspector(SerializedProperty legProp, TargetLeg targetLeg, Leg leg) { bool anythingChanged = false; GUIStyle foldoutStyle = new(EditorStyles.foldout) { margin = EditorStyles.objectField.margin }; EditorGUILayout.BeginHorizontal(); string legName = ConvertCamelCase(legProp.name); showfield = EditorGUILayout.Foldout(showfield, legName, true, foldoutStyle); SerializedProperty femurProp = legProp.FindPropertyRelative(nameof(Leg.femur)); SerializedProperty tibiaProp = legProp.FindPropertyRelative(nameof(Leg.tibia)); SerializedProperty tarsusProp = legProp.FindPropertyRelative(nameof(Leg.tarsus)); Transform newFemur = (Transform)EditorGUILayout.ObjectField(femurProp.objectReferenceValue, typeof(Transform), true); if (newFemur != femurProp.objectReferenceValue) { // Try to determine further bones when the femur has been updated femurProp.objectReferenceValue = newFemur; if (newFemur != null) { if (tibiaProp.objectReferenceValue == null && newFemur.childCount == 1) tibiaProp.objectReferenceValue = newFemur.GetChild(0); Transform tibia = (Transform)tibiaProp.objectReferenceValue; if (tibia != null) { if (tarsusProp.objectReferenceValue == null && tibia.childCount == 1) tarsusProp.objectReferenceValue = tibia.GetChild(0); } } anythingChanged = true; } EditorGUILayout.EndHorizontal(); if (femurProp.objectReferenceValue != null && tibiaProp.objectReferenceValue == null) showfield = true; if (showfield) { EditorGUI.indentLevel++; tibiaProp.objectReferenceValue = (Transform)EditorGUILayout.ObjectField("Lower Leg", tibiaProp.objectReferenceValue, typeof(Transform), true); tarsusProp.objectReferenceValue = (Transform)EditorGUILayout.ObjectField("Foot", tarsusProp.objectReferenceValue, typeof(Transform), true); // Need to check if anythingChanged and update the projection if it did EditorGUI.indentLevel--; } return anythingChanged; } private static string ConvertCamelCase(string text) { if (string.IsNullOrEmpty(text)) return text; System.Text.StringBuilder result = new(); for (int i = 0; i < text.Length; i++) { // Add a space before uppercase characters if (char.IsUpper(text[i]) && i > 0) result.Append(' '); // Add the character to the result result.Append(text[i]); } // Capitalize the first character of the result if (result.Length > 0) result[0] = char.ToUpper(result[0]); return result.ToString(); } } }