Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TinyTurnip1 · Nov 13, 2021 at 04:58 AM · uibuttonmenuui imagebuttonstates

How to Change Menu Button Rollover States with GameObjects

Hello!

My goal is to have 4 different GameObjects that are SetActive(true); or SetActive(false); based on when the button is pressed or when it is rolled over with the mouse. They are all UI Images.

alt text


I currently have this working for 3 States (Normal, Normal-Rollover and Selected) in my script that I have pasted below but I can't figure out how to add the extra 4th State being "Selected-Rollover".

What I want to add to the code:
if Normal = true then the rollover image should be Normal-Rollover
else if
Selected = true then the rollover image should be Selected-Rollover



This is my Button Controller script:

 public class ButtonController : MonoBehaviour
 {
 
     public GameObject normal;
     public GameObject selected;
 
     public GameObject normalRollover;
     public GameObject selectedRollover;

     public void ActiveButton()
     {
         // Sets the button to Selected when you click on it
         selected.SetActive(true);
         normal.SetActive(false);
     }
 
     public void DeActiveButton()
     {
         // Sets the button back to Normal when you click on another Button
         normal.SetActive(true);
         selected.SetActive(false);
     
     }
 
     public void OnPointerEnter()
     {
         normalRollover.SetActive(true);
         normal.SetActive(false);
     }
 
     public void OnPointerExit()
     {
         normal.SetActive(true);
         normalRollover.SetActive(false);
     }
 
   



This is my GameManager script

     public Button[] panelHatButtons;

     // NORMAL ROLLOVER 

     public void PanelHatPointerEnter(int buttonNum)
     { // Gets sent the int index of the button from the OnPointerEnter Event Trigger
         for (int i = 0; i < panelHatButtons.Length; i++)
         { // Loop through the array buttons
             if (i == buttonNum)
             { 
                 panelHatButtons[i].GetComponent<ButtonController>().OnPointerEnter(); 
             }
 
         }
     }
 
     public void PanelHatPointerExit(int buttonNum)
     { // Gets sent the int index of the button from the OnPointerExit Event Trigger
         for (int i = 0; i < panelHatButtons.Length; i++)
         { // Loop through the array buttons
             if (i == buttonNum)
             { 
                 panelHatButtons[i].GetComponent<ButtonController>().OnPointerExit();
             }
 
         }
     }

     // BUTTON SELECTED 

     public void PanelHatButtonPressed(int buttonNum)
     { // Gets sent the int index of the button from the OnClick()
         for (int i = 0; i < panelHatButtons.Length; i++)
         { // Loop through the array buttons
             if (i == buttonNum)
             { /
                 panelHatButtons[i].GetComponent<ButtonController>().ActiveButton(); 
             }
             else
             { // This is every other button
                 panelHatButtons[i].GetComponent<ButtonController>().DeActiveButton();
             }
         }
     }


Any advice/tips you could offer would be super appreciated! :-) Thank you so much!

@evskii Hello! I hope you don't mind me tagging you, I thought I would since I'm using the script you helped me with a few days ago and I have just attempted to add Rollover states to it. I got stuck on this part. Thank you!

button-in-hierarchy.jpg (14.9 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

Answer by TinyTurnip1 · Nov 13, 2021 at 11:40 AM

Okay! I managed to get it to work with a lot of fiddling but I'm not sure if this is the best way to do it.

 public class ButtonController : MonoBehaviour
 {
     // --------------------------------------------------------------------- 
     //  VARIABLES                               
     // ---------------------------------------------------------------------
 
     public GameObject normal;
     public GameObject normalRollover;
 
     public GameObject selected;
     public GameObject selectedRollover;
 
     // This is only on the Panel Buttons
     public GameObject panelTick;
 
     // bools
     public bool buttonIsSelected;
 
 
     // --------------------------------------------------------------------- 
     //  METHODS                            
     // ---------------------------------------------------------------------
 
     void Start()
     {
         buttonIsSelected = false;
     }
 
 
     //  Method for Selected & Unselected State (TopBar and the Panel)
 
     #region Button Press Active + Deactive Methods
     public void ActiveButton()
     {
 
 
         if (buttonIsSelected == false)
         {
             // Sets the button to Selected when you click on it
        
             selected.SetActive(true);
             normal.SetActive(false);
             normalRollover.SetActive(false);
 
             buttonIsSelected = true;
         }
         else if (buttonIsSelected == true)
         {
             
             normalRollover.SetActive(false);
             normal.SetActive(false);
             
         }
 
 
     }
 
     public void DeActiveButton()
     {
        
         normal.SetActive(true);
         selected.SetActive(false);
 
         buttonIsSelected = false;
 
     }
 
     #endregion
 
     // Method for Rollover State
 
     #region Button Rollover Methods
     public void OnPointerEnter()
     {
 
         if (buttonIsSelected == true)
         {
             
             selectedRollover.SetActive(true);
             normal.SetActive(false);
             selected.SetActive(false);
         }
         else
         {
            
             normalRollover.SetActive(true);
             normal.SetActive(false);
         }
 
 
     }
 
     public void OnPointerExit()
     {
         if (buttonIsSelected == true)
         {
             
             selectedRollover.SetActive(false);
             selected.SetActive(true);
             normal.SetActive(false);
         }
         else
         {
             normalRollover.SetActive(false);
             normal.SetActive(true);
         }
     }
Comment
Add comment · 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

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

238 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change Menu Buttons to a Selected Image and Deselected Image 2 Answers

Displaced UI RectTransform collider in some resolutions issue. 1 Answer

if button is highlighted run script (non-mouse) 0 Answers

I need to alter each child object individually 1 Answer

How to make a menu controled from Keyboard and using buttons? 0 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