Unity UI Onclick inspector with Enumeration

僤鯓⒐⒋嵵緔 提交于 2019-12-13 03:34:23

问题


I have a question.

Here's My inspector Window.

In case of On Click() window, I'd like to set parameter that is type of Enum. not string or int.

In other words, I'd like to use void GoToNext(DATA_TYPE type). But that doesn't show up.

Even if I set my enum as [SerializedField], that doesn't show in this window.

How can I do this?


回答1:


You can't currently do this, Unity doesn't support it. But, since enums are basically ints, perhaps you could setup your function to accept an int and somehow cast it to the appropriate enum?

I tried this little experiment in some code I have with an enum, and it seemed to work fine:

public enum unitType {orc_warrior, archer, none};

    int test2 = 0;
    unitType test;
    test = (unitType)test2;
    Debug.Log(test);
    test2 = 1;
    test = (unitType)test2;
    Debug.Log(test);

Debug correctly printed out orc_warrior and then archer




回答2:


I found a great solution to this from the user 'TobiLaForge' on this Unity forum. At least it is the best solution worked for me where I had only a few enums to handle.

1) Declare your enum outside of your class you use it in or create it anywhere else outside of a MonoBehaviour.

2) Create a script, attach it to your button:

using UnityEngine;
public class GetEnum : MonoBehaviour{
    public MyEnum state;
}

3 Add this or change your orginial function where you use the enum

public void GetEnumState(GetEnum g)
{ if(g.state == MyEnum.something)
DoSomething();
}

4) In the OnClick() function slot select your function and drag the GetEnum script into the slot.

This will need a new MonoBehaviour script for each enum you use in that way. Here is my inspector after.



来源:https://stackoverflow.com/questions/49003811/unity-ui-onclick-inspector-with-enumeration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!