Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Change Cursor in TextArea?

Discussion in 'Scripting' started by tcontrada, Jan 21, 2011.

  1. tcontrada

    tcontrada

    Joined:
    Dec 2, 2010
    Posts:
    19
    I would like to use a different cursor in the TextArea GUI control. For example instead of the pipe character I would like to use "_" instead. How can I do this?

    Also, is there a way to programatically position the cursor to the end of the text in the textarea?

    Thanks,
    Tony
     
  2. .Samuel.

    .Samuel.

    Joined:
    Jan 20, 2011
    Posts:
    11
    I guess you have to create the behavior yourself by using Input.InputString and a GUI Text object.

    if I tweak the example code from Input.InputString it would look like this.

    Code (csharp):
    1.  
    2. function Update () {
    3.     for (var c : char in Input.inputString) {
    4.         // Backspace - Remove the last character
    5.         if (c == "\b"[0]) {
    6.             if (guiText.text.Length != 0)
    7.                 guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
    8.         }
    9.             guiText.text += "_";// this would add the underscore to end
    10.  
    11.         // End of entry
    12.         else if (c == "\n"[0] || c == "\r"[0]) {// "\n" for Mac, "\r" for windows.
    13.             print ("User entered his name: " + guiText.text);
    14.         }
    15.         // Normal text input - just append to the end
    16.         else {
    17.             guiText.text += c;
    18.         }
    19.     }
    20. }
    21.  
    This is not the total solution for your problem but it is probably a good starting point.
     
  3. tcontrada

    tcontrada

    Joined:
    Dec 2, 2010
    Posts:
    19
    I saw that example, but it does not provide an actual cursor it just adds the "_" to the end of the text.
    I need to actually replace the defualt "|" cursor.
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, the only way to control and modify the cursor is to create your own. You need to either set Screen.showCursor to false and use GUI.DrawTexture(Rect(), Texture) to draw the mouse's new texture, or you need to go all the way and use Screen.lockCursor.

    The latter method would require you to grab Mouse X and Mouse Y to move your own 3D cursor, and it would also require you to code in GUI focus yourself, but it would give you full control over cursor position and texture at all times with enough coding.
     
  5. tcontrada

    tcontrada

    Joined:
    Dec 2, 2010
    Posts:
    19
    Is the method you're describing for the mouse cursor, or the blinking text cursor that is inherit to the textarea GUI control?

    I am looking to modify the textarea cursor (blinking |) withing the control itself.
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    I was thinking more the mouse cursor. I'm not sure there's a way to modify the cursor inside TextArea.
     
  7. alyxlander

    alyxlander

    Joined:
    Jan 18, 2013
    Posts:
    1
    Well, if you want to all together get rid of the cursor try this:

    the first part is not really necessary since in the the second part you are making it clear, but this works perfectly for me.

    Code (csharp):
    1.         GUI.skin.settings.cursorFlashSpeed = 0; // turns off textarea cursor | flashing
    2.         GUI.skin.settings.cursorColor = Color.clear; // Makes it clear