- Home /
Problem with controlling a 2D array to display strings on GUI.
Hello there, I am a relatively new learning and I got a very bad question that is hard to explain. In one of my demo projects, I am trying to create an onscreen menu where the user can change the displayed language according to what they choose. The language is stored in a 2D arrays, and now I have some problems when I try to display it.
There are 3 main gui screens. The first one is where the user can select a different camera, the second one is just a normal window with text, and the third section contains the buttons to change the language. Here are the codes so that you could play around with it. I apologize for simply naming my variables.
Luckily, I manage to cram all the gui elements into 1 script. The script is in C# and it is named "Test3"
Instructions -Create a new Scene -Add a second camera -Add a empty game object, add the test3 script to it. -In the test3 script: -Change the camera array size to 2, add the two camera
I thought by clicking on one of the language buttons, it would affect an integer variable (called language selector) that would also affect the way that the strings are output into a gui screen. ArrayName[LanguageSelector][Current Index]
Currently, the current index works, the language selector kinda works, but somehow it automatically goes to the last array.
using UnityEngine;
using System.Collections;
public class Test3 : MonoBehaviour {
public Texture2D GUIBackImage;
private Rect DropDownRect;
private Vector2 ListScrollPos;
private bool DropdownVisible;
private int SelectedListItem;
private float BeginAreaY;
private int BoxHeight;
//fields will be used in the Inspector
public Camera[] CameraArray; //drop in the cameras that will be used
public bool ListAbove; //whether to show the drop down list above or below
public GUISkin Skin1CameraSelector; //skining component
//parts for the other popup window
private Rect windowRect= new Rect(Screen.width*0.2f,Screen.height*0.2f, Screen.width*0.5f,Screen.height*0.8f);
string Title1 = ("XXX");
string Content1 =("XXX");
public GUISkin Skin2TextSelector;
Vector2 scrollPosition = Vector2.zero;
//Translating variables
string [][] Title=new string[2][];
string [][] Content=new string[2][];
//0 is for English
//1 is for BM
//The default is 0 for english. //Modifies the first array
private int LanguageSelector=0;
public GUIStyle ButtonStyle;
public GUIStyle DefaultStyle;
// Use this for initialization
void Start () {
LanguageSelector=0;
Title[0] = new string [30];
Title[1] = new string [30];
Content[0]=new string [30];
Content[1]=new string [30];
Title[0][0]="Hello There!";
Title[0][1]="Array0-1!";
//ContentArray[0]="";
Content[0][0]="Container.";
Content[0][1]="Content 2.";
Title [1][0]="Apa Khabar!";
Title [1][1]="Yang hendal!";
Content[1][0]="Array 2";
Content[1][1]="Array 2 index 1";
DropDownRect = new Rect(Screen.width*0f,Screen.height*0f,Screen.width*1f,Screen.height*0.075f);
DropdownVisible = false;
SelectedListItem = 0;
for (int i = 0; i < CameraArray.Length; i++)//disable all cameras
{
CameraArray[i].enabled = false;
}
CameraArray[0].enabled = true;
BoxHeight = CameraArray.Length * 15; //build the dropdown box height
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.skin=Skin1CameraSelector;
// The window can be dragged around by the users - make sure that it doesn't go offscreen.
DropDownRect.x = Mathf.Clamp (DropDownRect.x, 0, Screen.width - DropDownRect.width);
DropDownRect.y = Mathf.Clamp (DropDownRect.y, 0, Screen.height - DropDownRect.height);
if (DropdownVisible)
{
if(ListAbove) { BeginAreaY = DropDownRect.yMin - BoxHeight; } //set dropdown position if above
else {BeginAreaY = DropDownRect.yMin + DropDownRect.height;} //set dropdown position if below
//screen changes the dropdown width size
GUILayout.BeginArea(new Rect(DropDownRect.xMin, BeginAreaY,Screen.width, BoxHeight), "", "box");
ListScrollPos = GUILayout.BeginScrollView(ListScrollPos, false, true);
//changes the button size
GUILayout.BeginVertical(GUILayout.Width(320));
for (int i = 0; i < CameraArray.Length; i++)
{
if (!CameraArray[i].enabled == true && GUILayout.Button(CameraArray[i].name))
{
CameraArray[SelectedListItem].enabled = false; //switch off the previous camera
SelectedListItem = i;//Set the index for our currrently selected item
CameraArray[i].enabled = true; //switch on the chosen camera
DropdownVisible = false; //Hide the list
}
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndArea();
}
GUILayout.BeginArea(DropDownRect, "", "box");
GUILayout.BeginHorizontal();
string SelectedItemCaption = CameraArray[SelectedListItem].name;
string ButtonText = (DropdownVisible) ? "Close" : "Please select a different part";
GUILayout.TextField(SelectedItemCaption);
//slider? 32 20 default the enter button
DropdownVisible = GUILayout.Toggle(DropdownVisible, ButtonText, "button", GUILayout.Width(300), GUILayout.Height(35));
GUILayout.EndHorizontal();
GUILayout.EndArea();
//GUILayout.EndArea();
GUILayout.BeginArea(new Rect( Screen.width*0.86f,Screen.height*0.87f, Screen.width*0.95f,Screen.height*0.95f));
GUILayout.Label("Change Language",ButtonStyle);
if (GUILayout.Button("English"))
Debug.Log("English Clicky-click");
LanguageSelector=0;
if (GUILayout.Button("Japanese"))
Debug.Log("Japanese Clicker");
LanguageSelector=1;
print("Languageselectorvalueis"+LanguageSelector);
print ("current index"+SelectedListItem);
GUILayout.EndArea();
//other part
GUI.skin=Skin2TextSelector;
// Make a popup window
windowRect = GUILayout.Window (0, windowRect, DoControlsWindow, Title[LanguageSelector][SelectedListItem]);
// The window can be dragged around by the users - make sure that it doesn't go offscreen.
windowRect.x = Mathf.Clamp (windowRect.x, 0.0f, Screen.width - windowRect.width);
windowRect.y = Mathf.Clamp (windowRect.y, 0.0f, Screen.height - windowRect.height);
}
void DoControlsWindow ( int windowID ){
// Make the window be draggable in the top 70 pixels.
GUI.DragWindow ( new Rect(0,0, 10000, 70));
//The value changes the size of the label/box, GUILayout.MaxHeight(710)
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.MaxHeight(590), GUILayout.ExpandHeight (false));
GUILayout.Label (Content[LanguageSelector][SelectedListItem],GUILayout.ExpandHeight (true));
GUILayout.EndScrollView();
}
}
Answer by Owen-Reynolds · Feb 08, 2012 at 03:31 PM
Basic programming error -- you forgot the {}'s for the if. There's a special rule (in just about all languages) that when you leave them out, only the next statement is part of the if. In the code below, Debug
is in the if, but the Lang=
line will always run:
if (GUILayout.Button("Japanese"))
Debug.Log("Japanese Clicker");
LanguageSelector=1; // indented, but not part of the if
Most people aren't going to read that much code -- I skimmed for 6 seconds and happened to spot it. Often helpful to trim it down (comment out sections, maybe) and test the smallest thing that causes the error. Most times you'll solve it yourself that way, and feel special. If not, you've got something short that more people will be willing to take a look at.
Answer by calvintang · Feb 08, 2012 at 07:09 PM
Oh thanks! I was looking at the examples on how to make the button, and I thought that the curly brackets are only needed for Javascript, not C#!
From here: http://unity3d.com/support/documentation/ScriptReference/GUILayout.Button.html
Thanks a bunch Owen!
Your answer
Follow this Question
Related Questions
Buttons within Scroll View not able to communicate 2 Answers
Slider & Button Interaction Problem 0 Answers
Event trigger for instantiated objects 0 Answers
Making texture cover whole button 1 Answer
GUI.DrawTexture on GUI.Button press 1 Answer