Search Unity

Textarea in androide and js

Discussion in 'Scripting' started by metrickz62, Sep 29, 2014.

  1. metrickz62

    metrickz62

    Joined:
    Sep 29, 2014
    Posts:
    4
    Hey guys,

    I started code 2 weeks ago and now i reached a point were i need a textarea.
    But I have no idea how to do that.
    The textarea should display something like:

    Player hit with 5 dmg.
    Monster hit with 2 dmg.

    and ever 1 second (i got the timer) it add a line
    please help me
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    C# Code: (This script is named textDisplay)

    Code (CSharp):
    1.  
    2. public static string textToDisplay = "";
    3. void OnGUI(){
    4. GUI.Label(new Rect(0,0,Screen.width,Screen.height),textToDisplay);
    5. }
    6.  
    Then if you would like to change the text from another C# script

    Code (CSharp):
    1.  
    2. textDisplay.textToDisplay += "My New Text \n";
    3.  
    The \n is a new line.

    The same can be done in Javascript but someone would have to convert my code for you.
     
    Last edited: Sep 29, 2014
    metrickz62 likes this.
  3. metrickz62

    metrickz62

    Joined:
    Sep 29, 2014
    Posts:
    4
    ok thanks for ur reply. I think i can convert for my self.
    I will try it and post the java script if it work.

    the box:
    Code (JavaScript):
    1. function Start () {
    2.     PlayerPrefs.SetString("textToDisplay", "");
    3. }
    4.  
    5. function OnGUI () {
    6.         GUI.Label(new Rect(Screen.width*0.4f,Screen.height*0.7f ,Screen.width/3,Screen.height/5),PlayerPrefs.GetString("textToDisplay"));
    7. }
    the text:
    Code (JavaScript):
    1. function Update () {
    2.         textToDisplay = PlayerPrefs.GetString("textToDisplay");
    3.         textToDisplay += "Hello World! \n";
    4.         PlayerPrefs.SetString("textToDisplay", textToDisplay);
    5. }
    But my someone can tell me how i can limit the height of the box so it dont went all over the screen.
     
    Last edited: Sep 30, 2014
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    The new Rect() is the size.

    new Rect(x,y,WIDTH,HEIGHT) ;)
     
  5. metrickz62

    metrickz62

    Joined:
    Sep 29, 2014
    Posts:
    4
    u dont say ._. but see: textbox.JPG

    it only should go the half way down.
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Then use Screen.height / 2 for the height.
     
  7. metrickz62

    metrickz62

    Joined:
    Sep 29, 2014
    Posts:
    4
    ... nope:
    Code (JavaScript):
    1. function OnGUI () {  
    2.         scrollPosition = GUILayout.BeginScrollView (
    3.         scrollPosition, GUILayout.Width (Screen.width/2f), GUILayout.Height (Screen.height/4f));
    4.        
    5.         GUILayout.Label(PlayerPrefs.GetString("textToDisplay"));
    6.            
    7.         GUILayout.EndScrollView ();
    8.     }