我正在开发一款安卓游戏,我有一个可以左右移动的正交相机,上下触摸,我创建了一个gui按钮。当我触摸任何可以移动相机的地方时,我将apk导出到设备,但是当我触摸我创建的gui按钮时,相机也会移动。我希望当我点击按钮时,相机停止移动,当我触摸屏幕时,相机会移动。当我触摸屏幕时,我可以移动相机,然后双击按钮。我创建了一个boolean [ButtonPressed]但是当我点击GUI按钮时它没有工作相机移动这里是我的代码:
Touch touch;
public Vector2 startPos;
Vector2 endPos;
public bool fingerHold = false;
public bool ButtonPressed = false;
void Update()
{
if(!ButtonPressed)
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startPos = touch.position;
fingerHold = true;
}
else if (touch.phase == TouchPhase.Moved)
{
endPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
fingerHold = false;
}
}
if (fingerHold)
{
float deltaX = endPos.x - startPos.x;
float deltaY = endPos.y - startPos.y;
bool horizontal = false;
if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
horizontal = true;
if (horizontal)
{
if (deltaX < 0 )
transform.Translate(Vector3.left * Time.deltaTime * 20);
else if (deltaX > 0)
transform.Translate(Vector3.right * Time.deltaTime * 20);
}
else
{
if (deltaY < 0)
transform.Translate(Vector3.down * Time.deltaTime * 20);
else if (deltaY > 0)
transform.Translate(Vector3.up * Time.deltaTime * 20);
}
}
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 158, 54), "Click Button"))
{
ButtonPressed = true;
Print("Button Clicked");
}
}
感谢您的帮助。
答案 0 :(得分:0)
您可以检查您的触摸是否在按钮边界内,
if (touch.phase == TouchPhase.Began)
{
startPos = touch.position;
// if we touch inside the buttons bounds get out of here!
if(startPos.x < 168 && startPos.y < 64) return;
fingerHold = true;
}