Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by morgan23 · Apr 07, 2014 at 06:19 PM · guienum

Show enum in a loop in on GUI

Hello I want too show my enum on a GUI button for each enum what would be best way of doing this a for loop or a foreach loop?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by perchik · Apr 07, 2014 at 06:23 PM

I'd look at enum.GetNames if you're in C#

The example on that page is pretty much what you want to do, just iterate over all the names and make a button for each one.

EDIT 2: Replaced using System; with using Enum = System.Enum; to avoid using the whole system library

EDIT 1: Here's some code.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Enum = System.Enum;
 public class Test : MonoBehaviour
 {
 
     public enum Shapes { Triangle = 3, Square = 4, Circle = 10 };
     private Dictionary<string, bool> shapeBtnClicks;
 
     void Start()
     {
         shapeBtnClicks = new Dictionary<string, bool>();
         foreach (string name in Enum.GetNames(typeof(Shapes)))
         {
             shapeBtnClicks[name] = false;
         }
     }
 
     void OnGUI()
     {
         foreach (string name in Enum.GetNames(typeof(Shapes)))
         {
             shapeBtnClicks[name] = GUILayout.Button(name);
         }
         
         if (shapeBtnClicks["Triangle"])
         {
             //Do something;
         }
     }
 }
Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image morgan23 · Apr 07, 2014 at 06:34 PM 0
Share

I'm in C# i'm still new to program$$anonymous$$g but i'll try my best and see if it works. would it work if i'm using an index with my enum?

avatar image perchik · Apr 07, 2014 at 06:37 PM 0
Share

Yep. you can loop over the names using GetNames or you can loop over the values(indices) by using GetValues

avatar image morgan23 · Apr 07, 2014 at 08:40 PM 0
Share

I think your answer is right but i'm having problems trying too make it work.

avatar image perchik · Apr 07, 2014 at 09:16 PM 0
Share

Updated my answer with code

avatar image Bunny83 · Apr 07, 2014 at 09:40 PM 1
Share

I would recommend to use System.Enum.GetNames ins$$anonymous$$d of putting a using System; at the top since the System namespace has some collisions with the UnityEngine namespace. Having both in a using statement can cause some issues. The most popular ones are:

  • System.Object <-> UnityEngine.Object

  • System.Random <-> UnityEngine.Random

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Enum option into a game menu gui 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make spaces in between Gui boxes being made in for loops? 1 Answer

How to create an agenda or timetable system? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges