Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

create a box that player can type in it

Discussion in 'Immediate Mode GUI (IMGUI)' started by saeed_cpu_full, Sep 1, 2010.

  1. saeed_cpu_full

    saeed_cpu_full

    Joined:
    Jun 15, 2010
    Posts:
    15
    Hi
    How i can create a box with a button that player can type in box and if player click on the button all of text send in unity as a variable ?
    sorry for my english
     
  2. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    GUILayout.TextArea for multi-line input boxes:

    http://unity3d.com/support/documentation/ScriptReference/GUILayout.TextArea.html

    GUILayout.TextField for single-line input boxes:

    http://unity3d.com/support/documentation/ScriptReference/GUILayout.TextField.html

    ===========

    Code (csharp):
    1. string inputString = "";
    2. int lengthOfField = 30;
    3.  
    4. function OnGUI(){
    5.      inputString = GUILayout.TextField(inputString, lengthOfField);
    6. }
    Input string will always be equal to whatever is in the text box. If you want to make a read-only text box just omit the "inputString = ." Not what you want to do right now, but it may be useful later in situations where you want the user to be able to easily copy-paste something but not alter it.
     
  3. saeed_cpu_full

    saeed_cpu_full

    Joined:
    Jun 15, 2010
    Posts:
    15
    thanks but how i can send the text to unity as a variable ?
     
  4. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    "inputString" is the variable.

    inputString = GUILayout.TextField(inputString, lengthOfField);

    What this does is constantly set the value of "inputString" to whatever is typed in the box every frame. You can access/pass that variable the same way you would any string variable.
     
  5. thethunder

    thethunder

    Joined:
    Aug 28, 2013
    Posts:
    1
    you can use playerprefs to easily save and load a variable

    for example -

    Code (csharp):
    1.     string inputString = "";
    2.     int lengthOfField = 30;
    3.      
    4.     function OnGUI(){
    5.          inputString = GUILayout.TextField(inputString, lengthOfField);
    6.     }
    7.      
    8.      function Update() {
    9.        
    10.            PlayerPrefs.SetString("input text",inputString);
    11.      
    12. }
    13.      
    14.  
    This saves the string the player has typed in as a variable by the name of "input text". You can now call it in any other script1

    for example-

    Code (csharp):
    1. string PlayerName;
    2.  
    3. function Start() {
    4.  
    5.    PlayerName = PlayerPrefs.GetInt("input text");
    6.   print PlayerName;
    7.  
    8. }
    This should display the name the player had entered before.
     
    Last edited: Dec 27, 2013