Jump to content

GUI Help


jmbon

Recommended Posts

Hello, 

     I am in the process of creating a new mod and I find myself stuck on correctly creating two separate GUI's. The first is very simple as it has a toggle, a horizontal slider and 2 labels all inside of a window and all automatically positioned by using GUILayout.*. The second GUI is actually a compass that I want to display in the game window. I know how to do the rotations for the needle but I don't have much of a clue as to how to begin. I've read through forums, Unity how-to's etc and while they helped somewhat with the first GUI, they offered no help on the second. 

Here is all of my GUI code:

Main Class:

GUI_Code.GUI_Draw gdraw = new GUI_Code.GUI_Draw();

private void OnGUI()
{
	gdraw.OnGUI(1);
}

My GUI Class

using UnityEngine;

namespace GUI_Code
{
    public class GUI_Draw : MonoBehaviour
    {
        public bool toggle = true;
        public float hSliderValue = 0.0F;
        public Texture2D icon;

        private Rect windowRect = new Rect(10, 30, 100, 90);

//        Texture2D compass; // compass image
//        Texture2D needle; // needle image  
//        Rect compass_rect = new Rect(10, 10, 200, 200); // create a new rectangle at position (x = 1, y = 10) with size (x = 200, y == 200)
//        float angle; // angle to rotate the needle
//        Quaternion rot;
//        Vector3 axis; // but an axis variable must be provided as well


        public void OnGUI(int windowID)
        {
            // Make a background box
            GUI.Box(new Rect(10, 10, 150, 90), "1st GUI Menu");

//            GUI.DrawTexture(compass_rect, compass);
//            Vector2 compass_widget_center = new Vector2(compass_rect.x + compass_rect.width / 2,compass_rect.y + compass_rect.height / 2); // find the compass center coordinates
//            Matrix4x4 svMat = GUI.matrix; // save gui matrix
//           GUIUtility.RotateAroundPivot(angle, compass_widget_center); // prepare matrix to rotate
//            GUI.DrawTexture(compass_rect,needle); // draw the needle rotated by angle
//            GUI.matrix = svMat; // restore gui matrix

            // Draw all of the buttons inside of the window
            windowRect = GUI.Window(windowID, windowRect, WindowFunction, "My Window", HighLogic.Skin.window);
        }

        public void WindowFunction(int windowID)
        {
            // Begin the singular Horizontal Group
            GUILayout.BeginHorizontal();

            // Toggle setup
            wind_toggle = GUILayout.Toggle(toggle, "Toggle", HighLogic.Skin.toggle);
            GUILayout.Label("Toggle", HighLogic.Skin.label);


//            GUILayout.BeginVertical();


            // Slider setup
            hSliderValue = GUILayout.HorizontalSlider(1f, 0f, 10f);
            GUILayout.Box("Wind Scaling = " + Mathf.Round(hSliderValue), HighLogic.Skin.box);


            // Compass setup
 //           GUILayout.Label("Direction");


            // End the Groups and Area
//            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            GUILayout.EndArea();

            // This last thing checks whether the right mouse button or middle mouse button are clicked on the window. If they are, we ignore it, otherwise we GUI.DragWindow()
            // Calling that allows the window to be moved by clicking it (anywhere empty on the window) with the left mouse button and dragging it to wherever you want.
            if (!Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
                GUI.DragWindow();

        }

    }
}

The first GUI is the uncommented lines in OnGUI(int window ID). It has 2 major problems that I don't know how to correct. First, the window isn't draggable even though I make it draggable in the last line of OnGUI(int window). Second, in the Debug.Log, I am getting "[Exception]: InvalidOperationException: Operation is not valid due to the current state of the object" 

 

The second GUI is the commented out lines. I am trying to make a simple 2D compass similar to:

  compass-png.29895

How do I even go about setting this up and what's wrong with my first GUI? I'll keep looking through forums and Github code but so far, it's been dead ends. Thank you in advance. 

Also, if you know how to do it, I'd like both the 1st GUI and the compass to be attached to an icon at the right side of the screen. I am assuming that would be a 3rd GUI with just a toggle that did 1st_GUI.show() and compass.show() or 1st_GUI.hide() and compass.hide() (I know the syntax is wrong but you get the idea). Is this the correct way to do this?

Edited by jmbon
added some further code examples
Link to comment
Share on other sites

Monobhaviours and classes derived from them are not meqnt to be created via 'new' keyword in Unity.

They are meant to be instantiated via AddComponent on a relevant GameObject.

 

Moreover when you named a method OnGUI in your second class it made Unity call it automatically. So when you call it again in another class's OnGUI you have problems.

THirdly you dont need the IF befor GUI.DragWindow, it is handled automatically

I do recommed studying new Unity 5 UI and using it instead of ongui calls - it makes much more sense.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...