Vuforia AR相机Unity中的按钮点击无法工作

5

我已经给Vuforia AR相机组件添加了一个脚本,以便在AR相机表面上添加按钮。

void OnGUI()
{
    if (showComponent)
    {
        bool isOverlayClicked;
        int onePartHeight = Screen.width / 10;
        GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));  // x,y,w,h
        GUI.backgroundColor = Color.clear;
        isOverlayClicked=  GUI.Button(new Rect(0, 0, Screen.width, (Screen.height / 2) + 100 + (onePartHeight * 2)), "");
        GUI.DrawTexture(new Rect(0, 0, Screen.width, (Screen.height / 2) + 100 + (onePartHeight * 2)), btntexture);
        thisMenu.blocksRaycasts = thisMenu.interactable = false;//disallows clicks
        thisMenu.alpha = 0;   //makes menu invisible
        if (isOverlayClicked)
        {
            Debug.Log("Overlay clicked in unity");
        }
        GUILayout.EndArea();  

        bool isProfileButtonClicked;
        GUILayout.BeginArea(new Rect((Screen.width) - (iconSize + (25 * DPinPixels)), iconSize - (30 * DPinPixels), iconSize, iconSize));
        GUI.backgroundColor = Color.clear;
        isProfileButtonClicked = GUI.Button(new Rect(0, 0, iconSize, iconSize), "");
        GUI.DrawTexture(new Rect(0, 0, iconSize, iconSize), profileViewTexture);
        if (isProfileButtonClicked)
        {
            Debug.Log("Profile icon clicked in unity");
            openProfileActivity();
        }
        GUILayout.EndArea();
    }
}

每当我点击个人资料图片时,总是会调用叠加图像单击事件。

请注意: 叠加图像填充整个屏幕。

我在这里附上我的应用程序截图。任何帮助都将不胜感激。enter image description here


我猜你所说的“不起作用”是指虽然你只想执行个人资料按钮点击,但却调用了覆盖层点击? - derHugo
2个回答

0
一般来说,我强烈建议不要使用OnGUI,而是使用Unity.Ui

然而,您可以将GUI绘制和布尔设置与反应代码的执行分开:

void OnGUI()
{
    if (showComponent)
    {

        // First only draw the GUI and set the bools

        bool isOverlayClicked;
        int onePartHeight = Screen.width / 10;
        GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));  // x,y,w,h
        GUI.backgroundColor = Color.clear;
        isOverlayClicked=  GUI.Button(new Rect(0, 0, Screen.width, (Screen.height / 2) + 100 + (onePartHeight * 2)), "");
        GUI.DrawTexture(new Rect(0, 0, Screen.width, (Screen.height / 2) + 100 + (onePartHeight * 2)), btntexture);
        thisMenu.blocksRaycasts = thisMenu.interactable = false;//disallows clicks
        thisMenu.alpha = 0;   //makes menu invisible
        GUILayout.EndArea();  

        bool isProfileButtonClicked;
        GUILayout.BeginArea(new Rect((Screen.width) - (iconSize + (25 * DPinPixels)), iconSize - (30 * DPinPixels), iconSize, iconSize));
        GUI.backgroundColor = Color.clear;
        isProfileButtonClicked = GUI.Button(new Rect(0, 0, iconSize, iconSize), "");
        GUI.DrawTexture(new Rect(0, 0, iconSize, iconSize), profileViewTexture);
        GUILayout.EndArea();


        // Than later only react to the bools
        if (isProfileButtonClicked)
        {
            Debug.Log("Profile icon clicked in unity");
            openProfileActivity();

            // Return so nothing else is executed
            return;
        }

        // This is only reached if the other button was not clicked
        if (isOverlayClicked)
        {
            Debug.Log("Overlay clicked in unity");
        }
    }
}

0

感谢您的回答。

最终我使用了GUI.BeginGroup属性解决了问题。

我将配置文件按钮添加为覆盖图像的子级。

同时创建了一个虚拟按钮,位于用于点击触发的矩形上方。代码如下:

    Rect overlayRect = new Rect(0, 0, Screen.width, Screen.height);
                GUI.BeginGroup(overlayRect);  // x,y,w,h
                GUI.DrawTexture(overlayRect, btntexture);

                Rect profileRect = new Rect((Screen.width) - (iconSize + (5 * DPinPixels)), 10, iconSize, iconSize);
                GUI.DrawTexture(profileRect, profileViewTexture);


if (GUI.Button(profileRect, "", new GUIStyle()))
            {
                openProfileActivity();
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接