Skip to content

Instantly share code, notes, and snippets.

@asus4
Created April 27, 2012 13:16
Show Gist options
  • Save asus4/2509133 to your computer and use it in GitHub Desktop.
Save asus4/2509133 to your computer and use it in GitHub Desktop.
Label Randamize Animation for nGUI Unity
using UnityEngine;
using System.Collections;
[AddComponentMenu("NGUI/Tween/Ex/Label Randamize")]
public class LabelRandamize : MonoBehaviour {
public UILabel label;
public bool autoDestroy = false;
const string randamText = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ11234567890";
[SerializeField] string text;
float randomTime;
float startTime;
int randomIndex;
void Start () {
if (label == null) {
label = GetComponent<UILabel>();
}
this.enabled = false;
if(text != null) {
RandomText(text, randomTime);
}
}
void Update () {
// update text
string str = "";
if(randomIndex <= 0) {
this.enabled = false;
label.text = this.text;
if(autoDestroy) {
Destroy(this);
}
return;
}
for(int i=0; i<randomIndex; ++i) {
str += GetRandChar();
}
label.text = str;
if(Time.time-startTime > randomTime) {
randomIndex--;
str = text.Substring(0, text.Length-randomIndex) + str;
}
}
public void RandomText(string text, float time) {
this.text = text;
this.randomTime = time;
this.enabled = true;
this.startTime = Time.time;
this.randomIndex = text.Length;
}
public static LabelRandamize MakeRandomLabel(GameObject go, string text, float time) {
LabelRandamize r = go.AddComponent<LabelRandamize>();
r.autoDestroy = true;
r.RandomText(text, time);
return r;
}
char GetRandChar() {
return randamText[Random.Range(0, randamText.Length)];
}
}
@asus4
Copy link
Author

asus4 commented Apr 27, 2012

Usage

LabelRandamize.MakeRandomLabel(uilabel.gameObject, "hogehoge", 0.1f);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment