Ste*_*tte 3 c# unity-game-engine unity3d-editor
我有一个MyClass具有枚举和属性的类。根据枚举,我想在编辑器中显示某些属性。
有这样的枚举 {first, Second} 和属性 health, step,position 。如果选择第一个,则在编辑器中显示名称和步骤,如果选择第二个,则显示步骤和位置。我想出了如何为单一行为类做到这一点。以及如何执行此操作以使数组的每个元素都具有动态属性?该图像突出显示了我在选择此列表时希望看到的字段。提前致谢 。抱歉我的英语不好
ReaorderableList这是一个使用from的示例UnityEditorInternal(我基本上是使用这个很酷的 turorial来学习的),我发现它比直接在OnInspectorGUI.
顾名思义,一个附加功能是:可以使用拖放来选择元素并使其可重新排序!
List 元素的类
[Serializeable]
puclic class YourClass
{
public enum YourEnum
{
first,
second
}
public YourEnum Enum;
public string Name;
public int Step;
public Vector3 Position;
}
Run Code Online (Sandbox Code Playgroud)
包含列表的类
public class YourOtherClass : MonoBehaviour
{
public List<YourClass> YourList = new List<YourClass>();
// It works the same for arrays if you prefere that, no need to change the inspector
// Note that in this case you can't initalize it here yet but the Inspector does that for you
// public YourClass[] YourList ;
}
Run Code Online (Sandbox Code Playgroud)
编辑
[CustomEditor(typeof(YourOtherClass))]
public class YourOtherClassEditor : Editor
{
// This will be the serialized "copy" of YourOtherClass.YourList
private SerializedProperty YourList;
private ReorderableList YourReorderableList;
private void OnEnable()
{
// Step 1 "link" the SerializedProperties to the properties of YourOtherClass
YourList = serializedObject.FindProperty("YourList");
// Step 2 setup the ReorderableList
YourReorderableList = new ReorderableList(serializedObject, YourList)
{
// Can your objects be dragged an their positions changed within the List?
draggable = true,
// Can you add elements by pressing the "+" button?
displayAdd = true,
// Can you remove Elements by pressing the "-" button?
displayRemove = true,
// Make a header for the list
drawHeaderCallback = rect =>
{
EditorGUI.LabelField(rect, "This are your Elements");
},
// Now to the interesting part: Here you setup how elements look like
drawElementCallback = (rect, index, active, focused) =>
{
// Get the currently to be drawn element from YourList
var element = YourList.GetArrayElementAtIndex(index);
// Get the elements Properties into SerializedProperties
var Enum = element.FindPropertyRelative("Enum");
var Name = element.FindPropertyRelative("Name");
var Step = element.FindPropertyRelative("Step");
var Position = element.FindPropertyRelative("Position");
// Draw the Enum field
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), Enum);
// start the next property in the next line
rect.y += EditorGUIUtility.singleLineHeight;
// only show Name field if selected "first"
if ((YourClass.YourEnum)Enum.intValue == YourClass.YourEnum.first)
{
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), Name);
// start the next property in the next line
rect.y += EditorGUIUtility.singleLineHeight;
}
// Draw the Step field
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), Step);
// start the next property in the next line
rect.y += EditorGUIUtility.singleLineHeight;
// only show Step field if selected "seconds"
if ((YourClass.YourEnum)Enum.intValue == YourClass.YourEnum.second)
{
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), Position);
}
},
// And since we have more than one line (default) you'll have to configure
// how tall your elements are. Luckyly in your example it will always be exactly
// 3 Lines in each case. If not you would have to change this.
// In some cases it becomes also more readable if you use one more Line as spacer between the elements
elementHeight = EditorGUIUtility.singleLineHeight * 3,
//alternatively if you have different heights you would use e.g.
//elementHeightCallback = index =>
//{
// var element = YourList.GetArrayElementAtIndex(index);
// var Enum = element.FindPropertyRelative("Enum");
// switch ((YourClass.YourEnum)Enum.intValue)
// {
// case YourClass.YourEnum.first:
// return EditorGUIUtility.singleLineHeight * 3;
// case YourClass.YourEnum.second:
// return EditorGUIUtility.singleLineHeight * 5;
// default:
// return EditorGUIUtility.singleLineHeight;
// }
//}
// optional: Set default Values when adding a new element
// (otherwise the values of the last list item will be copied)
onAddCallback = list =>
{
// The new index will be the current List size ()before adding
var index = list.serializedProperty.arraySize;
// Since this method overwrites the usual adding, we have to do it manually:
// Simply counting up the array size will automatically add an element
list.serializedProperty.arraySize++;
list.index = index;
var element = list.serializedProperty.GetArrayElementAtIndex(index);
// again link the properties of the element in SerializedProperties
var Enum = element.FindPropertyRelative("Enum");
var Name = element.FindPropertyRelative("Name");
var Step = element.FindPropertyRelative("Step");
var Position = element.FindPropertyRelative("Position");
// and set default values
Enum.intValue = (int) YourClass.YourEnum.first;
Name.stringValue = "";
Step.intValue = 0;
Position.vector3Value = Vector3.zero;
}
};
}
public override void OnInspectorGUI()
{
// copy the values of the real Class to the linked SerializedProperties
serializedObject.Update();
// print the reorderable list
YourReorderableList.DoLayoutList();
// apply the changed SerializedProperties values to the real class
serializedObject.ApplyModifiedProperties();
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记使用
using UnityEditor;
using UnityEditorInternal;
Run Code Online (Sandbox Code Playgroud)
结果: