- Home /
How do I create a text box to display scrollable text I send it via script?
I'm currently making a game where I want to display text to describe what's happening. How can I send text to a text box without erasing the previous text, making it scroll up? Something like an in-game console where I could just print text on specific actions. Also, how could I erase "old" text (text that has been "scrolled up" x times)? Here's what I'm doing (I'm still learning how to code), and I realized I'm starting to run out of space :p
function OnGui () {
if (money < 15)
{
GUI.Label (Rect(10,260,200,200), "You have no money to buy food");
}
if (actionsPerDay == 0)
{
GUI.Label (Rect(10,280,400,200), "You're tired and need to go to sleep");
}
if (hunger == 0)
{
GUI.Label (Rect(10,300,400,200), "You're starving! You need to eat urgently!");
}
}
Answer by getyour411 · Mar 03, 2014 at 11:48 PM
http://answers.unity3d.com/questions/508268/i-want-to-create-an-in-game-log-which-will-print-a.html
An example in C# which uses a Queue instead of List:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class playerLog : MonoBehaviour {
public int maxLines = 8;
private Queue<string> queue = new Queue<string>();
private string Mytext = "";
public void NewMessage(string message) {
if (queue.Count >= maxLines)
queue.Dequeue();
queue.Enqueue(message);
Mytext = "";
foreach (string st in queue)
Mytext = Mytext + st + "\n";
}
void OnGUI() {
GUI.Label(new Rect(5, // x, left offset
(Screen.height - 150), // y, bottom offset
300f, // width
150f), Mytext,GUI.skin.textArea); // height, text, Skin features}
}
You could then use a GetComponent and pass messages to playerLog.NewMessage(newMessage), or declare that bit static so all classes can pass things to it w/o GetComponent references, etc.
Cool, that thread was what I was looking for! Thank you!
yw; if you happen to see this, please tick the Accept Solution below the thumbs.