Button Click not working in Vuforia AR camera unity

Viewed 801

I have attached a script to Vuforia AR camera component inorder to add buttons to Ar camera surface .

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

Overlay Image click is always being called whenever I clicks the profile image.

To your note: Overlay Image fills the entire screen.

I am attaching the screenshot of my app here. Any help would be greatly appreciated.enter image description here

2 Answers

In general I strongly recommend to not use OnGUI but rather the Unity.Ui!


However, you could e.g. separate the GUI drawing and the setting of the bools from the executions of the reaction code:

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

Thanks for your answers.

Finally i solved it using

GUI.BeginGroup attribute.

I added the profile button as a child to the overlay image.

Also created a Fake button above the Rect which is used for click trigger. The code goes like this

    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();
            }
Related