【问题标题】:Camera movement And GUI Button Unity Android相机移动和 GUI 按钮 Unity Android
【发布时间】:2014-09-29 08:27:40
【问题描述】:

这是我的问题,我有一个相机,我可以在 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

非常感谢您的帮助

【问题讨论】:

    标签: android camera unity3d


    【解决方案1】:

    检查 GUI 热控制 id 将解决该问题,即如果 GUIUtility.hotControl 的值大于 0,则表示用户按住按钮。因此,您可以简单地修改您的 if 语句,以在用户按下按钮时绕过相机移动:

    void Update()
    {
      ...
      ...
    
      // Check additionally if the GUI button is currently free
      if (GUIUtility.hotControl == 0 && fingerHold)
      {
        ...
        ...
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多