- Home /
GUI Button within panel
How can I create a GUI.Button through script that is influenced by the Rect properties of a panel, and not by the Canvas. Thus creating a button that lies within a panel and is influenced by it.
To elaborate, this simple script creates a Button in a Canvas using the properties of that Canvas's Rect transform
private void OnGUI ()
{
if(GUI.Button(new Rect(10,10,100,100),"thanks"))
{
Debug.Log("Thanks for clicking this Button");
}
}
which yields this
where as my desired result if the button was influenced by the panel should look something similar to this.
Answer by Bunny83 · Apr 28, 2017 at 08:34 PM
Mixing the new UI system with the old IMGUI system makes not much sense, Those two systems aren't compatible. If you want to use the new UI system (Canvas, Panels, ...) you should use an UI.Button component.
If you want to use the IMGUI system, you should create the "panel" also in OnGUI. This can be done by simply wrapping the GUI.Button inside a GUI.BeginArea / EndArea pair. You can display this area with the style "box" which will create a panel-like area.
In theory you could positioning an IMGUI button relative to an element of the new UI. However that wouldn't always work as the new UI can be displayed in 3d while the old IMGUI system is restricted to the screen (2d). You would need to manually getting the position of your UI panel, transform it into GUI space and just add it to the position that you're using to draw the GUI.Button.
Though just using an UI.Button would be way easier.
didn't know there are old and new GUI systems. will look into UI.Button. Thanks
Answer by gorsefan · Jul 09, 2016 at 12:15 AM
Make the button a child of the panel, something like;
yourGUIButtonGameobject.transform.SetParent(yourPanelGameObject.transform);
not helping. I don't see a way to store if (GUI.Button) to a variable. So there is no way to have yourGUIButtonGameobject.
you method will work only if the button is in the scene or instantiated.
Your answer
Follow this Question
Related Questions
Is It Possible to Render the same Data to 2 Different UI Text Objects? 1 Answer
How to make a Panel (Or Scrollbar) Appear on Button Click 0 Answers
Load Bar For Camera Switch on Canvas 0 Answers
UI Elemets disapear after changing Parent Canvas. 0 Answers
User selected canvas background sprite 2 Answers