我创建了这个脚本来检测UP和DOWN滑动。它需要做的就是更改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
}
}
它无法正常工作,我无法理解为什么?有什么帮助吗?
答案 0 :(得分:0)
为了确保,您是否正在测试实际设备或编辑器中的触摸?代码应该在设备上工作,但在我的知识中不会在编辑器中这样做。
您需要使用
检查输入
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
相反。
答案 1 :(得分:0)
这是我在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