Touch.phase

Unity · Версия 2020.3 LTS · Scripting · Touch

Touch.phase

public TouchPhase phase;

Описание

Описывает фазу касания.

Этап касания phase относится к действию, которое палец выполнил при последнем обновлении кадра. Поскольку касание отслеживается устройством в течение его «жизненного цикла», о начале и конце касания, а также о движениях между ними можно сообщать в кадрах, в которых они происходят. Свойство phase можно использовать как основу оператора "switch" или как часть более сложной системы обработки состояний.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Vector2 startPos;
    public Vector2 direction;
    public bool directionChosen;
    void Update()
    {
        // Track a single touch as a direction control.
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            // Handle finger movements based on touch phase.
            switch (touch.phase)
            {
                // Record initial touch position.
                case TouchPhase.Began:
                    startPos = touch.position;
                    directionChosen = false;
                    break;

                // Determine direction by comparing the current touch position with the initial one.
                case TouchPhase.Moved:
                    direction = touch.position - startPos;
                    break;

                // Report that a direction has been chosen when the finger is lifted.
                case TouchPhase.Ended:
                    directionChosen = true;
                    break;
            }
        }
        if (directionChosen)
        {
            // Something that uses the chosen direction...
        }
    }
}

Обновлено 17 ноября 2026 г.