From Unify Community Wiki
Revision as of 23:28, 15 February 2009 by Capnbishop (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: capnbishop



These two scripts can be used to create a list of automatic self-destroying messages that fade away. Each added message is put at the top or bottom of a list, moving the rest down or up (respectively) depending on the direction. The message will last for a designated period of time, then fade away and destroy itself.


Use MessageList.js on an empty game object in the scene. Use the TimedFadeText.js script on a GUIText prefab, then assign that prefab to MessageList. Call AddMessage() on MessageList to create a message and have it added to the list. New messages will be instantiated at the MessageList game object's position, and existing messages will be moved along the y axis by the lineSize property. The duration of the life of the message and fading period can be set in TimedFadeText.

Javascript - MessageList.js

<javascript> var messagePrefab : GUIText; // The prefab for our text object var lineSize = .05; // The normalized spacing between text objects private var messages = new Array(); // The array that holds all our text objects

function Start () {

   // First make sure that we have a prefab set.  If not, then disable the script
   if (!messagePrefab) { enabled = false; }


// AddMessage() accepts a text value and adds it as a status message. // All other status messages will be moved along the y axis by a normalized distance of lineSize. // AddMessage() also handles automatic removing of any GUIText objects that automatically destroy // themselves. function AddMessage (msgText : String) {

   // Itterate through the messages, removing any that don't exist anymore, and moving the rest
   for (var i = 0; i < messages.length; i++) {
       // If this message is null, remove it, drop back the i count, and jump back to the begining
       //   of the loop.
       if (!messages[i]) { messages.RemoveAt(i); i--; continue;}
       // If this message object does exist, then move it along the y axis by lineSize
       messages[i].transform.position.y += lineSize;
   // All the existing messages have been moved, making room for the new one.
   // Instantiate a new message from the prefab, set it's text value, and add it to the
   //   array of messages.
   var newMessage = Instantiate(messagePrefab, transform.position, transform.rotation);
   newMessage.text = msgText;

} </javascript>

Javascript - TimedFadeText.js

<javascript> var liveTime = 5.0; // The number of seconds the GUIText will last before starting to fade var fadeTime = 2.0; // The number of seconds to fade until totally transparent private var time = 0.0; // Static var to track how much time has passed private var isFading = false; // Static var to track if we're in the fading stage private var startAlpha = 1.0; // Static var to keep track of the initial amount of alpha

function Start () {

   // This script uses the GUIText's material to set the alpha fade.
   // If the font doesn't have a material, then this won't work, so disable the script.
   if (!guiText.material) {
       enabled = false;
   // Get the starting alpha value.
   // If the developer has the text start transparent, then we need to fade from that point.
   startAlpha = guiText.material.color.a;


function Update () {

   // Update our time var to keep track of how much time has passed.
   time += Time.deltaTime;
   if (isFading) {
       // We're in the fading stage.  If we've reached the end of this stage, then destroy the gameObject.
       if (time >= fadeTime) {
       // We're still fading, so update the material's alpha color to make it fade a little more.
       guiText.material.color.a = CalculateAlpha();
   else if (time >= liveTime) {
       // If we're not fading yet, but should be, then update our values to proceed to the fading stage.
       isFading = true;
       time = 0.0;
   // If we're not fading yet, and don't need to be yet, then nothign will happen at this point.  The
   //   text will just exist, and the timer will keep incrementing until there's a state change.


// CalculateAlpha() simple takes the static global vars we're using to keep track of everything // to figure out our current alpha value from 0 to 1. function CalculateAlpha() {

   // Find out the percent of time from 0 to 1 that has gone between when the text starts and stops fading
   var timePercent = Mathf.Clamp01((fadeTime - time) / fadeTime);
   // Generate a nice, smooth value from 1 to 0 to represent how faded the text is
   var smoothAlpha = Mathf.SmoothStep(0.0, startAlpha, timePercent);
   // We actually could just return the timePercent value for a linear fade, but we want it to be smooth,
   //  so return the smoothAlpha.
   return smoothAlpha;


@script RequireComponent(GUIText) </javascript>

Personal tools