鸟语天空
UGUI—触摸控制物体旋转缩放
post by:追风剑情 2019-10-30 10:04

示例

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
    }
}

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容