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 calvintang · Feb 08, 2012 at 02:26 PM · guibuttononguievents2d array

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();
     
 
 }
     
 
         
 
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
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
avatar image
0

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!

Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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