这是我的问题,我有一个相机,我在Android设备中使用Touch向右,向左,向下和向上移动,我有一个gui按钮,当我双击它时向我显示文本。当我触摸gui按钮时,相机移动的问题。我想当我触摸gui按钮时,相机停止移动,当我触摸屏幕中的任何其他位置时,相机会移动。
这是我的代码:
public float tapSpeed = 0.5f;
private float lastTapTime = 0;
public Touch touch;
public Vector2 startPos;
public Vector2 endPos;
public bool fingerHold = false;
private float CameraYLimitUp;
private float CameraYLimitDown;
private float CameraXLimitRight;
private float CameraXLimitLeft;
public GUIText guiTextTap;
void Awake()
{
CameraYLimitUp = 0;
CameraYLimitDown = 27;
CameraXLimitRight = -15;
CameraXLimitLeft = 22;
}
void Update()
{
// Camera moves left right up down with touch
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startPos = touch.position;
fingerHold = true;
print("touch began");
}
else if (touch.phase == TouchPhase.Moved)
{
endPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
fingerHold = false;
print("touch end");
}
}
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.position.x < CameraXLimitLeft)
transform.Translate(Vector3.left * Time.deltaTime * 20);
else if (deltaX > 0 && transform.position.x > CameraXLimitRight)
transform.Translate(Vector3.right * Time.deltaTime * 20);
}
else
{
if (deltaY < 0 && transform.position.y < CameraYLimitDown)
transform.Translate(Vector3.down * Time.deltaTime * 20);
else if (deltaY > 0 && transform.position.y > CameraYLimitUp)
transform.Translate(Vector3.up * Time.deltaTime * 20);
}
}
}
void OnGUI()
{
if (GUI.Button(new Rect(480 , 289.5f , 100, 100), "GUI Test"))
{
if ((Time.time - lastTapTime) < tapSpeed)
{
Debug.Log("Gui Button Taped");
guiTextTap.text = "Gui Button Taped";
}
lastTapTime = Time.time;
}
}
我上传了我的项目,你可以在这里找到它:https://mega.co.nz/#!0EkHQRDI!yDcUfJR_B5poXokku7fExOc-NtlDyYDeTCaBiAePzMs
非常感谢你的帮助
答案 0 :(得分:0)
检查GUI热控制ID将解决该问题,即如果GUIUtility.hotControl的值大于0,则表示用户按下按钮。因此,当用户按下按钮时,您可以简单地修改if语句,如下所示,以绕过相机移动:
void Update()
{
...
...
// Check additionally if the GUI button is currently free
if (GUIUtility.hotControl == 0 && fingerHold)
{
...
...
}
}