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 Jepz · Aug 26, 2014 at 09:00 PM · buttonfocus

Focus button with gui.controlfocus

I have i my List "_lootitems" where i add gameobjects to, when i rightclick on the object, which then adds a button to my inventory. But when i try to focus the first button, things goes wrong... Can anyone plz tell me how i can focus a sertain (the first) button when tab is pushed? i want the focus to go the the next button when tab is pressed again.

here is my code:

  using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class Collect_Inventory : MonoBehaviour {
         
         //raycast
         public float Range = 20f;
         
         //Inventory    
         private bool _DisplayInventoryWindow = true;
         private const int Inventory_Window_ID = 1;
         private Rect _InventoryWindowRect = new Rect(10f,10f,300,290f);
         private static int _InventoryRows =4;
         private static int _InventoryCollums = 3;
         private int _InventoryCheck = _InventoryRows * _InventoryCollums;
         private bool InvFull = false;
     
     
         //Store Item - modify Arms and Legs later, into feet/Hands, unter/upper - Arm/Leg
         public List<GameObject> _LootItems = new List<GameObject> ();
         public List<string> ItemNames = new List<string> ();
         public List<bool> ButtonSelected = new List<bool> ();
     
         //Button Prop
         private float ButtonWidth = 40f;
         private float ButtonHeight = 40f;
         private float _offset = 1.1f;
     
     
         // Use this for initialization
         void Start () 
         {
             ButtonSelected = new List<bool> (ItemNames.Count);
         }
         
     
         void Update () 
         {
     
                     //inventory
                     if (Input.GetKeyUp (KeyCode.Alpha1) && _DisplayInventoryWindow == true)
                             _DisplayInventoryWindow = false;
                     else
     
             if (Input.GetKeyUp (KeyCode.Alpha1) && _DisplayInventoryWindow == false)
                             _DisplayInventoryWindow = true;
     
     //        if (Input.GetKeyDown (KeyCode.Tab) && _DisplayInventoryWindow == true) {
     //            int counter = 0;
     //                        for (int i = 0; i < _LootItems.Count; i++) {
     //                                if (GUILayout.Toggle (counter == i, _LootItems [i].name, "Button"))
     //                                        counter = i;
     //                        }
     //                }
     //        if (Input.GetKeyUp (KeyCode.Q) && _DisplayInventoryWindow == true)
     //        _LootItems.Remove(den der er markeret)
                     
                     // raycast
                     if (Input.GetMouseButtonDown (1) && InvFull == true){
                              
                               print ("Inventory is Full!!\n" +
                                     "Drop some Items for more space");
                 return;
             }
             if (Input.GetMouseButtonDown (1) && InvFull==false) {
     
                             
                             Ray ray = new Ray (Camera.main.transform.position + Camera.main.transform.forward, Camera.main.transform.forward);
                             RaycastHit HitInfo;
     
                                                                                     
                 if (Physics.Raycast (ray, out HitInfo, Range) && HitInfo.transform.transform.tag == "Robot") {
                                 GameObject go = HitInfo.collider.transform.parent.gameObject;
                     int nbr4 =0;
                     nbr4++;
                             ItemNames.Add(nbr4.ToString());
                             Destroy (go);
                             _LootItems.Add(go);
                             }
                     }
         }
     
     
         //Loot Window:
         void OnGUI()
         {
     
     
             if(_DisplayInventoryWindow == true)
             _InventoryWindowRect = GUI.Window (Inventory_Window_ID, _InventoryWindowRect, InventoryWindow, "Inventory"); 
             
         }
     
          
          void InventoryWindow (int id)
         {                    
             int nbr = -1, nbr2 = 0, nbr3 = 0;
     
                     foreach (GameObject go in _LootItems) 
         {
                 if(_LootItems.Count != nbr3)
             {
                 nbr++;
                 nbr3++;
                 
     
                                     if (nbr == _InventoryCollums) {
                                             nbr -= _InventoryCollums;
                                             nbr2++;
                                     }
                     if (_LootItems.Count >= 0)
                     {
                         for(int i = 0; i < _LootItems.Count; i++) 
                         {
                         GUI.SetNextControlName(ItemNames[i]);
                         GUI.Button (new Rect (_offset * (nbr * ButtonWidth), 20 + _offset * (nbr2 * ButtonHeight), ButtonWidth, ButtonHeight), nbr3.ToString());
     
                                 if (Input.GetButton("tab")) 
                             {
                                 GUI.FocusControl(nbr3.ToString());
     
                             }
     
                         }
                     }
     
             }
                                         
                         if(nbr3 == _InventoryCheck)
                         {
                         InvFull = true;
                         }
     
         }
                             
     
                             for (int y = 0; y < _InventoryRows; y++)
                                     for (int x = 0; x < _InventoryCollums; x++)
                                             GUI.Label (new Rect (_offset * (x * ButtonWidth), 20 + _offset * (y * ButtonHeight), ButtonWidth, ButtonHeight), "", "box");
     
             return;
         }
     }



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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

UI Element Got & Lost Focus Handling 2 Answers

Focus chat input field when pressing enter 2 Answers

Easy way to make buttons in-accessible when under a pop-up window? 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer

How can I create a editor button that can not be selected 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