- Home /
Custom Console in Application
I'm trying to make a 'TextWindow' that holds messages that the Application sends to it. The problem is that I have no idea on how to do this. I want to do something like, in C#,
TextWindow.NewLine("This will be displayed in the TextWindow");
Would I just create a new script called "TextWindow" and set it up something like this?
using UnityEngine;
using System.Collections;
public class TextWindow : MonoBehaviour
{
public TextWindow(Rect Size, string WindowName)
{
GUI.Box(new Rect(Size), WindowName);
}
public NewLine(string Text)
{
GUI.Label(new Rect(Rect), Text);
}
}
Then call it using,
TextWindow tWindow = new TextWindow(Size);
Size of course being a Rect.
This is a basic concept on how I want it to look in the end.
Answer by Jamora · Sep 09, 2013 at 07:46 AM
Because this is a console, and you will ever only need one, you should begin by making your console a static class.
The basic principle is simple: you have a ScrollView, a VerticalArea, FlexibleSpace and a List. So something like:
public static class MyConsole{
private static List< string > messages;
private static Vector2 viewPoint;
public static void DrawConsole(Rect area){
GUILayout.BeginArea(area);
viewPoint = GUILayout.BeginScrollView(viewPoint);
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
//print each string in messages here
//now close all the above areas
}
public static void NewMessage(string message){
messages.Add(message);
viewPoint = Vector2.one;
}
}
Now, in an OnGUI, to draw this, you use MyConsole.DrawConsole(area);
and to add messages, MyConsole.NewMessage(message);
If you, for some reason need multiple consoles/textwindows, the same idea applies, you just need to remove the static, and keep track of the references.
GUI Error: You are pushing more GUIClips than you are popping. $$anonymous$$ake sure they are balanced.
I'm getting that error continuously. Also, I tried using the code, but it doesn't draw the console, nor can I write messages to it. I tried making them non-static, and initializing an instance of Console, but that didn't work either. I did have to set the return type of DrawConsole to void, it was giving me an error,
Parse Error: Class, struct, or interface method must have a return type
warning CS0649: Field `Console.messages' is never assigned to, and will always have its default value `null'
General Warning, never goes away.
I also had to 'using System.Collections.Generic;' even though I already have System.Collections. But oh well, not a problem there.
I also get this error when I call Console.New$$anonymous$$essage.
NullReferenceException: Object reference not set to an instance of an object Console.New$$anonymous$$essage
Any reason you think it won't draw? This is the code I'm using it in.
using UnityEngine;
using System.Collections;
public class Server : $$anonymous$$onoBehaviour
{
public string IP;
public string Port;
public int ConnectionCap = 10;
string ServerStatus;
void OnGUI()
{
GUI.Label(new Rect(Screen.width*0.035f, Screen.height*0.035f, 150, 25), ServerStatus);
GUI.Label(new Rect(Screen.width*0.035f, Screen.height*0.06f, 150, 25), "Connections: " + Network.connections.Length);
IP = GUI.TextField(new Rect(Screen.width*0.035f, Screen.height*0.0935f, 150, 25), IP);
Port = GUI.TextField(new Rect(Screen.width*0.035f, Screen.height*0.14f, 75, 25), Port);
if(GUI.Button(new Rect(Screen.width*0.9f-50, Screen.height*0.9f-12.5f, 100, 25), "Start Server"))
{
ServerStatus = "Starting...";
Console.New$$anonymous$$essage(ServerStatus);
Network.InitializeServer(ConnectionCap, int.Parse(Port), true);
ServerStatus = "Started";
Console.New$$anonymous$$essage(ServerStatus);
}
if(GUI.Button(new Rect(Screen.width*0.9f-50, Screen.height*0.95f-12.5f, 100, 25), "Stop Server"))
{
ServerStatus = "Stopping...";
Console.New$$anonymous$$essage(ServerStatus);
Network.Disconnect();
ServerStatus = "Stopped";
Console.New$$anonymous$$essage(ServerStatus);
}
Console.DrawConsole(new Rect(Screen.width/2-100, Screen.height/2-50, 200, 100));
}
void Start()
{
ServerStatus = "Stopped";
}
}
Because I think Unity Answers isn't a Write-$$anonymous$$yCode-For-$$anonymous$$e service, I only gave you the rough idea on how to accomplish the console.
The reason you get the GUIClips error is because you have to have a corresponding close statement for each begin; i.e. GUILayout.BeginVertical();
is closed with GUILayout.EndVertical();
. I did mention this in the code snippet.
You also need to initialize the messages list. Currently it is only declared, and as such has the value null.
I'll concede that I forgot to type in the return type to the DrawConsole method.
I did attempt to write my own, so you aren't necessarily writing my code. Anyway, How would I go about printing the string's? I really don't have a clue about this one and that's why I come here after trying everything I can on my own. I do appreciate the help. I would like the text to get pushed upwards, and align right to left if you wouldn't $$anonymous$$d doing that.
I have the corresponding close statements as well. Still getting GUIClip error's.
You would print the strings by iterating over the messages list, then using GUILayout.Label on each string.
The text automatically gets pushed upwards, because you print all the strings in the list, then set the viewPoint to Vector2.one (that is bottom in ScrollViews).
You can set all kinds of style options in GUIStyles, if you need a lot of GUIStyles, you can use a GUISkin, which is just a collection of GUIStyles.
If you have EndArea, EndScrollView, and EndVertical; you shouldn't get GUIClip errors.
Could I use a for loop where you stated to print the strings? Would that even work? Wouldn't it need to be in an update function or something? Or does OnGUI update by itself?
Your answer
![](https://koobas.hobune.stream/wayback/20220613113826im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing a GUILabel text SIZE 3 Answers
Color.white not so much white... 3 Answers
How to set text(timer) to a fixed place 1 Answer
How to clear a GUI Label 1 Answer