Hey all,
I’m trying to make a debug text database to output basic info to the screen. I’m a complete Unity newb and I can’t figure out an effective way to accomplish my goal. But I’m betting someone out there knows a better unity specific way of accomplishing this.
What I’d like is to output a series of debugging text (things like player position, camera position, etc) to the screen. So, in previous game engines you just output the text at the end of your update loop. Generally, I just make a container that you fill up through your update cycle with a series of debugging statements, and then when the container starts printing to the screen, it auto-positions the text for me. And, after printing, I delete the string. I delete it to achieve an immediate mode effect, and don’t fill up my container with multiple copies of the same string.
But in unity, it seems like I can only print to the screen during OnGUI, which is called several times a frame. So, when I clear out my text container after printing once, it will still be called again, but there’s nothing left to print. It already printed earlier in the same frame.
How do I achieve this effect in Unity the proper way?
Here’s the script I use to achieve this effect currently
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelUpdate : MonoBehaviour {
public float LabelHeight = 22.0f;
public float LabelWidth = 400.0f;
public System.Collections.Generic.List<string> ScreenText = new System.Collections.Generic.List<string>();
private GameObject Player = null;
// Use this for initialization
void Start ()
{
Player = GameObject.Find("Player");
}
// Update is called once per frame
void Update ()
{
ScreenText.Add( "Player Position:" + Player.transform.localPosition.ToString());
}
void LateUpdate()
{
}
void OnGUI()
{
float CurrentScreenTextPos = 0.0f;
int count = 0;
foreach( string screentext in ScreenText )
{
if( ( screentext != null ) && ( screentext != "" ) )
{
Rect label = new Rect( 0.0f, CurrentScreenTextPos, LabelWidth, LabelHeight );
GUI.Label( label, screentext);
CurrentScreenTextPos += LabelHeight;
count++;
}
}
ScreenText.Clear();
GUI.Label( new Rect(0.0f, CurrentScreenTextPos, LabelWidth, LabelHeight), "I printed out " + count.ToString() + " lines");
}
}