From Unify Community Wiki
Revision as of 19:00, 19 October 2009 by NCarter (Talk | contribs)

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 pixel coordinate position specified in startPos (origin at the bottom left corner), and existing messages will be moved a set number of pixels 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 = 20.0; // The pixel spacing between text objects var startPos = Vector3(20, 20, 0); // The position GUIText objects will be instantiated var layerTag = 0; private var messages = new Array(); // The array that holds all our text objects

// Provide singleton support for this class. // The script must still be attached to a game object, but this will allow it to be called // from anywhere without specifically identifying that game object. private static var instance; public static function Instance () {

   //if (!instance) { instance = new MessageList(); }
   return instance;


function Awake () {

   // First make sure that we have a prefab set.  If not, then disable the script
   if (!messagePrefab) { enabled = false; Debug.Log("Must set the GUIText prefab for MessageList"); }
   // Set the singleton instance of this class
   instance = this;


// 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.
       // The y axis uses normalized coordinates, so we divide by the screen height to convert 
       //  pixel coordinates into normalized screen coordinates.
       messages[i].transform.position.y += (lineSize/Screen.height);
   // 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, Vector3(startPos.x/Screen.width, startPos.y/Screen.height, startPos.z), transform.rotation);
   newMessage.text = msgText;
   newMessage.gameObject.layer = layerTag;

} </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