Hi,
I’m working on a menu, two GUI windows.
The first menu is generated and named by the enumerator types in my ‘Items.cs’ Script.
The second is generating buttons based on the type, so if ‘Notes’ in menu one is clicked, window two will display all items with type ‘Note’.
My problem is that the window two focus control, only works for my drawNotes screen, and the handle window two function only uses the drawNotes elements.
I thought about adding
newArray[i] = bag[i];
inside the for loop, to set all the buttons of the correct type into an array, then use that somehow within my switch statement, to get it to act dependent on the array…
Unfortunately the line above gives me this error :
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[items].set_Item (Int32 index, .items value) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:639)
menu.drawNotes () (at Assets/menu.cs:207)
menu.windowTwoFunc (Int32 id) (at Assets/menu.cs:145)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style)
It’s a big script (in terms of reading it on this site, so I’ll post it below, and i’ll also link to the project, if anyone wouldn’t mind opening it themselves and taking a look if it’s easier )
Here’s the link to the project: It’s just two scripts and a main camera, so the files really small
EDIT: The problem resides in my ‘drawNotes()’ menu always taking charge of the gui.focus , even when I use drawKeys or drawWeapons, the notes holds the focus
Thanks for anyone that helps, I realise theres a lot to sift through possibly
Menu.CS
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class menu : MonoBehaviour {
Rect windowOneRect = new Rect (10, 10, 150, 150);
Rect windowTwoRect = new Rect (200, 10, 350, 150);
List<items> bag = new List<items>();
List<items> noteList = new List<items>();
string[] itemArray;
bool drawWeaponsBool = false;
bool drawNotesBool = false;
bool drawKeysBool = false;
bool showWindowOne = true;
bool showWindowTwo = false;
int windowOneIndex = 0;
int windowTwoIndex = 0;
int tempInt = 0;
void Start () {
itemArray = System.Enum.GetNames(typeof(items.objectType));
bag.Add (new items ("Note01", items.objectType.note));
bag.Add (new items ("Note02", items.objectType.note));
bag.Add (new items ("Note03", items.objectType.note));
bag.Add (new items ("Note04", items.objectType.note));
bag.Add (new items ("Key01", items.objectType.key));
bag.Add (new items ("Key02", items.objectType.key));
bag.Add (new items ("Weapon01", items.objectType.weapon));
bag.Add (new items ("Weapon02", items.objectType.weapon));
}
void OnGUI () {
// Just to check what gets added to the array, save messing in the inspector
if(noteList.Count > 0) {
int y = 100;
for(int i = 0; i < noteList.Count; i++){
GUI.Label (new Rect (Screen.width - 270, y, 150, 25), "New Array " + i + noteList[i].name);
y+=30;
}
}
//Just checking values in the GUI
if (bag.Count > 0) {
GUI.Label (new Rect (Screen.width - 270, 20, 150, 25), "Window One Index = " + windowOneIndex.ToString ());
GUI.Label (new Rect (Screen.width - 270, 50, 240, 25), "Number of items in this catagory: " + (windowTwoIndex + 1 ).ToString());
GUI.Label (new Rect (Screen.width - 270, 80, 240, 25), "WindowTwo index bag.Name = " + bag[windowTwoIndex].name);
if (showWindowOne == true) {
GUI.FocusControl(itemArray[windowOneIndex]);
GUI.Window (0, windowOneRect, windowOneFunc, "Window One");
}
if (showWindowTwo == true) {
GUI.Window (1, windowTwoRect, windowTwoFunc, "Window Two");
}
}
}
void windowOneFunc(int id){
int y = 30;
for (int i = 0; i < System.Enum.GetValues(typeof(items.objectType)).Length; i ++) {
GUI.SetNextControlName(itemArray [i]);
GUI.Button (new Rect (10, y, 100, 25), itemArray [i]);
y += 30;
}
if (Input.GetKeyDown (KeyCode.E)) {
windowOneHandler ();
}
}
void windowOneHandler(){
switch (windowOneIndex) {
case 0:
print("Clicked " + itemArray[windowOneIndex]);
drawNotesBool = true;
drawWeaponsBool = false;
drawKeysBool = false;
showWindowOne = false;
showWindowTwo = true;
return;
case 1:
print("Clicked " + itemArray[windowOneIndex]);
drawKeysBool = true;
drawWeaponsBool = false;
drawNotesBool = false;
showWindowOne = false;
showWindowTwo = true;
return;
case 2:
print("Clicked " + itemArray[windowOneIndex]);
drawWeaponsBool = true;
drawNotesBool = false;
drawKeysBool = false;
showWindowOne = false;
showWindowTwo = true;
return;
default:
print("No option here");
return;
}
}
void windowTwoFunc(int id){
if (drawNotesBool == true) {
drawNotes ();
}
if (drawWeaponsBool == true) {
drawKeys ();
}
if (drawKeysBool == true) {
drawWeapons ();
}
if (Input.GetKeyDown (KeyCode.Backspace)) {
showWindowTwo = false;
showWindowOne = true;
}
if (Input.GetKeyDown (KeyCode.E)) {
windowTwoHandler ();
}
}
void windowTwoHandler(){
switch (windowTwoIndex) {
case 0:
print("Clicked " + bag[windowTwoIndex].name);
return;
case 1:
print("Clicked " + bag[windowTwoIndex].name);
return;
case 2:
print("Clicked " + bag[windowTwoIndex].name);
return;
case 3:
print("Clicked " + bag[windowTwoIndex].name);
return;
default:
print("No option here");
return;
}
}
void drawNotes(){
GUI.FocusControl(bag[windowTwoIndex].name);
int y = 20;
int ammount = 0;
for (int i = 0; i < bag.Count; i ++) {
if(bag[i].enumVar == items.objectType.note){
noteList[i] = bag[i];
GUI.SetNextControlName(bag[i].name);
GUI.Button (new Rect (10, y, 100, 25), bag [i].name);
ammount ++;
y += 30;
}
}
tempInt = ammount;
}
void drawKeys(){
GUI.FocusControl(bag[windowTwoIndex].name);
int ammount = 0;
int y = 20;
for (int i = 0; i < bag.Count; i ++) {
if(bag[i].enumVar == items.objectType.key){
GUI.SetNextControlName(bag[i].name);
GUI.Button (new Rect (10, y, 100, 25), bag [i].name);
ammount ++;
y += 30;
}
}
tempInt = ammount;
}
void drawWeapons(){
GUI.FocusControl(bag[windowTwoIndex].name);
int ammount = 0;
int y = 20;
for (int i = 0; i < bag.Count; i ++) {
if(bag[i].enumVar == items.objectType.weapon){
GUI.SetNextControlName(bag[i].name);
GUI.Button (new Rect (10, y, 100, 25), bag [i].name);
ammount ++;
y += 30;
}
}
tempInt = ammount;
}
void Update(){
//Contains the keyboard input to move through the index's of each window
if (showWindowOne == true) {
if (Input.GetKeyDown (KeyCode.UpArrow)) {
if (windowOneIndex == 0) {
windowOneIndex = itemArray.Length - 1;
} else {
windowOneIndex --;
}
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
if (windowOneIndex == itemArray.Length - 1) {
windowOneIndex = 0;
} else {
windowOneIndex ++;
}
}
}
if (showWindowTwo == true) {
if (Input.GetKeyDown (KeyCode.UpArrow)) {
if (windowTwoIndex == 0) {
windowTwoIndex = tempInt - 1;
} else {
windowTwoIndex --;
}
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
if (windowTwoIndex == tempInt - 1) {
windowTwoIndex = 0;
} else {
windowTwoIndex ++;
}
}
}
}
}
Items.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class items {
// Use this for initialization
public string name;
public enum objectType {
note,
weapon,
key
}
public objectType enumVar;
// Update is called once per frame
public items(string newName, objectType newType){
name = newName;
enumVar = newType;
}
}