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 Tim-A · Aug 14, 2012 at 06:08 PM · javascriptguigrid

GUI Grid click

I have a script witch is

var selGridInt : int = 0; var selStrings : String[] = ["Empty", "Empty", "Empty", "Empty"];

function OnGUI () {

  if (Input.GetKey("tab"))
   
     selGridInt = GUI.SelectionGrid (Rect (1000, 10, 300, 100), selGridInt, selStrings, 2);

}

and I want it to be able to enable mesh renderer on a sword called "Broadsword" when i click one of them. Any help would be greatly appreciated. Thanks.

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
1

Answer by Paulo-Henrique025 · Aug 14, 2012 at 07:15 PM

 void OnGUI()
 {
      int valueYouWant;
 
      int storeIndex;
 
      selGridInt = GUI.SelectionGrid (Rect (1000, 10, 300, 100), selGridInt, selStrings, 2);

      if(storeIndex != selGridInt)
      {
            storeIndex = selGridInt;
            if(selGridInt == valueYouWant)
            {
                 //DoWhatYouWant
            }
      }
 }

If your selGrindInt has changed check to see if it is the item you want and perform something

Edit: The complete working copy

 using UnityEngine;
 using System.Collections;
 
 public class DrawGrid : MonoBehaviour {
 
  public int selGridInt = -1; // The indexer of the grid
  public string[] selStrings; // Itens of the grid
  private int storeIndex = -1; // Store the actual value to check if there was some change in the grid
  public int valueYouWant; // The value you are aiming for, if the grid index match this something must happen!
  
  void Start()
  {
  selGridInt = -1; // Start the grid with nothing selected
  }
  
  void OnGUI()
     {
  selGridInt = GUILayout.SelectionGrid(selGridInt, selStrings, 2); // The grid using GUYLayout so you dont have to define a Rect, if you want to use Rect just use GUI.SelectionGrid
 
      if(storeIndex != selGridInt) // If the grid index and the value we were holding are diferent it means there was some change in the grid
      {
            storeIndex = selGridInt; // The store now receives the grid index, so the next time OnGUI is called the if statement just above will no be true and this line will no be executed, this way the code is executed only once
            if(selGridInt == valueYouWant) // If the grid index is the value you want do something
            {
                 Debug.Log("lol");
            }
      }
  
      }
 }

Just keep in mind that the SelectableGrid does not behave like a button. If you are looking for a button like behavior just use button. If you think drawing lots of buttons seems hard and boring try to use GUILayout or even a for/foreach routine to draw them for you.

Comment
Add comment · Show 6 · 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 Tim-A · Aug 14, 2012 at 11:25 PM 0
Share

What Language is that C# or Javascript.

avatar image Paulo-Henrique025 · Aug 14, 2012 at 11:36 PM 0
Share

its C#, you can identify by the the return type beside OnGUI, void means that no value is returned, in JS just function would work.

Looking more closer I've notice that part of my code was pasted from yours, which is JS. The correct call of the function would be: GUI.SelectionGrid (new Rect (1000, 10, 300, 100), selGridInt, selStrings, 2); The new was missing.

avatar image Tim-A · Aug 14, 2012 at 11:46 PM 0
Share

Sorry I'm a bit confused about the second part of your comment. "The correct call of the function would be: GUI.SelectionGrid (new Rect (1000, 10, 300, 100), selGridInt, selStrings, 2)"

sorry I'm kinda new to scripts. What does that mean.

avatar image Paulo-Henrique025 · Aug 15, 2012 at 12:03 AM 0
Share

While using C# you need to use a keyword called "new" to create an object, you cant create them out of nothing like in JS. Thats why the keyword "new" was missing before the type Rect, the code would never work in a .cs script. I'll edit the answer with the correct code.

avatar image Tim-A · Aug 15, 2012 at 12:07 AM 0
Share

Thanks for everything you've done.

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

8 People are following this question.

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

Is this a problem of unity 3 or i am doing something wrong. 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

How to display textures that are in an array? 1 Answer

I want a HitMark(GUI) system like in Crysis-CoD that indicates where i am hit from and also a Camera/Screen shake effect 3 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