Search Unity

A begginer question

Discussion in 'Scripting' started by SirMarley, Jul 29, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Guys...

    A simple question that probably a few of you had when you first started...

    If I have a Player.Prefs storing an option for the languaje selection...(i.e English / Spanish) and after that selection I have to display 2 GUI buttons, let´s say YES / NO vs SI / NO; after I define the button (if (GUI.Button, etc), where I have to put the text that is going to be inside of it, can I open a {, put an if and the var with the Player.Prefs stored there? or it has to be placed prior to the GUI button instance? (does it make sense?)

    Thanks!
     
  2. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    Can you try that again , Im not entirely sure what your trying to after the language selection
     
  3. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Sure... I want to know if somethin like this is possible:

    Code (CSharp):
    1. If (GUI.Button(new Rect(0,0,10,10), {
    2. if (lSel ==1)"SOUND";
    3. if (lSel ==2)"SONIDO";
    4. }))
    5. {}
    6.  
    (lSel is the var where I have stored the PlayerPrefs)

    Instead of if (lSel ==1) >> define GUI.Button
    And if (lSel ==2) >> define GUI.Button again
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Localisation should be data focused, not hardcoded. If you had a localisation system which loaded a file, with a key string and a key value pair, you use that to set the string.

    LocalisationSystem.GetNamedString(mySoundStringKey);

    Obviously writing that system will be a lot of work, but it's well worth it. Plus, you can then use it for any other game you make
     
  5. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    I +1 what A.Killingbeck is saying. To expand on that I'd create two XML files that holds string information in fragments

    Example:

    GameName_UI_ENG.xml
    <fragment fragmentId="startGame">
    <text>Start Game</text>
    </fragment>

    GameName_UI_ESP.xml
    <fragment fragmentId="startGame">
    <text>Iniciar Juego</text>
    </fragment>

    then write a method that pulls data from XmlDocument.
     
  6. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    are you trying to select and language and then a yes and No buttons to either confirm selection or close and select again??
     
  7. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    I`m C# only but this is what I would do , but again im not sure exactly what your trying
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LanguageSelectionExample : MonoBehaviour {
    5.  
    6.     int              languageSelection;
    7.  
    8.     bool             drawSpanish = false, drawEnglish = false;
    9.  
    10.     const string     _myLanguage  = "myLanguage";
    11.  
    12.     public GUIStyle  englishLanguageSelectionButton,spanishLanguageSelectionButton;
    13.  
    14.  
    15.     void Start () {
    16.  
    17.    languageSelection  = PlayerPrefs.GetInt ("myLanguage",0);
    18.  
    19.         if (languageSelection == 2) {
    20.             drawEnglish = true;
    21.         } else {
    22.             drawEnglish = false;
    23.         }
    24.        
    25.         if (languageSelection == 3) {
    26.             drawSpanish = true;
    27.         } else {
    28.             drawSpanish = false;
    29.         }
    30.  
    31.    
    32.     }
    33.  
    34.     void Update () {
    35.  
    36.         }
    37.  
    38.  
    39.  
    40.     void OnGUI()
    41.  
    42.     {
    43.  
    44.         if (Application.loadedLevel == 0)
    45.        
    46.         {
    47.             GUI.skin.button = englishLanguageSelectionButton;
    48.             if (GUI.RepeatButton (new Rect (Screen.width / 2.75f, Screen.height / 5f, Screen.width / 3.8f, Screen.height / 6.3f), "")) {
    49.                
    50.                 PlayerPrefs.SetInt ("myLanguage", 2);
    51.                 PlayerPrefs.Save ();
    52.                 Application.LoadLevel (2);
    53.                
    54.             }
    55.  
    56.             GUI.skin.button = spanishLanguageSelectionButton;
    57.             if (GUI.RepeatButton (new Rect (Screen.width / 2.75f, Screen.height / 1.5f, Screen.width / 3.8f, Screen.height / 6.3f), "")) {
    58.                
    59.                 PlayerPrefs.SetInt ("myLanguage", 3);
    60.                 PlayerPrefs.Save ();
    61.                 Application.LoadLevel (3);
    62.             }
    63.  
    64.         }
    65.  
    66.         if (Application.loadedLevel == 1)
    67.         {
    68.             if (drawEnglish == true)
    69.             {
    70.  
    71.             }
    72.            
    73.             if (drawSpanish == true)
    74.             {
    75.  
    76.             }
    77.  
    78.  
    79.         }
    80.  
    81.  
    82.     }
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102. }
    103.  
     
  8. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Dublinjonny,

    I believe he's trying to implement a feature to change the language on the entire application and asking for help on how he would handle hot-swapping the text from language to language.

    Moreover, your reply is still an example of hardcoding localization handlers. Not to mention being a nightmare to add or remove languages. A more practical example would be to load up a language file dynamically:

    Code (CSharp):
    1.  
    2. //Be sure to import xml libs----
    3. using system.xml;
    4. //------------------------------------------
    5.  
    6. XMLDocument LangFile;
    7.  
    8. void Start(){
    9.     //Make sure you can load the correct language file needed for the application
    10.     if(bGetLangConfig()){
    11.         //Continue with other stuff
    12.     }
    13. }
    14.  
    15. bool bGetLangConfig(){
    16.     try{
    17.         string sysLang = PlayerPrefs.GetString("Language");
    18.  
    19.         //Load the language file
    20.         LangFile =  XmlDocument.Load("[Resources]/languageConfigs/"+sysLang+".xml");
    21.     }catch(err){
    22.        //stuff
    23.     }
    24. }
    25. string getText(string nodeName){
    26.     if(LangFile != null){
    27.        return LangFile.GetElementById(nodeName);
    28.     }else{
    29.         return "";
    30.     }
    31. }
    32.  
    33. void OnGUI(){
    34.     GUI.Button(someRect,getText("startGame"));
    35. }
    36.  
    It's worth noting that I skipped some necessary xml steps pertinent to loading and reading docs to save on time and space. But I assume you can just fish around the MSDN to find them.
     
    Last edited: Jul 29, 2014