You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何为OnGUI中初始化的GUI Button设置父面板?

How to Attach an OnGUI Button to a Panel Parent

Hey there! Let's work through getting your OnGUI button properly tied to that panel parent you've set up. The key thing to understand first is that Unity's OnGUI system (Immediate Mode GUI) is completely separate from the modern UGUI system (the one using RectTransforms, Panels, etc.). That's why your button is stuck in the screen corner—you can't directly assign a Transform parent to an OnGUI element like you would with a UGUI object.

Here are two solid solutions depending on whether you want to stick with OnGUI or switch to the more integrated UGUI approach:

Option 1: Keep Using OnGUI (Anchor to Panel)

To make your OnGUI button follow the panel's position and bounds, you'll need to convert the panel's UI space coordinates to screen space, then restrict your button's drawing area to that region using GUI.BeginGroup:

public Transform panelParent; // Assign your Panel in the Inspector
private GUIContent button_tex_con;

void Start()
{
    // Initialize your GUIContent here (load sprite/text, etc.)
    button_tex_con = new GUIContent("My Button"); // Example content
}

void OnGUI()
{
    if (panelParent == null) return;

    // Get the panel's RectTransform and convert it to screen-space bounds
    RectTransform panelRect = panelParent.GetComponent<RectTransform>();
    Canvas canvas = panelParent.GetComponentInParent<Canvas>();
    Rect panelScreenBounds = RectTransformUtility.PixelAdjustRect(panelRect, canvas);

    // Restrict all subsequent GUI drawing to the panel's bounds
    GUI.BeginGroup(panelScreenBounds);
    {
        // Draw your button inside the group (it will now follow the panel)
        // Use GUILayout for auto-layout, or GUI.Button for fixed positioning
        if (GUILayout.Button(button_tex_con, GUILayout.Width(100), GUILayout.Height(100)))
        {
            Debug.Log("Clicked");
        }

        // Optional: For fixed positioning (e.g., center the button in the panel)
        // Rect buttonRect = new Rect(
        //     (panelScreenBounds.width - 100) / 2, 
        //     (panelScreenBounds.height - 100) / 2, 
        //     100, 100
        // );
        // if (GUI.Button(buttonRect, button_tex_con))
        // {
        //     Debug.Log("Clicked");
        // }
    }
    GUI.EndGroup();
}

Notes for Option 1:

  • Make sure your panel's parent Canvas uses a compatible Render Mode (Screen Space - Overlay or Screen Space - Camera work best here).
  • RectTransformUtility.PixelAdjustRect handles the conversion from UI space to screen space, accounting for Canvas scaling.

If you don't have a hard requirement to use OnGUI, switching to a UGUI Button is the cleaner, more maintainable approach. UGUI elements natively work with parent-child Transform relationships, so your button will automatically inherit the panel's position, scaling, and visibility:

  1. Create the Button: Right-click your Panel in the Hierarchy → UI → Button. This automatically makes the button a child of the panel.
  2. Add Click Logic: Create a simple script to handle the button click:
    using UnityEngine;
    
    public class ButtonClickHandler : MonoBehaviour
    {
        public void OnMyButtonClicked()
        {
            Debug.Log("Clicked");
        }
    }
    
  3. Link the Script: Attach this script to any GameObject (like the Panel itself), then in the Button's Inspector, drag that GameObject into the OnClick() event slot. Select ButtonClickHandler → OnMyButtonClicked() from the dropdown.

Why This Is Better:

  • No coordinate conversion hassle—everything works within Unity's standard UI system.
  • You get all UGUI features for free: auto-layout, state transitions (hover/pressed), easy styling, and better performance for complex UIs.

内容的提问来源于stack exchange,提问作者zyonneo

火山引擎 最新活动