【问题标题】:Unity Android Touch Controls Not Working - 2DUnity Android Touch Controls 不工作 - 2D
【发布时间】:2016-11-15 01:52:09
【问题描述】:

我创建了这个脚本来检测向上和向下滑动。它需要做的就是更改 UI 文本..

脚本如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TouchControls : MonoBehaviour 
{
    private Vector2 startPos;

    Text instruction;

    void start() {
        instruction = GetComponent<Text>();
    }

    float swipeValue = 0.0f;
    void Update()
    {
        #if UNITY_ANDROID
        if (Input.touchCount > 0){
                Touch touch = Input.touches[0];
                if(touch.phase == TouchPhase.Began){
                    startPos = touch.position;
                }
                else if(touch.phase == TouchPhase.Ended){

                    swipeValue = Mathf.Sign(touch.position.y - startPos.y);

                    if (swipeValue > 0){//up swipe

                        instruction.text="UP";
                    }   
                    else if (swipeValue < 0){//down swipe

                        instruction.text="DOWN";
                    }
                }
            }
        #endif
        }
    }

它不工作,我不明白为什么?有什么帮助吗?

【问题讨论】:

    标签: android unity3d 2d


    【解决方案1】:

    只是为了确保,您是在实际设备上还是在编辑器中测试触摸?该代码应该可以在设备上运行,但据我所知,在编辑器中不会这样做。
    您需要使用
    Input.GetMouseButtonUp(0); //returns true if the button has been released
    Input.GetMouseButtonDown(0); //returns true if the button has been pressed 检查输入

    Vector3 touch = Input.mousePosition; //returns the mouse position
    改为。

    【讨论】:

    • 我正在实际设备上进行测试... :)
    • 那么请尝试使用Touch touch = Input.touches[0]; --> 触摸触摸 = Input.GetTouch(0); Input.touches 似乎有点奇怪,我无法向您解释,但 GetTouch 肯定对我有用,除非您的代码中有其他错误。哦,还要对 EQUALS = in: if (swipeValue >= 0){ 进行测试,以确保在 swipeValue 为 0 的情况下触发 if 语句。
    【解决方案2】:

    这是我在 Unity 版本 5.1.1 上解决的问题的解决方案

    void Update () {
    
            #if UNITY_IOS || UNITY_ANDROID
            //iOS Controls (same as Android because they both deal with screen touches)
            //if the touch count on the screen is higher than 0, then we allow stuff to happen to control the player.
    
                    if(Input.touchCount > 0){
                        foreach(Touch touch1 in Input.touches) {
                            if (touch1.phase == TouchPhase.Began) {
                                firstPosition = touch1.position;
                                lastPosition = touch1.position;
                            }
                            if (touch1.phase == TouchPhase.Moved) {
                                lastPosition = touch1.position;
                            }
                            if (touch1.phase == TouchPhase.Ended) {             
    
                                if (firstPosition.y - lastPosition.y < -80) {
                                    //Up
                                }  else {
                                    //Down
                                }
    
                            }   
    
                        }
    
                #endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      相关资源
      最近更新 更多