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 CrispyCarry · Dec 17, 2014 at 12:33 PM · buttonstoggleenum

How can I change the position of an enum toggle?

I just seen a thread about toggles and it turned out to be exactly what I was looking for. My question is how can I use an enum on custom toggles?

For example, I have 2 custom toggles and I want make it so that one when box is selected, it will automatically deselect the other box? It is similar to another thread but the other one is for basic toggles.

alt text

These are my buttons, and this is the code for them.

 enum ButtonPressed {Stats, Info};
     ButtonPressed selectedButton;
 
     void OnGUI() {
         
     foreach (ButtonPressed value in ButtonPressed.GetValues(typeof(ButtonPressed))) {
                 if(GUILayout.Toggle (value == selectedButton, value.ToString() + "")){
                     selectedButton = value;
                 }
             }
     
         isStats = GUI.Toggle(new Rect (1460, 521, 110, 35), isStats, "");
         
         isInfo = GUI.Toggle (new Rect (1275, 520, 110, 35), isInfo, "");
                         
         
         
                 }

Ok, now my question again. How can I make the enum count for the two bools I have for the buttons, instead of just making two different buttons?

buttons.png (11.1 kB)
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
0
Best Answer

Answer by SkaredCreations · Dec 17, 2014 at 12:53 PM

If you're not really near the completion of your project and you're using Unity 4.6 then I'd suggest to switch to the new UI tools where you have a ToggleGroup component because it's much easier to manage a visual UI.

Else you need to couple your toggles with the enum, for example:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Test : MonoBehaviour {
 
     enum ButtonPressed {Stats, Info};
 
     class MyButton {
         public ButtonPressed value;
         public Rect rect;
         public GUIContent content;
 
         public MyButton(ButtonPressed value, Rect rect, GUIContent content) {
             this.value = value;
             this.rect = rect;
             this.content = content;
         }
     }
 
     List<MyButton> buttons = new List<MyButton>();
     MyButton selectedButton;
 
     void Start() {
         buttons.Add(new MyButton( ButtonPressed.Info, new Rect (0, 0, 110, 35), new GUIContent("Info") ));
         buttons.Add(new MyButton( ButtonPressed.Stats, new Rect (0, 40, 110, 35), new GUIContent("Stats") ));
     }
 
     void OnGUI() {
         foreach (MyButton button in buttons) {
             if (GUI.Toggle(button.rect, (selectedButton == button), button.content)) {
                 selectedButton = button;
             }
         }
     }
 }
 
Comment
Add comment · Show 5 · 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 CrispyCarry · Dec 17, 2014 at 07:54 PM 0
Share

Sweet thanks, works perfectly. Now what about the ToggleGroup? What does that do exactly?

avatar image SkaredCreations · Dec 17, 2014 at 09:10 PM 0
Share

ToghleGroup does exactly what I did in the code, just assign the toggle group to the toggles. It's part of the new UI tools shipped in the version 4.6. Please mark the answer as accepted so it goes away from queue, if you have enough karma points on UA to do that.

avatar image CrispyCarry · Dec 17, 2014 at 09:20 PM 0
Share

For future reference, do you $$anonymous$$d explaining how to do this? I'm playing around with it right now and I don't really understand it. Does it require scripting? How can I attach the togglegroup script to the GUIToggles made in a different script?

avatar image SkaredCreations · Dec 18, 2014 at 02:52 AM 0
Share

As I already said, ToggleGroup is part of the NEW UI, not UnityGUI :)

avatar image CrispyCarry · Dec 18, 2014 at 03:10 AM 0
Share

So, if it's part of the new UI, am I capable of attaching skins to this "ToggleGroup" I am kind of confused on what you mean. I read the documents for the ToggleGroup and attempted it, I added Toggles and grouped them together, but neither of them would toggle. Should I just make a new question?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using an Enum for GUI.Toggle? 1 Answer

Turn Another UI image on and off with a Single UI Button 1 Answer

Make a Button Behave Like a Toggle 5 Answers

Best way to combine GUI.Toggles and Flaged Enum 1 Answer

Do Something ONLY when all Toggles are On or Off 4 Answers


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