0

我正在创建一个 GUI,用于在受UnityEvent GUI启发的 Unity 自定义EditorWindow中选择一个函数。我无法让UnityEvent本身工作;使用EditorGUILayout.PropertyField并将UnityEvent成员作为序列化属性引用会产生一个空的折叠。

我选择了一个有效的函数,但我不知道如何允许用户指定函数参数参数。

using System.Reflection;
int functionIndex = 0;
MethodInfo[] methods = typeof(LvlGenFunctions).GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
string methodNames = new string[methods.Length];
for(int i = 0; i < methods.Length; ++i)
{
  methodNames[i] = methods[i].Name;
}
functionIndex = EditorGUILayout.Popup(functionIndex, methodNames);
methods[functionIndex].Invoke(null, null);

我可以获得ParameterInfoParameterType,但我不知道如何创建输入 GUI 字段来指定适当的参数参数。

如何为类型由反射确定的参数创建 Unity IMGUI 输入字段?

4

1 回答 1

0

要清楚

EditorWindow : ScriptableObjectScriptableObject : Object

SerializedObject使用,UnityEngine.Object因此您可以使用此构造函数:

new SerializedObject(Object target);

EditorWindow

public UnityEvent OnSomeEvent;
private SerializedObject serializedObject;
private SerializedProperty property;

private void OnEnable()
{
    serializedObject = new SerializedObject(this);
    property = serializedObject.FindProperty(nameof(OnSomeEvent));
}

private void OnGUI()
{
    EditorGUILayout.PropertyField(property, true);
}

现在您有了检查员,但这不是您想要的。


问题是太丑了。

更新您的代码,如下所示:

public UnityEvent OnSomeEvent;
private SerializedObject serializedObject;
private SerializedProperty property;
// new fields
private UnityEventDrawer EventDrawer;
private GUIContent Content;

private void OnEnable()
{
    serializedObject = new SerializedObject(this);
    property = serializedObject.FindProperty(nameof(OnSomeEvent));
    // new initialization
    EventDrawer = new UnityEventDrawer();
    Content = new GUIContent(property.displayName, property.tooltip);
}

private void OnGUI()
{
    // new drawer
    var eventHeigth = EventDrawer.GetPropertyHeight(property, label);
    var rect = EditorGUILayout.GetControlRect(true, eventHeigth);
    EventDrawer.OnGUI(rect, property, label);
}
于 2018-07-29T03:19:01.967 回答