72 lines
3.1 KiB
C#

using UnityEditor;
using UnityEngine;
namespace CreatureControl {
public class Leg_Editor {
private static bool showfield = false;
public static void Inspector(SerializedProperty legProp) {
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) {
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);
}
}
}
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--;
}
}
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();
}
}
}