create a box that player can type in it

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

GUILayout.TextArea for multi-line input boxes:

GUILayout.TextField for single-line input boxes:

===========

string inputString = "";
int lengthOfField = 30;

function OnGUI(){
     inputString = GUILayout.TextField(inputString, lengthOfField);
}

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.

1 Like

thanks but how i can send the text to unity as a variable ?

“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.

you can use playerprefs to easily save and load a variable

for example -

    string inputString = "";
    int lengthOfField = 30;
     
    function OnGUI(){
         inputString = GUILayout.TextField(inputString, lengthOfField);
    }
     
     function Update() {
        
           PlayerPrefs.SetString("input text",inputString);
     
}

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-

string PlayerName;

function Start() {

   PlayerName = PlayerPrefs.GetInt("input text");
  print PlayerName;

}

This should display the name the player had entered before.

1 Like