This is a bit old, but I had this problem just right now, so I’ll share my solution to it:
To avoid the problems of the upcast from ints, like the compatibility with other codes, the requirement of keep tracking the meaning of each number, or the problem if the enum get modified in the original order of the elements; and to avoid add another MonoBehaviour for each of the elements/buttons I have to create (so don’t overload the Unity engine with more starts/updates than those I need) using the “class with public enum property” solution, I’ve done another function as a wrap on the script to call for each of the enum options. In this way, the button only have to call the function for his own enum option, and add any other parameter if you need it too, ofc:
public void FuncWithEnum(EnumType enumType) {
switch (enumType) {
case EnumType.Value1:
//Do something
break;
case EnumType.Value2:
//Do something
break;
}
}
public void FuncWithEnumValue1() => FuncWithEnum(EnumType.Value1);
public void FuncWithEnumValue2() => FuncWithEnum(EnumType.Value2);
And now you can select it without problems:
The only problem with this it’s you have to modify the class and add a new function every time you add some new value to the enum, but at least that’s all and there is no problems of retro-compatibility with the rest of the code.
I just going to post here for fun but you can try to change the button instead.
//this is when the player hover on a button, it will play a hover clip.
public class CustomButton : MonoBehaviour , IPointerEnterHandler , IPointerDownHandler
{
[SerializeField] private SFXClip clipToPlayWhenClick;
[SerializeField] private SFXClip clipToPlayWhenHovered;
public UnityEvent actions;
//when the player hover on the button, play the hover clip
public void OnPointerEnter(PointerEventData eventData)
{
SoundManager.Instance.PlayAudio(clipToPlayWhenHovered);
}
//this is special case for the button for the connecting button.
public void ClickSound()
{
SoundManager.Instance.PlayAudio(clipToPlayWhenClick);
}
public void OnPointerDown(PointerEventData eventData)
{
ClickSound();
StartCoroutine(PlayingButtonBeforeActions());
}
private IEnumerator PlayingButtonBeforeActions()
{
yield return new WaitForSecondsRealtime(1);
actions?.Invoke();
}
}
That way in your inspector, you can still use Enums.
I think that this question is worth to be answered because enums can be very useful in some cases. Certainly it’s better then to just use a string as param and then make a typo that causes problems.
This is the way I do it:
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;
}
}
}