Blinking Text Entry Cursor
From Unify Community Wiki
(Difference between revisions)
| Line 1: | Line 1: | ||
| − | == JavaScript== | + | Author: Joseph Quigley (CPUFreak91) |
| − | <javascript>//These are made private so that they don't appear in the Inspector | + | ==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: | ||
| + | <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) { | + | if (Time.time - m_TimeStamp >= 0.5) { // Display/remove the cursor ever 1/2 second |
| − | m_TimeStamp = Time.time; | + | m_TimeStamp = Time.time; // Reset the time stamp |
| − | if (cursor == false) { | + | if (cursor == false) { //If the cursor is off, enable it |
cursor = true; | cursor = true; | ||
| − | if (enteredString.Length < maxStringLength) | + | 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 += "_"; | cursorChar += "_"; | ||
} | } | ||
| Line 17: | Line 27: | ||
cursor = false; | cursor = false; | ||
if (cursorChar.Length != 0) | if (cursorChar.Length != 0) | ||
| − | cursorChar = cursorChar.Substring(0, cursorChar.Length - 1); | + | cursorChar = cursorChar.Substring(0, cursorChar.Length - 1); //Remove the cursor character. cursorChar = ""; would also work |
} | } | ||
} | } | ||
} | } | ||
| − | </ | + | </syntaxhighlight> |
| + | |||
| + | == 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]] | ||
Latest revision as of 21:03, 19 January 2012
Author: Joseph Quigley (CPUFreak91)
Contents |
[edit] Description
This script will allow you to display a blinking cursor (which can be any character, but I'm using an underline).
[edit] 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:
GUI.Label (Rect(400,35, 200, 70), "Type Your Name: " + nameString + cursorChar);
[edit] JavaScript - BlinkingCursor.js
//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 } } }
[edit] C# - BlinkingCursor.cs
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); } } } } }