【发布时间】:2014-09-27 10:37:40
【问题描述】:
我正在开发一个安卓游戏,我有一个正交相机,我可以通过触摸左右移动,上下移动,我创建了一个 gui 按钮。当我触摸可以移动相机的任何地方时,我将 apk 导出到设备,但是当我触摸我创建的 gui 按钮时,相机也会移动。我希望当我单击按钮时相机停止移动,当我触摸屏幕的任何位置时相机会移动。或者当我触摸屏幕并双击按钮时是否有可能移动相机。我创建了一个布尔值 [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");
}
}
感谢您的帮助。
【问题讨论】: