- Home /
How do i get texture from GUI box?
Hi, i have an array of flags(245) and i have them in gui box.I need when i click on the box then the script grabs the texture from the box and put them into the material, any idea?
Thanks
using UnityEngine;
using System.Collections;
public class GUIEleveation : MonoBehaviour {
public GUISkin skin;
public string name;
public Texture2D w1, w2, w3, w4, m1, m2, m3, m4;
public Texture2D[] flags;
public Texture2D selectedFlag;
public Vector2 scroll;
public int progress;
void OnGUI () {
GUI.skin = skin;
switch (progress) {
case 0:
// Name Panel
GUI.Box(new Rect(Screen.width /2-200,Screen.height /2-200,400,400),"User Info");
name = GUI.TextField(new Rect(Screen.width/2-150, Screen.height/2-33.3f, 300, 50), name, 20);
if (GUI.Button(new Rect(Screen.width/2-150, Screen.height/2+100, 300, 50), "Next")) {progress = 1;}
break;
case 1:
// Portret Select
GUI.Box(new Rect(Screen.width/2-400,Screen.height/2-300,800,600),"Portret");
GUILayout.BeginArea(new Rect(Screen.width/2-400,Screen.height/2-270,800,600));
GUILayout.BeginHorizontal();
GUILayout.Box(w1,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(w2,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(w3,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(w4,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(Screen.width/2-400,Screen.height/2-50,800,600));
GUILayout.BeginHorizontal();
GUILayout.Box(m1,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(m2,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(m3,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.FlexibleSpace();
GUILayout.Box(m4,GUILayout.Height(190),GUILayout.Width(190));
GUILayout.EndHorizontal();
GUILayout.EndArea();
if (GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,50),"Next")) {progress = 2;}
break;
case 2:
// Flag Selection
GUI.Box(new Rect(Screen.width/2-450,Screen.height/2-300,900,600),"Flag");
GUILayout.BeginArea(new Rect(Screen.width/2-425,Screen.height/2-225,850,450));
scroll = GUILayout.BeginScrollView(scroll);
foreach (Texture2D flag in flags) {
GUILayout.Box(flag);
}
GUILayout.EndScrollView();
GUILayout.EndArea();
if (GUI.Button(new Rect(Screen.width/2-150, Screen.height/2+233.33f, 300, 50), "Next")) {progress = 3;}
break;
case 3:
// Main Menu
GUI.Box(new Rect(Screen.width/2-200, Screen.height/2-150, 400, 250), "Main Menu");
GUILayout.BeginArea(new Rect(Screen.width/2-150, Screen.height/2-75, 300, 150));
if (GUILayout.Button("Start Game", GUILayout.Width(300), GUILayout.Height(50))) {}
GUILayout.FlexibleSpace();
if (GUILayout.Button("Options", GUILayout.Width(300), GUILayout.Height(50))) {progress = 4;}
GUILayout.EndArea();
break;
case 4:
//Options
GUILayout.BeginArea(new Rect(Screen.width/2-350, Screen.height/2-300, 700, 600));
GUILayout.Box("Options", GUILayout.Width(700), GUILayout.Height(600));
GUILayout.EndArea();
break;
}
}
}
Comment
A look at your display code would help with answering this question.
i have to grab the texture from the box i clicked, flags are at line 66
Best Answer
Answer by Jamora · Mar 02, 2014 at 09:42 PM
The best course of action here seems to be to have a button dressed as a box. So your code in the foreach block (line 66) should be
if(GUILayout.Button(flag,"Box")){
selectedFlag = flag;
}