Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Unity编辑器中显示数组成员的某些属性,具体取决于这些数组成员的其他属性_C#_Unity3d_Unity3d Editor - Fatal编程技术网

C# 在Unity编辑器中显示数组成员的某些属性,具体取决于这些数组成员的其他属性

C# 在Unity编辑器中显示数组成员的某些属性,具体取决于这些数组成员的其他属性,c#,unity3d,unity3d-editor,C#,Unity3d,Unity3d Editor,我有一个类MyClass,它有enum和属性。根据枚举,我希望在编辑器中显示某些属性 有这样的枚举{first,second}和属性health,step,position。如果选择第一个,则在编辑器中显示名称和步骤;如果选择第二个,则显示步骤和位置。我想出了如何在Monobhavior类中实现这一点。如何做到这一点,使数组的每个元素都具有动态属性?当我选择此列表时,图像将突出显示我希望看到的字段。提前谢谢。对不起,我的英语不好 下面是一个使用UnityEditorInternal中的ReoOr

我有一个类
MyClass
,它有enum和属性。根据枚举,我希望在编辑器中显示某些属性

有这样的枚举{first,second}和属性health,step,position。如果选择第一个,则在编辑器中显示名称和步骤;如果选择第二个,则显示步骤和位置。我想出了如何在Monobhavior类中实现这一点。如何做到这一点,使数组的每个元素都具有动态属性?当我选择此列表时,图像将突出显示我希望看到的字段。提前谢谢。对不起,我的英语不好


下面是一个使用
UnityEditorInternal
中的
ReoOrderableList
的示例(我基本上是通过使用来学习的),我发现这比在InspectorGUI上直接做任何事情都更加灵活和干净

还有一个额外的特性,正如它的名字所说的:可以使用拖放选择元素并使其可重新排序

列表元素的类

[可序列化]
puclic类YourClass
{
公共枚举YourEnum
{
第一,
第二
}
公共YourEnum Enum;
公共字符串名称;
公共int步骤;
公共向量3位置;
}
包含列表的类

public class YourOtherClass:monobhavior
{
public List YourList=新列表();
//如果您愿意,它对数组也同样适用,无需更改检查器
//请注意,在这种情况下,您还不能在这里初始化它,但检查器会为您进行初始化
//公开你的班级[]你的名单;
}
编辑器

[CustomEditor(typeof(YourOtherClass))]
公共类YourOtherClassEditor:编辑器
{
//这将是YourOtherClass.YourList的序列化“副本”
私有财产清单;
私人可再减额列表您的可再减额列表;
私有void OnEnable()
{
//步骤1“链接”SerializedProperties到OtherClass的属性
YourList=serializedObject.FindProperty(“YourList”);
//步骤2:设置可重新降级列表
YourReorderableList=新的ReorderableList(serializedObject,YourList)
{
//可以拖动对象并更改其在列表中的位置吗?
draggable=true,
//可以通过按“+”按钮添加元素吗?
displayAdd=true,
//您能按“-”按钮删除元素吗?
displayRemove=true,
//为列表制作一个标题
drawHeaderCallback=rect=>
{
LabelField(rect,“这是您的元素”);
},
//现在进入有趣的部分:在这里设置元素的外观
DropelementCallback=(矩形、索引、活动、聚焦)=>
{
//从列表中获取当前要绘制的元素
var element=YourList.getArrayElementIndex(索引);
//将元素属性转换为SerializedProperties
var Enum=element.FindPropertyRelative(“Enum”);
var Name=element.FindPropertyRelative(“名称”);
var步骤=元素。FindPropertyRelative(“步骤”);
var位置=元素。FindPropertyRelative(“位置”);
//绘制枚举字段
属性字段(新的Rect(Rect.x、Rect.y、Rect.width、EditorGUIUtility.singleLineHeight)、枚举);
//在下一行中启动下一个属性
rect.y+=EditorGUIUtility.singleLineHeight;
//仅在选择“第一个”时显示名称字段
if((YourClass.YourEnum)Enum.intValue==YourClass.YourEnum.first)
{
EditorGUI.PropertyField(新的Rect(Rect.x、Rect.y、Rect.width、EditorGUIUtility.singleLineHeight)、Name);
//在下一行中启动下一个属性
rect.y+=EditorGUIUtility.singleLineHeight;
}
//绘制步骤字段
属性字段(新的Rect(Rect.x,Rect.y,Rect.width,EditorGUIUtility.singleLineHeight),步长);
//在下一行中启动下一个属性
rect.y+=EditorGUIUtility.singleLineHeight;
//仅在选择“秒”时显示步骤字段
if((YourClass.YourEnum)Enum.intValue==YourClass.YourEnum.second)
{
EditorGUI.PropertyField(新的Rect(Rect.x、Rect.y、Rect.width、EditorGUIUtility.singleLineHeight)、位置);
}
},
//由于我们有多行(默认),您必须进行配置
//你的元素有多高。幸运的是,在你的例子中,它总是精确的
//每种情况下3行。如果不是,则必须更改此项。
//在某些情况下,如果在元素之间使用一条以上的线作为分隔符,它也会变得更可读
elementHeight=EditorGUIUtility.singleLineHeight*3,
//或者,如果您有不同的高度,您可以使用。
//elementHeightCallback=索引=>
//{
//var element=YourList.getArrayElementIndex(索引);
//var Enum=element.FindPropertyRelative(“Enum”);
//开关((YourClass.YourEnum)Enum.intValue)
//    {
//案例YourClass.YourEnum.first:
//返回EditorGUIUtility.singleLineHeight*3;
//案例YourClass.YourEnum.second:
//返回EditorGUIUtility.singleLineHeight*5;
//默认值:
//返回EditorGUIUtility.singleLineHeight;
//    }
//}