Blinking Text Entry Cursor

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by CPUFreak91)
Line 1: Line 1:
 +
Author: Joseph Quigley (CPUFreak91)
 +
==Description==
 +
This script will allow you to display a blinking cursor (which can be any character, but I'm using an underline).
  
 +
==Usage==
 +
Assign this behavior to any GameObject.  It was designed to work with the example in the [http://unity3d.com/Documentation/ScriptReference/Input-inputString.html 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>
 +
[[Category:JavaScript]]

Revision as of 19:26, 19 October 2009

Author: Joseph Quigley (CPUFreak91)

Description

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

Usage

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
Namespaces

Variants
Actions
Navigation
Extras
Toolbox