Search Feedback

1108
votes

Allow Enum's with onclick buttons

GUI

-

-

When you create an enum and make it public like:
public enum CURRENT_MENU_SELECTION {Tower, Base, Special, Stats, Options, Purchase};

you are able to change the enum on a gameobject...why can't this be possible on a button with something like this with switch statements:
public void ChangeMenu(CURRENT_MENU_SELECTION e)
it would be nice so i can assign tons of buttons at once with the same function and just change the enum.

much faster than what we have to do now..you have to create each function like so and add them individually:
public void TowerMenu()
public void BaseMenu()
public void SpecialMenu()
public void StatsMenu()
public void OptionsMenu()
public void PurchaseMenu()

Comments (22)

  1. 46fb8ecccda88b6ef14aa9e89841349c?d=mm

    jeongjinwook

    Mar 21, 2018 03:20

    Yes I think Unity must this function

  2. 8052e3f3e50be6d55ab439bb118202dd?d=mm

    Samacvuk

    Feb 19, 2018 23:48

    I was baffled today in finding out this is not a feature already... it is insane to think that in 2018 unity have not yet caught up with game development conventions of the 90's

  3. C2778d4182b1b39798b1e8e74dd2e143?d=mm

    Fydar

    Aug 24, 2017 10:59

    TobilaForge, you can use ScriptableObjects.

  4. Adaa67dfc10eb722a29fb03da5495257?d=mm

    TobiLaForge

    Apr 21, 2017 09:40

    Just found a simple workaround (if you don't have too much enums):
    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 of course need a new MonoBehaviour/script for each Enum you use in that way.

  5. B713f430622d37a8a2e7e726a8d04d74?d=mm

    Costiome

    Apr 19, 2017 09:47

    Totally what i am looking for today, this will be very useful.

  6. B6a02b44fc22e74e2f1bb0fe25da6971?d=mm

    mgeorgedeveloper

    Apr 05, 2017 13:27

    Yes please - the whole point of enum is type safety and to reduce the possibility of introducing errors due to typos or just not remembering that 1 = Dog and 2 = Cat. That's why we don't have strings "Monday", "Tuesday" and "Wednesday" in our code - but instead use named int values, i.e. enums.

    I don't want to pass strings or ints to OnClick events and then cast them to the typesafe enum. This is just asking for errors to creep in.

  7. F3aae3f8c0055782339651135a803c0c?d=mm

    timuz

    Feb 12, 2017 09:02

    Really good to have it..

  8. 3cbf274fa13971491fb6eea6c88db922?d=mm

    llamagod

    Jan 20, 2017 17:11

    You could try this: https://www.assetstore.unity3d.com/en/#!/content/79135
    It allows that and much more. It also enables you to display and serialize polymorphic types, dictionaries, properties, generic types, HashSets and delegates.

  9. 1b2d9c872ab3cc6d6200eecd9d259378?d=mm

    Lhg

    Jan 16, 2017 13:39

    Simple and powerful feature, very handy! Waiting on this :)

  10. 98a7e5b20b6224f46b0fe184961c63e1?d=mm

    Khawar Munir

    Apr 23, 2016 07:02

    Yes, It would be a great feature. It would save a lot of time.

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(9911)

2D

(260)

Ads

(50)

AI & Navigation

(77)

Analytics

(115)

Animation

(366)

Asset Store

(271)

Assets

(510)

Audio

(169)

Cloud Build

(122)

Collaborate

(45)

Docs & Tutorials

(217)

Editor

(2293)

Everyplay

(13)

Game Performance Reporting

(16)

General

(890)

Graphics

(821)

GUI

(397)

Input

(157)

Licensing

(84)

Networking

(176)

Physics

(362)

Platforms

(427)

Profiling & Optimization

(76)

Runtime

(171)

Scripting

(1097)

Terrain

(164)

WebGL

(138)