MessageDisplayer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Oops.. restored)
Line 55: Line 55:
 
}
 
}
 
</csharp>
 
</csharp>
 +
 +
 +
And now, PsychicParrot presents ... the same script! Same usage, just in Javascript, for fun :)
 +
 +
== Javascript - MessageDisplayer.js ==
 +
 +
<javascript>
 +
// Use this script on a guiText object to have status messages
 +
// Just call messageDisplayerObject.DisplayMessage("hello") and you'll
 +
// get a line of self disappearing messages.
 +
 +
private var messages = new Array();
 +
 +
function Start(){
 +
 +
 +
}
 +
 +
function DisplayMessage(message) {
 +
       
 +
        messages.Add(message);
 +
        UpdateDisplay();
 +
        Invoke("DeleteOldestMessage", 3);
 +
 +
}
 +
   
 +
function DeleteOldestMessage() {
 +
        // The following "if statement" protects from this function
 +
        // getting called by SendMessage from another script and
 +
        // crashing.
 +
        if (messages.length > 0)
 +
        {
 +
            messages.Shift();
 +
            UpdateDisplay();
 +
        }
 +
    }
 +
   
 +
function UpdateDisplay() {
 +
 +
        formattedMessages = "";
 +
       
 +
        for(l=0;l<messages.length;l++)
 +
        {
 +
            formattedMessages += messages[l] + "\n";
 +
        }
 +
       
 +
        guiText.text = formattedMessages;
 +
 +
}
 +
</javascript>

Revision as of 02:14, 27 June 2008

Author: Jonathan Czeck (aarku)

Contents

Description

Use this script on a guiText object to have status messages. Just call messageDisplayerObject.DisplayMessage("hello") and you'll get a line of self disappearing messages. TODO: Make this script more of a singleton.

Usage

Use this script on a guiText object to have status messages.

C# - MessageDisplayer.cs

<csharp> using UnityEngine; using System.Collections;

// Use this script on a guiText object to have status messages // Just call messageDisplayerObject.DisplayMessage("hello") and you'll // get a line of self disappearing messages.

public class MessageDisplayer : MonoBehaviour {

   ArrayList messages = new ArrayList();
   
   public void DisplayMessage(string message)
   {
       messages.Add(message);
       UpdateDisplay();
       Invoke("DeleteOldestMessage", 5F);
   }
   
   void DeleteOldestMessage()
   {
       // The following "if statement" protects from this function
       // getting called by SendMessage from another script and
       // crashing.
       if (messages.Count > 0)
       {
           messages.RemoveAt(0);
           UpdateDisplay();
       }
   }
   
   void UpdateDisplay()
   {
       string formattedMessages = "";
       
       foreach (string message in messages)
       {
           formattedMessages += message + "\n";
       }
       
       guiText.text = formattedMessages;
   }

} </csharp>


And now, PsychicParrot presents ... the same script! Same usage, just in Javascript, for fun :)

Javascript - MessageDisplayer.js

<javascript> // Use this script on a guiText object to have status messages // Just call messageDisplayerObject.DisplayMessage("hello") and you'll // get a line of self disappearing messages.

private var messages = new Array();

function Start(){


}

function DisplayMessage(message) {

       messages.Add(message);
       UpdateDisplay();
       Invoke("DeleteOldestMessage", 3);

}

function DeleteOldestMessage() {

       // The following "if statement" protects from this function
       // getting called by SendMessage from another script and
       // crashing.
       if (messages.length > 0)
       {
           messages.Shift();
           UpdateDisplay();
       }
   }
   

function UpdateDisplay() {

       formattedMessages = "";
       
       for(l=0;l<messages.length;l++) 
       {
           formattedMessages += messages[l] + "\n";
       }
       
       guiText.text = formattedMessages;

} </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox