Reputation: 59
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?
Upvotes: 3
Views: 15593
Reputation: 2617
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.
Declare your enum
outside of your class you use it in or create it anywhere else outside of a MonoBehaviour
.
Create a script, attach it to your button:
using UnityEngine;
public class GetEnum : MonoBehaviour {
public MyEnum state;
}
3 Add this or change your original function where you use the enum
public void GetEnumState(GetEnum g) {
if (g.state == MyEnum.something)
DoSomething();
}
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:
Upvotes: 5
Reputation: 12965
To make clear how it works what nipunasudha suggest, here the full code example how I use it. Please notice the need of ´SerializeReference´ instad of ´SerializeField´ otherwise all your buttons would end with the same state - which is usually not what you want to achieve.
using UnityEngine;
public class MenuInvoker : MonoBehaviour
{
private enum State { Main, Settings };
[SerializeReference] private State state;
public void InvokeMenu(MenuInvoker currState)
{
switch (currState.state)
{
case State.Main:
Debug.Log("Main");
break;
case State.Settings:
Debug.Log("Settings");
break;
default:
Debug.Log("Default Menu");
break;
}
}
}
Upvotes: 1
Reputation: 4788
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
Upvotes: 2