Jump to content

GUI.button image and child UI's


jmbon

Recommended Posts

Hello, 

I'm relatively new to Unity still and I am having two issues that are GUI related. First, I am trying to set a .png image as the background for a GUI.button(rect, image). I just can not get the image to display as the background of the button. My second problem is that I am trying to use the same button to open another config UI for my mod so that players can enable and disable certain mod features. However, when I click the button (button appears even if the background image doesn't), my debug statement prints but the second UI doesn't draw to the screen. For the past several hours, I have searched forums, Github code (mainly Stage Recovery as it's code is commented and not a mess) and Unity 5.3 tutorials/examples/manual entries trying to figure out what I am doing wrong. Nothing has helped thus far. Hopefully someone here can spot my mistake. I greatly appreciate the help. 

public void OnDraw(int windowID)
        {
            draw_Icon_GUI(windowID);
//            GUILayout.Window(windowID, control_window_Rect, draw_Control_GUI, "Control", HighLogic.Skin.window);
        }

        void draw_Icon_GUI(int windowID)
        {
            Rect _Icon_Rect = new Rect(Screen.width - 40, 220, 40, 40);
//            Texture image = (Texture)Resources.Load("C:/Program Files (x86)/Steam/SteamApps/common/Kerbal Space Program/Resources/_symbol.png") as Texture; // <------- First attempt at displaying an image as the button background
//            GUI.skin.button.active.background = image; // <------- First attempt at displaying an image as the button background

            GUIContent image = new GUIContent("Click me", (Texture)Resources.Load("C:/Program Files (x86)/Steam/SteamApps/common/Kerbal Space Program/Resources/_symbol")); // <------- Third attempt at displaying an image as the button background
            if (image != null)
            {
                // Draw the background.
 //               GUI.DrawTexture(wind_Icon_Rect, image); // <------- Second attempt at displaying an image as the button background
            }
            else
                Debug.Log("Bad texture: " + image);


            if (GUI.Button(wind_Icon_Rect, image))
            {
                Debug.Log("[MOD : Gui Draw] Button was clicked."); // Prints when the button is clicked
                control_window_Rect = GUILayout.Window(windowID, control_window_Rect, draw_Control_GUI, "Control", HighLogic.Skin.window); // Doesn't execute ever. Worked before I started trying to make a button execute it.
            }

        }

        void draw_Control_GUI(int windowID) // Worked before I started trying to make a button execute it.
        {
            GUILayout.BeginVertical();

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

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

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

            GUI.DragWindow(); // GUI window is also having issues with not being draggable even when this section of code worked
        }

Above is the code that handles my GUI drawing. OnGUI() is in another class but all id does is call OnDraw(int windowID). I put // <---- comments where I believe my code is having the issues. I greatly appreciate any help I can get. Thank you. 

Link to comment
Share on other sites

Can't help with the image, but the second window won't draw because button is only ever true once and it needs to be called every frame you want the window to be visible in. I would suggest toggling a boolean and drawing the window elsewhere based on the state of that bool

bool visible = false;

OnGUI()
{
	if (button...)
		visible = !visible;
	if (visible)
		drawWindow...
}

 

Edited by Crzyrndm
Link to comment
Share on other sites

Thanks for that. I already had the bool in the code, I just wasn't using it there. Putting it there solved that issue. Still trying to figure out how to add an image. If I figure it out, I'll post it here. Thanks again. 

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