使用检查器为动态列表分配ButtonClickedEvent的Unity 2018错误

时间:2019-07-04 21:27:07

标签: c# unity3d

我写了一些动态列表来填写统一检查器,

[Serializable]
public class MenuItem
{
    public string title = "Item";
    public bool enabled = true;
    public Button.ButtonClickedEvent onClick = null;
    public MenuItem[] submenu;
}
public MenuItem[] menu = null;

现在,当我使用检查器为菜单分配值时,在第一级中一切正常。但是在第二级之后,我无法将ButtonClickedEvent分配给创建的MenuItem子项。我在统一编辑器中收到此错误:

type is not a enum value
UnityEditor.SerializedProperty:set_enumValueIndex(Int32)
UnityEditorInternal.UnityEventDrawer:AddEventListener(ReorderableList) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:355)
UnityEditorInternal.Defaults:DrawFooter(Rect, ReorderableList) (at C:/buildslave/unity/build/Editor/Mono/GUI/ReorderableList.cs:115)
UnityEditorInternal.ReorderableList:DoListFooter(Rect) (at C:/buildslave/unity/build/Editor/Mono/GUI/ReorderableList.cs:641)
UnityEditorInternal.ReorderableList:DoList(Rect) (at C:/buildslave/unity/build/Editor/Mono/GUI/ReorderableList.cs:413)
UnityEditorInternal.UnityEventDrawer:OnGUI(Rect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:148)
UnityEditorInternal.UnityEventDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:117)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

在检查器中,我看到一个奇怪的0。 unity inspector error for dynamic list with ButtonClickedEvent in childs

1 个答案:

答案 0 :(得分:-1)

这里 UnityEvent Editor breaking in nested struct 有一个建议使用ScriptableObject而不是递归列表的答案。但是没有办法!

但是请注意答案,如果我有特定级别的水平,我可以使用类似的东西

[Serializable]
public class MenuItem
{
    public string title = "Item";
    public bool enabled = true;
    public int width = 100;
    public string relatedLeftDockPanel = "";
    public string relatedRightDockPanel = "";
    public Button.ButtonClickedEvent onClick = null;
    public MenuItemLevel1[] submenu;
}

[Serializable]
public class MenuItemLevel1
{
    public string title = "Item";
    public bool enabled = true;
    public int width = 100;
    public string relatedLeftDockPanel = "";
    public string relatedRightDockPanel = "";
    public Button.ButtonClickedEvent onClick = null;
    public MenuItemLevel2[] submenu;
}

[Serializable]
public class MenuItemLevel2
{
    public string title = "Item";
    public bool enabled = true;
    public int width = 100;
    public string relatedLeftDockPanel = "";
    public string relatedRightDockPanel = "";
    public Button.ButtonClickedEvent onClick = null;
}

编辑:甚至更好

public class MenuItem
{
    public string title = "Item";
    public bool enabled = true;
    public int width = 100;
    public string relatedLeftDockPanel = "";
    public string relatedRightDockPanel = "";
    public Button.ButtonClickedEvent onClick = null;
}

[Serializable]
public class MenuItemLevel0 : MenuItem
{
    public MenuItemLevel1[] submenu;
}

[Serializable]
public class MenuItemLevel1 : MenuItem
{
    public MenuItemLevel2[] submenu;
}

[Serializable]
public class MenuItemLevel2 : MenuItem
{
}

它可以解决问题,但是我想有一些更好的解决方案。