我已将脚本附加到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();
    }
}

每当我单击个人资料图像时,总是会调用“覆盖图像单击”。

请注意:
重叠图像会填满整个屏幕。

我在这里附上我的应用程序的屏幕截图。任何帮助将不胜感激。 c# - 按钮单击在 Vuforia AR 相机统一中不起作用-LMLPHP

最佳答案

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

但是,您可以例如将 GUI 绘图和 bool 的设置与 react 代码的执行分开:

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");
        }
    }
}

关于c# - 按钮单击在 Vuforia AR 相机统一中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52385640/

10-11 21:34
查看更多