Created
April 27, 2012 13:16
-
-
Save asus4/2509133 to your computer and use it in GitHub Desktop.
Label Randamize Animation for nGUI Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage