MessageDisplayer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category: MonoBehaviour]]
+
[[Category:MonoBehaviour]]
[[Category: C Sharp]]
+
[[Category:C Sharp]]
[[Category: HUD]]
+
[[Category:Heads Up Display Scripts]]
[[Category: Displays]]
+
[[Category:GUIText]]
[[Category: GUIText]]
+
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
 
==Description==
 
==Description==
Line 12: Line 11:
  
 
==C# - MessageDisplayer.cs==
 
==C# - MessageDisplayer.cs==
<csharp>
+
<syntaxhighlight lang="csharp">
using UnityEngine;
+
using UnityEngine;
using System.Collections;
+
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 this function from
 +
        // 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;
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
And now, PsychicParrot presents ... the same script! Same usage, just in Javascript, for fun :)
 +
 
 +
== Javascript - MessageDisplayer.js ==
 +
 
 +
<syntaxhighlight lang="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.
 
   
 
   
// Use this script on a guiText object to have status messages
+
private var messages = new Array();
// Just call messageDisplayerObject.DisplayMessage("hello") and you'll
+
 
// get a line of self disappearing messages.
+
function Start(){
+
public class MessageDisplayer : MonoBehaviour
+
{
+
}
ArrayList messages = new ArrayList();
+
 
+
function DisplayMessage(message) {
public void DisplayMessage(string message)
+
       
{
+
        messages.Add(message);
messages.Add(message);
+
        UpdateDisplay();
UpdateDisplay();
+
        Invoke("DeleteOldestMessage", 3);
Invoke("DeleteOldestMessage", 5F);
+
 
}
+
}
+
   
void DeleteOldestMessage()
+
function DeleteOldestMessage() {
{
+
        // The following "if statement" protects this function from
messages.RemoveAt(0);
+
        // getting called by SendMessage from another script and
UpdateDisplay();
+
        // crashing.
}
+
        if (messages.length > 0)
+
        {
void UpdateDisplay()
+
            messages.Shift();
{
+
            UpdateDisplay();
string formattedMessages = "";
+
        }
+
    }
foreach (string message in messages)
+
   
{
+
function UpdateDisplay() {
formattedMessages += message + "\n";
+
 
}
+
        formattedMessages = "";
+
       
guiText.text = formattedMessages;
+
        for(l=0;l<messages.length;l++)  
}
+
        {
}
+
            formattedMessages += messages[l] + "\n";
</csharp>
+
        }
 +
       
 +
        guiText.text = formattedMessages;
 +
 
 +
}
 +
</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

Author: Jonathan Czeck (aarku)

Contents

[edit] 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.

[edit] Usage

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

[edit] C# - MessageDisplayer.cs

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 this function from
        // 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;
    }
}


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

[edit] Javascript - MessageDisplayer.js

// 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 this function from
        // 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;
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox