[HELP] Couple of things to fix up

Hi.

I’m fairly new to unity and C#, so mind me on some of my sloppy code; I currently study game design in a course I go to.

The main catch to what I am trying to do is make a console that allows you to enter commands etc. (just for experience on how to do it etc).

There are 2 things that i’d like to change, which is the spacing between the text and the fact that i need to click out of the text field to click enter on the keyboard to submit the command
For now, I just want the basis of the console working (writing in commands and entering them), im planning on doing the commands once I’ve fixed these issues.

Heres the code.

using UnityEngine;
using System.Collections.Generic;

public class Console : MonoBehaviour
{

    public bool consoleEnabled;

    public List<string> consoleHistory = new List<string>();

    public string command, currentCommand, commandTextBox;

    public GUIStyle align;
       
    public bool ToggleConsole()
    {
        if(consoleEnabled == true)
        {
            consoleEnabled = false;
            return (false);
        }   
        else
        {
            consoleEnabled = true;
            return (true);
        }
    }

    void Start ()
    {
        consoleEnabled = false;   
    }

    // Update is called once per frame
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.BackQuote))
        {
            ToggleConsole();
        }
    }

    void OnGUI ()
    {
        string consoleHistoryCsv = string.Join("\n", consoleHistory.ToArray());
        if(consoleEnabled)
        {

            GUI.Box (new Rect (0*Screen.width / 16, 0*Screen.height / 15, 16*Screen.width / 16, 4*Screen.height / 15), consoleHistoryCsv, align);
            currentCommand = GUI.TextField(new Rect(0*Screen.width/16, 4*Screen.height/15, 16*Screen.width/16, 0.65f*Screen.height/15), currentCommand);
            if(Input.GetKeyDown(KeyCode.Return))
            {
                consoleHistory.Add(currentCommand);
                currentCommand = "";
            }
        }
    }

}

Nothing to interesting.

Thanks for the help

Btw, this is the spacing issue im talking about.

What if you Use The New UI for this ?

To be honest, I find UI a bit confusing at times, and I guess using hard-coded stuff is good aswell, gives me the experience if i need it in the near future
Of course I’ll use the New UI for it someday, but really this is just for educational purposes :stuck_out_tongue:

… you want to learn old clunky, hard to understand, difficult to maintain, slow to perform UI system. Rather than the whatYouSeeIsWhatYouGet speedy new system… :eyes:

for one thing, custom editor scripting. If you’re doing in game UI you really want to be using the newer canvas based system.

I know I know, but like i said before, you never know if i’ll need it or not in the future

I want to get into UI stuff but theres honestly not that many good tutorials that explain anything clearly… any suggestions on that??

The ones in the Learn section are quite good for explaining the basics, what do you have difficulty with when it comes to the new UI. You’ve said you find it confusing, but not really why or what parts confuse.

1 Like

In the Update couldn’t you just have something like

if(!consoleEnabled && Input.GetKeyDown(KeyCode.Return))
{
    // code to submit etc
}

New UI? I haven’t used Unity in a long time, what do you mean by that? You mean the canvas system right? That’s the only UI I ever used in Unity, what was it previously?

Just the general parts about it. I’ve watched the canvas youtube video they’ve made and I’m not interested in old code anymore xD

Anyways, thanks for the help. Gonna work with the UI now :smile:

Canvas System yes, it looks much better and easier than the hardcode way thankfully :stuck_out_tongue:

What you’re looking for is GUI.SetNextControlName. You can see an example of creating a text field and putting the cursor in it here.

As people have said, the GUI system was semi-replaced with the canvas system. Canvas is much better for UI because it automatically resizes based on screen size, as long as you set it up right. For prototyping, though, GUI is just so much faster. You don’t need to create a Canvas in the scene, you don’t have to bind that canvas to some global script, or search for it, you don’t have to find the correct components, etc. You want a label on the screen, you just go GUI.Label and it’s done.

I don’t think it’s worthwhile to become a GUI-guru, you’re going to use the Canvas system in every project that gets bigger than a prototype, but that doesn’t mean that the GUI system isn’t worthwhile to at least have a decent grasp of.

“new UI” is indeed the “canvas system”. Prior to that all UI inGame or inEditor was done through the OnGUI function.

There are a few different terms different people use and it gets a little more confusing when you bring in the old asset store “improved UI” which people used instead of the ongui approach.

OnGUI UI (legacy)
Canvas UI (“new”)
Immediate Mode UI (IMGUI, technical term for the OnGUI approach http://docs.unity3d.com/Manual/GUIScriptingGuide.html)
uGUI (fairly sure this is/was used to refer to the new canvas system)
nGUI (asset store replacement for legacy ui…)

:eyes:

personally I use the first two :slight_smile:

you can also create UI prefabs, which is really useful.