我已将脚本附加到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();
}
}
点击个人资料图片时,总是会点击覆盖图片。
请注意: 重叠图像会填满整个屏幕。
答案 0 :(得分:0)
通常,我强烈建议不要使用OnGUI
而是使用Unity.Ui!
但是,您可以例如将GUI图形和bool的设置与反应代码的执行分开:
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");
}
}
}
答案 1 :(得分:0)
感谢您的回答。
最后我用
解决了GUI.BeginGroup属性。
我将个人资料按钮作为子级添加到了叠加图像中。
还在Rect上方创建了一个Fake按钮,用于单击触发。代码是这样的
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();
}