Blinking Text Entry Cursor

From Unify Community Wiki
Revision as of 19:26, 19 October 2009 by NCarter (Talk | contribs)

Jump to: navigation, search

Author: Joseph Quigley (CPUFreak91)


This script will allow you to display a blinking cursor (which can be any character, but I'm using an underline).


Assign this behavior to any GameObject. It was designed to work with the example in the Input String Example in the Unity docs, so you may want to move this code into your input-related scripts. Append the cursorChar variable to the end of your input string. Example: <javascript>GUI.Label (Rect(400,35, 200, 70), "Type Your Name: " + nameString + cursorChar);</javascript>

JavaScript - BlinkingCursor.js

<javascript>//These are made private so that they don't appear in the Inspector private var m_TimeStamp = Time.time; private var cursor = false; private var cursorChar = ""; var maxStringLength : int; // Maximum characters to allow in the text entry field

function Update () {

   if (Time.time - m_TimeStamp >= 0.5) { // Display/remove the cursor ever 1/2 second
       m_TimeStamp = Time.time; // Reset the time stamp
       if (cursor == false) { //If the cursor is off, enable it
           cursor = true;
           if (enteredString.Length < maxStringLength) // Only show the cursor character, though, if the entered string is less than the maximum length.
           //If it's the same as, or more than the max length don't display the cursor character
               cursorChar += "_";
       else {
           cursor = false;
           if (cursorChar.Length != 0)
               cursorChar = cursorChar.Substring(0, cursorChar.Length - 1); //Remove the cursor character. cursorChar = ""; would also work

} </javascript>

Personal tools