UGUI—触摸控制物体旋转缩放

作者:追风剑情 发布于:2019-10-30 10:04 分类:Unity3d

示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
/// <summary>
/// 通过手指触摸或鼠标拖拽控制3D模型旋转或缩放
/// </summary>
public class UITouchTransform : MonoBehaviour, IDragHandler
{
    public Transform target;
    [SerializeField]
    private TouchRotate touchRotate;
    [SerializeField]
    private float sensitivity = 1f;
    private Vector3 rotate = Vector3.zero;

    public void OnDrag(PointerEventData eventData)
    {
        if (target == null)
            return;
        Vector2 delta = eventData.delta * sensitivity;
        rotate.x = rotate.y = rotate.z = 0;
        switch (touchRotate)
        {
            case TouchRotate.AxisX:
                rotate.y = delta.x;
                break;
            case TouchRotate.AxisY:
                rotate.x = delta.y;
                break;
            case TouchRotate.AxisXY:
                rotate.y = delta.x;
                rotate.x = delta.y;
                break;
        }
        target.Rotate(rotate);
    }

    private enum TouchRotate
    {
        AxisX,
        AxisY,
        AxisXY
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号