MessageDisplayer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - MessageDisplayer.cs)
Line 13: Line 13:
 
==C# - MessageDisplayer.cs==
 
==C# - MessageDisplayer.cs==
 
<csharp>
 
<csharp>
using UnityEngine;
+
using UnityEngine;
using System.Collections;
+
using System.Collections;
+
 
// Use this script on a guiText object to have status messages
+
// Use this script on a guiText object to have status messages
// Just call messageDisplayerObject.DisplayMessage("hello") and you'll
+
// Just call messageDisplayerObject.DisplayMessage("hello") and you'll
// get a line of self disappearing messages.
+
// get a line of self disappearing messages.
+
 
public class MessageDisplayer : MonoBehaviour
+
public class MessageDisplayer : MonoBehaviour
{
+
{
ArrayList messages = new ArrayList();
+
    ArrayList messages = new ArrayList();
+
   
public void DisplayMessage(string message)
+
    public void DisplayMessage(string message)
{
+
    {
messages.Add(message);
+
        messages.Add(message);
UpdateDisplay();
+
        UpdateDisplay();
Invoke("DeleteOldestMessage", 5F);
+
        Invoke("DeleteOldestMessage", 5F);
}
+
    }
+
   
void DeleteOldestMessage()
+
    void DeleteOldestMessage()
{
+
    {
messages.RemoveAt(0);
+
        // The following "if statement" protects from this function
UpdateDisplay();
+
        // getting called by SendMessage from another script and
}
+
        // crashing.
+
        if (messages.Count > 0)
void UpdateDisplay()
+
        {
{
+
            messages.RemoveAt(0);
string formattedMessages = "";
+
            UpdateDisplay();
+
        }
foreach (string message in messages)
+
    }
{
+
   
formattedMessages += message + "\n";
+
    void UpdateDisplay()
}
+
    {
+
        string formattedMessages = "";
guiText.text = formattedMessages;
+
       
}
+
        foreach (string message in messages)
}
+
        {
 +
            formattedMessages += message + "\n";
 +
        }
 +
       
 +
        guiText.text = formattedMessages;
 +
    }
 +
}
 
</csharp>
 
</csharp>

Revision as of 22:21, 15 December 2005

Author: Jonathan Czeck (aarku)

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>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox