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.
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.
Make the button a child of the panel, something like;
yourGUIButtonGameobject.transform.SetParent(yourPanelGameObject.transform);