UGUI—变换UI元素(UITouchTransform)

作者:追风剑情 发布于:2020-10-10 17:21 分类:Unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 对RectTransform进行控制
/// 双指缩放、单指移动
/// </summary>
public class UITouchTransform : MonoBehaviour
{
    //要控制的目标对象
    [SerializeField]
    private RectTransform m_Target;
    //定义可交互区域
    [SerializeField]
    private RectTransform m_InteractableRect;
    //UI相机
    [SerializeField]
    private Camera m_UICamera = null;
    //触摸缩放速度
    [SerializeField]
    private float m_TouchScaleSpeed = 0.01f;
    //最小缩放值
    [SerializeField]
    private float m_MinScale = 0.1f;
    //最大缩放值
    [SerializeField]
    private float m_MaxScale = 2f;
    //是否允许拖动
    [SerializeField]
    private bool m_AllowDrag = true;
    //是否允许缩放
    [SerializeField]
    private bool m_AllowScale = true;
    private float m_LastDistance = 0;
    private Vector2 m_lastPosition = Vector2.zero;

    void Awake()
    {
        if (m_Target == null)
            m_Target = this.GetComponent<RectTransform>();
    }

    void Update()
    {
        if (!Input.touchSupported) //设备是否支持触摸
            return;
        int touchCount = Input.touchCount;
        if (touchCount <= 0)
            return;
        switch (touchCount)
        {
            case 1: TouchDrag(); break;
            case 2: TouchScale(); break;
        }
    }

    private void TouchDrag()
    {
        if (!m_AllowDrag)
            return;

        //判断手指是否在允许操作的区域内操作
        if (m_InteractableRect != null &&
            !RectTransformUtility.RectangleContainsScreenPoint(m_InteractableRect, Input.touches[0].position, m_UICamera))
            return;

        Vector2 deltaPosition = Input.touches[0].deltaPosition;
        if (Input.touches[0].phase == TouchPhase.Moved)
        {
            m_Target.anchoredPosition += deltaPosition;
        }
    }

    private void TouchScale()
    {
        if (!m_AllowScale)
            return;

        //判断手指是否在允许操作的区域内操作
        if (m_InteractableRect != null &&
            !RectTransformUtility.RectangleContainsScreenPoint(m_InteractableRect, Input.touches[0].position, m_UICamera) &&
            !RectTransformUtility.RectangleContainsScreenPoint(m_InteractableRect, Input.touches[1].position, m_UICamera))
            return;

        Vector3 firstTouch = Input.touches[0].position;
        Vector3 secondTouch = Input.touches[1].position;
        float twoTouchDistance = Vector2.Distance(firstTouch, secondTouch);
        if (Input.touches[1].phase == TouchPhase.Began)
            m_LastDistance = twoTouchDistance;
        else if (Input.touches[1].phase == TouchPhase.Ended)
            //双指变单指的情况,不会触发touches[0]的Began阶段
            m_lastPosition = Input.touches[0].position;
        else if (Input.touches[0].phase == TouchPhase.Ended)
            //双指变单指的情况,不会触发touches[1]的Began阶段
            m_lastPosition = Input.touches[1].position;

        float scale = Mathf.Clamp(m_Target.localScale.x + (twoTouchDistance - m_LastDistance) * m_TouchScaleSpeed, m_MinScale, m_MaxScale);
        m_Target.localScale = new Vector3(scale, scale, scale);
        m_LastDistance = twoTouchDistance;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号