Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GeoDoX · Sep 09, 2013 at 04:31 AM · guitextlabel

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. alt text

textwindowexample.gif (25.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GeoDoX · Sep 09, 2013 at 08:22 AM 0
Share
 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";
     }
     
 }
avatar image Jamora · Sep 09, 2013 at 08:44 AM 0
Share

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.

avatar image GeoDoX · Sep 09, 2013 at 08:58 AM 0
Share

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.

avatar image Jamora · Sep 09, 2013 at 09:26 AM 0
Share

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.

avatar image GeoDoX · Sep 09, 2013 at 10:55 AM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Unity 5 UI Text Glitch (illegible characters) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges