Меня также несколько смутило объяснение , которое я прочитал , потому что я пытался добавить свойство к существующему прототипу, который я не писал, поэтому замена прототипа казалась неправильным подходом. Итак, для потомков, вот как я добавил свойство last
в Array
:
Object.defineProperty(Array.prototype, "last", {
get: function() { return this[this.length - 1] }
});
Когда-либо настолько приятнее, чем добавление функции IMHO.
Вот код, который я использовал. Я проверял, и это работает, но иногда я заметил задержку. Дайте мне знать, если это достаточно хорошо для вас. В основном вам не нужно проходить все касания, если вам не нужно мультитач. И вам просто нужны фазы начала и окончания касания.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class touch : MonoBehaviour {
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
private Vector2 currentSwipe;
public Text textbox;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
//save began touch 2d point
startTouchPosition = new Vector2(touch.position.x, touch.position.y);
}
if (touch.phase == TouchPhase.Ended)
{
//save ended touch 2d point
endTouchPosition = new Vector2(touch.position.x, touch.position.y);
//create vector from the two points
currentSwipe = new Vector2(endTouchPosition.x - startTouchPosition.x, endTouchPosition.y - startTouchPosition.y);
//normalize the 2d vector
currentSwipe.Normalize();
if(Mathf.Abs(currentSwipe.y) < 0.1f && Mathf.Abs(currentSwipe.x) < 0.1f)
{
if (touch.position.x > (Screen.width / 2))
{
textbox.text= "jump right";
}
else if (touch.position.x < (Screen.width / 2))
{
textbox.text= "jump left";
}
}
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
textbox.text= "Dash forward";
}
}
}
}
}