Unity Android 触摸控制不工作 - 2D
Unity Android Touch Controls Not Working - 2D
我创建了这个脚本来检测向上和向下滑动。它需要做的就是更改 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
我创建了这个脚本来检测向上和向下滑动。它需要做的就是更改 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