[英]Unity How to remove the elements label (Element 0 - Element …) in a array in the inspector
[英]How to rename elements in an array based on the length of said array? (In the Unity Inspector)
我正在嘗試創建一個程序 animation 引擎。 因為我使用叉積來計算身體的方向,所以腿在數組中需要按特定順序排列。
我想確保它對任何使用它的人來說都是直觀的,所以理想情況下,我希望它根據數組的長度重命名檢查器中的元素。
示例:理想示例
這種東西是通過實現CustomEditor
或CustomPropertyDrawer
來實現的
例如,假設您的 class 看起來像
public class DynamicLegList : MonoBehaviour
{
public LegStepper[] Legs;
}
然后你可以實現你通過 eg 展示的東西
// Since the UnityEditor namespace is completely stripped of during the build process
// this should be placed in a folder called "Editor"
// or if using Assemblies this should be placed in a separate Assembly which
// is excluded from any platform except the UnityEditor itself
[CustomEditor(typeof(DynamicLegList))]
internal class DynamicLegListEditor : Editor
{
private SerializedProperty legs;
private void OnEnable()
{
legs = serializedObject.FindProperty(nameof(DynamicLegList.Legs));
}
private readonly Dictionary<int, Dictionary<int, string>> legNames = new Dictionary<int, Dictionary<int, string>>
{
{
2, new Dictionary<int, string>()
{
{0, "RightLeg"},
{1, "LeftLeg"},
}
},
{
4, new Dictionary<int, string>()
{
{0, "FrontRightLeg"},
{1, "FrontLeftLeg"},
{2, "BackRightLeg"},
{3, "BackLeftLeg"}
}
}
};
private int count;
public override void OnInspectorGUI()
{
DrawScriptField();
serializedObject.Update();
legs.isExpanded = EditorGUILayout.Foldout(legs.isExpanded, legs.displayName, true);
if (legs.isExpanded)
{
EditorGUI.indentLevel++;
{
EditorGUI.BeginChangeCheck();
{
count = EditorGUILayout.IntField("Size", legs.arraySize);
}
if (EditorGUI.EndChangeCheck())
{
if (count == 1 || legNames.ContainsKey(count))
{
legs.arraySize = count;
}
}
if (legs.arraySize == 1)
{
var leg = legs.GetArrayElementAtIndex(0);
EditorGUILayout.PropertyField(leg, new GUIContent("Leg"));
}
else
{
if (legNames.TryGetValue(legs.arraySize, out var names))
{
for (var i = 0; i < legs.arraySize; i++)
{
var leg = legs.GetArrayElementAtIndex(i);
EditorGUILayout.PropertyField(leg, new GUIContent(names[i]));
}
}
}
}
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties();
}
private void DrawScriptField()
{
EditorGUI.BeginDisabledGroup(true);
{
EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((DynamicLegList) target), typeof(DynamicLegList), false);
}
EditorGUI.EndDisabledGroup();
}
}
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.