• 大小: 7KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签: u3d  脚本  拖拽  缩放  旋转  

资源简介

u3d拖拽旋转缩放模型脚本: 拖放到要操作的模型即可 拖放到要操作的模型即可

资源截图

代码片段和文件信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DragRoteScale : MonoBehaviour
{
    public float scaleRate = 0.01f;         //缩放系数
    public float scaleMin = 0.1f;           //最小缩放
    public float scaleMax = 10.0f;          //最大缩放

    public bool isRotateY = true;   //是否绕y轴旋转
    public bool isRotateX = true;   //是否绕X轴旋转
    public float rotateRate = 1.0f; //旋转系数

    public Transform transform = null;  //要操作的物体若直接挂在物体上不用设置

    private Touch oldTouch1;  //上次触摸点1(手指1)
    private Touch oldTouch2;  //上次触摸点2(手指2)

    private bool isDrag = false;        //拖拽标志
    private Vector3 m_Offset;           //偏移值
    private Vector3 m_TargetScreenVec;  //当前物体对应的屏幕坐标

    private bool isRote = false;        //是否旋转
    private Vector2 oldMousePos;        //鼠标上次位置


    void Start()
    {
        if (transform == null)
        {
            transform = gameobject.transform;
        }
    }

    void Update()
    {

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            if (Input.touchCount <= 0)  //没有触摸
                return;

            Touch touch1 = new Touch();
            Touch touch2 = new Touch();

            if (Input.touchCount >= 1)
                touch1 = Input.GetTouch(0);

            if (Input.touchCount >= 2)
                touch2 = Input.GetTouch(1);


            if (Input.touchCount == 1)//单指
            {
                if (touch1.phase == TouchPhase.Began)//开始单击
                {
                    Ray ray = Camera.main.ScreenPointToRay(touch1.position);
                    //hit用来存储碰撞物体的信息   
                    RaycastHit hit;
                    //ray表示射线,hit存储物体的信息100为设定射线发射的距离   
                    if (Physics.Raycast(ray out hit 100) && hit.transform == transform) //自身被点中
                    {
                        isDrag = true;
                        //当前物体对应的屏幕坐标
                        m_TargetScreenVec = Camera.main.WorldToScreenPoint(transform.position);
                        //偏移值=物体的世界坐标,减去转化之后的鼠标世界坐标(z轴的值为物体屏幕坐标的z值)
                        m_Offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(touch1.position.x touch1.position.y m_TargetScreenVec.z));
                    }
                    else
                        isDrag = false;
                }
                else
                {
                    if (isDrag)  //拖拽
                    {
                        //当前坐标等于转化鼠标为世界坐标(z轴的值为物体屏幕坐标的z值)+ 偏移量
                        transform.position = Camera.main.ScreenToWorldPoint(new Vector3(touch1.position.x touch1.position.y m_TargetScreenVec.z)) + m_Offset;
                    }
                    else        //旋转
                    {
                        if (isRotateY)
                            transform.Rotate(Vector3.down * touch1.deltaPosition.x * rotat

评论

共有 条评论