- Home /
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
Your answer
Follow this Question
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