我有这个脚本,我创建检测向上和向下滑动。它所需要做的就是改变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
}
}
它不工作,我不明白为什么?请帮忙好吗?
只是为了确保,你是在实际设备上还是在编辑器中测试触摸?代码应该在设备上工作,但据我所知,在编辑器中不会这样做。
您需要使用
检查输入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
。
这是我在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