Blinking Text Entry Cursor

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 1: Line 1:
Author: Joseph Quigley (CPUFreak91)
+
== JavaScript==
==Description==
+
<javascript>//These are made private so that they don't appear in the Inspector
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:
+
<syntaxhighlight lang="javascript">GUI.Label (Rect(400,35, 200, 70), "Type Your Name: " + nameString + cursorChar);</syntaxhighlight>
+
 
+
== JavaScript - BlinkingCursor.js==
+
<syntaxhighlight lang="javascript">//These are made private so that they don't appear in the Inspector
+
 
private var m_TimeStamp = Time.time;
 
private var m_TimeStamp = Time.time;
 
private var cursor = false;
 
private var cursor = false;
 
private var cursorChar = "";
 
private var cursorChar = "";
var maxStringLength : int; // Maximum characters to allow in the text entry field
+
var maxStringLength : int; //Maximum characters to allow in the text entry field
  
 
function Update () {
 
function Update () {
     if (Time.time - m_TimeStamp >= 0.5) { // Display/remove the cursor ever 1/2 second
+
     if (Time.time - m_TimeStamp >= 0.5) {
         m_TimeStamp = Time.time; // Reset the time stamp
+
         m_TimeStamp = Time.time;
         if (cursor == false) { //If the cursor is off, enable it
+
         if (cursor == false) {
 
             cursor = true;
 
             cursor = true;
             if (enteredString.Length < maxStringLength) // Only show the cursor character, though, if the entered string is less than the maximum length.
+
             if (enteredString.Length < maxStringLength)
            //If it's the same as, or more than the max length don't display the cursor character
+
 
                 cursorChar += "_";
 
                 cursorChar += "_";
 
         }
 
         }
Line 27: Line 17:
 
             cursor = false;
 
             cursor = false;
 
             if (cursorChar.Length != 0)
 
             if (cursorChar.Length != 0)
                 cursorChar = cursorChar.Substring(0, cursorChar.Length - 1); //Remove the cursor character. cursorChar = ""; would also work
+
                 cursorChar = cursorChar.Substring(0, cursorChar.Length - 1);
 
         }
 
         }
 
     }
 
     }
 
}
 
}
</syntaxhighlight>
+
</javascript>
 
+
== C# - BlinkingCursor.cs==
+
<syntaxhighlight lang="csharp">
+
using UnityEngine;
+
using System.Collections;
+
 
+
public class BlinkingCursor : MonoBehaviour {     
+
 
+
private float m_TimeStamp;
+
private bool cursor = false;
+
private string cursorChar = "";
+
private int maxStringLength = 24;
+
 
+
        void update() {
+
            if (Time.time - m_TimeStamp >= 0.5)
+
      {
+
      m_TimeStamp = Time.time;
+
      if (cursor == false)
+
      {
+
                                cursor = true;
+
      if (enteredString.Length < maxStringLength)
+
      {
+
      cursorChar += "_";
+
      }
+
      }
+
      else
+
      {
+
      cursor = false;
+
      if (cursorChar.Length != 0)
+
      {
+
      cursorChar = cursorChar.Substring(0, cursorChar.Length - 1);
+
      }
+
      }
+
      }
+
        }
+
}
+
</syntaxhighlight>
+
 
+
 
+
[[Category:JavaScript]]
+

Revision as of 04:18, 18 January 2012

JavaScript

<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) {
       m_TimeStamp = Time.time;
       if (cursor == false) {
           cursor = true;
           if (enteredString.Length < maxStringLength)
               cursorChar += "_";
       }
       else {
           cursor = false;
           if (cursorChar.Length != 0)
               cursorChar = cursorChar.Substring(0, cursorChar.Length - 1);
       }
   }

} </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox