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 Kruggerand · Apr 21, 2011 at 12:24 AM · guigridguistylehoverselectiongrid

SelectionGrid layout and style issues

I'm trying to make a Level Select screen for my game using GUI.SelectionGrid but I'm having a couple problems:

  1. I applied a GUIStyle to my SelectionGrid which includes a "hover" style. This works fine EXCEPT on the very first element of the grid, which doesn't change color when hovered over (this is the same with "active" as well as "hover")

  2. It seems that when I click, even if I don't click on one of the grid buttons, it registers the nearest one as active, so I could click in the very far corner but it will think I'm clicking on one of the buttons

Code:

using UnityEngine; using System.Collections;

public class LevelSelect : MonoBehaviour { private int levelCount; private int selected = -1; private string[] grid; public GUIStyle style;

 void Start()
 {
     levelCount = Application.levelCount-2;
     grid = new string[levelCount];
     for(int i = 0; i < levelCount; i++)
     {
         grid[i] = "Level " + i; 
     }   
 }

 void OnGUI() {

     selected = GUI.SelectionGrid(new Rect(0,0, Screen.width, Screen.height), 0, grid, 5, style);
     if(Input.GetMouseButtonUp(0))
     {
         Application.LoadLevel(selected+2);
     }       
 }

}

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 ziomagica · Jul 27, 2012 at 03:18 AM

Might be outdated seeing how the question is months old, but hope this helps.

1) I'm guessing you initialized the value of selected to 0 (the first element), thereby setting your first element as the selected element. The way SelectionGrid seems to be implemented, the currently selected element will not react to the "hover" defined in your GUIStyle (someone correct me if I'm wrong). Try selecting another element, then check to see if your first element now properly shows "hover" effects (if it still doesn't, I'm out of ideas).

2) The Input.GetMouseButton function is a global input handler, not specific to your active GUI component, hence the behaviour you are getting.. Because of the way GUI.SelectionGrid is called, we cannot directly define a handler for it.

Might not be the best way to go about it, but this is how I did it previously:

 void OnGUI() {
     int prevSelection = selected;
     selected = GUI.SelectionGrid(new Rect(0, 0, Screen.width, Screen.height), 0, grid, 5, style);
 
     if (prevSelection != selected) {
         Application.LoadLevel(selected+2);
     }
 }
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

1 Person is following this question.

avatar image

Related Questions

Styling an individual button in SelectionGrid 0 Answers

detect mouseover with grid buttons? 1 Answer

Selectiongrid formatting with guistyle.wordwrap 1 Answer

Hover state doesn't work on slider with custom styles 0 Answers

Whats wrong with my GUI.Toggle? 2 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