Нажатие кнопки не работает в Vuforia AR camera unity

Я прикрепил скрипт к компоненту камеры 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();
    }
}

Щелчок по наложенному изображению всегда вызывается всякий раз, когда я нажимаю на изображение профиля.

На заметку: оверлейное изображение заполняет весь экран.

Прилагаю сюда снимок экрана своего приложения. Мы будем благодарны за любую помощь.  введите описание изображения здесь


person Kanagalingam    schedule 18.09.2018    source источник
comment
Я предполагаю, что с not working вы имеете в виду, что щелчок наложения вызывается, хотя вы хотите, чтобы выполнялся только щелчок по кнопке профиля?   -  person derHugo    schedule 18.09.2018


Ответы (2)


В общем, я настоятельно рекомендую не использовать OnGUI, а использовать Unity.Ui!


Однако вы могли бы, например, отделите рисунок графического интерфейса пользователя и настройку логических значений от выполнения кода реакции:

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");
        }
    }
}
person derHugo    schedule 18.09.2018

Спасибо за ответы.

Наконец я решил это, используя

Атрибут 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();
            }
person Kanagalingam    schedule 19.09.2018