using Sirenix.OdinInspector.Editor;
using UnityEditor;
[CustomEditor(typeof(Point))]
public class PointEditor : OdinEditor
{
private SerializedProperty pointTypeProperty;
protected override void OnEnable()
{
base.OnEnable();
pointTypeProperty = serializedObject.FindProperty("pointType");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(pointTypeProperty);
serializedObject.ApplyModifiedProperties();
// 绘制包含Point类特定成员变量的GUI
DrawDefaultInspector();
}
}
对不起,我之前提供的代码中的 PropertyRange 特性确实是错误的,目前 Odin Inspector 不支持以这种方式动态设置 PropertyRange。为了解决这个问题,我们可以使用另一种方式来实现下拉列表功能。
下面是更新后的代码:
csharp
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
public class Point : MonoBehaviour
{
[SerializeField]
private string pointTypeName;
private PointType currentPointType;
private void OnValidate()
{
if (!string.IsNullOrEmpty(pointTypeName))
{
System.Type type = System.Type.GetType(pointTypeName);
if (type != null && type.IsSubclassOf(typeof(PointType)))
{
currentPointType = (PointType)System.Activator.CreateInstance(type);
}
}
}
[PropertySpace(10)]
[PropertyOrder(1)]
[PropertyDropdown("GetPointTypes")]
[OnValueChanged("UpdatePointTypeInInspector")]
public string PointTypeName
{
get { return pointTypeName; }
set { pointTypeName = value; }
}
private static string[] GetPointTypes()
{
// 获取所有继承了 PointType 类的类型
var derivedPointTypes = System.AppDomain.CurrentDomain.GetAssemblies()[0].GetTypes();
return System.Array.FindAll(derivedPointTypes, t => t.IsSubclassOf(typeof(PointType))).Select(t => t.FullName).ToArray();
}
private void UpdatePointTypeInInspector()
{
System.Type type = System.Type.GetType(pointTypeName);
if (type != null && type.IsSubclassOf(typeof(PointType)))
{
currentPointType = (PointType)System.Activator.CreateInstance(type);
}
else
{
currentPointType = null;
}
}
[PropertySpace(10)]
[PropertyOrder(2)]
[ShowIf(nameof(HasValidPointType))]
[InlineEditor(ObjectFieldMode = InlineEditorObjectFieldModes.Hidden)]
public PointType CurrentPointType
{
get { return currentPointType; }
}
private bool HasValidPointType()
{
return currentPointType != null;
}
}
在这个更新后的代码中,我们通过添加一个字符串字段 pointTypeName,来存储所选的继承 PointType 类的类型的全名。然后,我们在 OnValidate 方法中,通过反射获取所选类型,并根据其生成一个实例,并将其赋值给 currentPointType 变量。
在 OnInspectorGUI 方法中,我们使用 PropertyDropdown 特性来创建一个下拉列表,通过调用 GetPointTypes 方法获取所有继承 PointType 类的类型的全名。我们还使用 OnValueChanged 特性,来在选择下拉列表值时自动调用 UpdatePointTypeInInspector 方法,来更新 currentPointType 变量的值。
最后,我们使用 ShowIf 特性来检查是否存在有效的 currentPointType,如果存在,则使用 InlineEditor 特性来在 Inspector 窗口内嵌显示该类型的编辑器。
需要注意的是,在上述的代码中,我们假设了所有的派生类都位于同一个程序集中。如果有派生类位于其他程序集中,你需要相应地修改代码来获取这些类型。
希望这次提供的代码可以满足你的需求。如果你还有其他问题,请随时提问。
抱歉我之前的答案有误。你希望在选择特定子类之后,在同一个 Inspector 窗口中动态显示选中子类的成员变量。实现这一功能需要使用 Unity 的属性绘制API来实现动态绘制。
下面是更新后的代码:
csharp
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
public class Point : MonoBehaviour
{
[SerializeField]
private string pointTypeName;
private PointType currentPointType;
private SerializedObject serializedObject;
private void OnValidate()
{
if (!string.IsNullOrEmpty(pointTypeName))
{
System.Type type = System.Type.GetType(pointTypeName);
if (type != null && type.IsSubclassOf(typeof(PointType)))
{
currentPointType = (PointType)System.Activator.CreateInstance(type);
}
}
}
[PropertyOrder(1)]
[PropertyDropdown("GetPointTypes")]
[OnValueChanged("UpdatePointTypeInInspector")]
public string PointTypeName
{
get { return pointTypeName; }
set { pointTypeName = value; }
}
private static string[] GetPointTypes()
{
var derivedPointTypes = System.AppDomain.CurrentDomain.GetAssemblies()[0].GetTypes();
return System.Array.FindAll(derivedPointTypes, t => t.IsSubclassOf(typeof(PointType))).Select(t => t.FullName).ToArray();
}
private void UpdatePointTypeInInspector()
{
serializedObject = null;
System.Type type = System.Type.GetType(pointTypeName);
if (type != null && type.IsSubclassOf(typeof(PointType)))
{
currentPointType = (PointType)System.Activator.CreateInstance(type);
serializedObject = new SerializedObject(currentPointType);
}
else
{
currentPointType = null;
}
}
private void OnEnable()
{
UpdatePointTypeInInspector();
}
[PropertyOrder(2)]
[ShowIf(nameof(HasValidPointType))]
public void DrawPointTypeInspector()
{
if (serializedObject != null)
{
serializedObject.Update();
EditorGUILayout.Space();
var iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
enterChildren = false;
EditorGUILayout.PropertyField(iterator, true);
}
serializedObject.ApplyModifiedProperties();
}
}
private bool HasValidPointType()
{
return currentPointType != null;
}
}
在代码中,我们现在使用了 SerializedObject 类来处理选中的子类的绘制。当选择一个不同的子类时,我们将新建一个 SerializedObject 并在 DrawPointTypeInspector 方法中使用它来绘制选中子类的成员变量。
我们还添加了一个新方法 DrawPointTypeInspector,用于在 Inspector 窗口中绘制选中的子类的成员变量。在这个方法中,我们首先使用 serializedObject.GetIterator() 获取子类的所有序列化属性,并在循环中使用 EditorGUILayout.PropertyField 方法绘制每个属性。
最后,我们在 Point 类中添加了一个新的 [ShowIf] 特性用于判断是否存在有效的子类实例,并在其后调用 DrawPointTypeInspector 方法。
请注意,由于 Unity Inspector 的限制,我们不能将 DrawPointTypeInspector 方法直接绘制为一个按钮或可展开的区域。您需要手动调整其相对于其他属性的顺序和布局。
希望这次的答案满足你的需求。如果你有任何疑问,请随时提问。