如何解决如何根据所述数组的长度重命名数组中的元素? 在 Unity Inspector 中
我正在尝试创建一个程序动画引擎。因为我使用叉积来计算身体的方向,所以腿需要在数组中按特定顺序排列。
我想确保它对任何使用它的人来说都是直观的,所以理想情况下,我希望它根据数组的长度重命名检查器中的元素。
示例: ideal example
解决方法
这种东西是通过实现 CustomEditor
或 CustomPropertyDrawer
让我们例如说你的班级看起来像
public class DynamicLegList : MonoBehaviour
{
public LegStepper[] Legs;
}
然后你可以实现你通过例如展示的东西
// 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,string>>
{
{
2,new Dictionary<int,string>()
{
{0,"RightLeg"},{1,"LeftLeg"},}
},{
4,"FrontRightLeg"},"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();
}
}
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。