鼠标控制物体缩放&旋转

作者:追风剑情 发布于:2019-5-29 11:10 分类:Unity3d

示例


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 鼠标控制物体缩放&旋转
/// 注意:某些软件会造成按键冲突
/// </summary>
public class MouseScaler : MonoBehaviour {

    public Transform target; //目标对象
    public float dragThreshold = 2f; //拖曳阈值
    public float rotationFactor = 0.1f;//旋转因子
    public float scaleFactor = 0.01f; //缩放因子
    public float minScaleMultiple = 0.5f;//最小缩放倍数
    public float maxScaleMultiple = 3f; //最大缩放倍数
    public bool interactable = false; //是否可交互

    private Vector2 downPos;
    private bool isRotation;
    private bool isScale;
    private bool isDownControl;
    private float offsetX = 0f;
    private float offsetY = 0f;

    private Vector3 downRotation;
    private Vector3 downScale;
    private Vector3 originScale = Vector3.one;
    private Quaternion originRotation = Quaternion.identity;

    private static MouseScaler _instace;
    public static MouseScaler Instace { get { return _instace; } }

    private Vector3 tempVec3 = Vector3.zero;

    private void Awake()
    {
        _instace = this;
        if (target != null)
        {
            originScale = transform.localScale;
        }
    }

    private void OnApplicationFocus(bool focus)
    {
        if (!focus)
        {
            isDownControl = false;
            isRotation = false;
            isScale = false;
        }
    }

    private void Update()
    {
        if (!interactable)
            return;

        if (target == null)
            return;

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            isDownControl = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            isDownControl = false;
        }

        if (Input.GetMouseButtonDown(0))
        {
            downScale = target.localScale;
            downPos = Input.mousePosition;
            isScale = true;
        }
        else if (Input.GetMouseButtonDown(1))
        {
            downRotation = target.localEulerAngles;
            downPos = Input.mousePosition;
            isRotation = true;
        }

        if (Input.GetMouseButtonUp(0))
        {
            isScale = false;
        }
        else if (Input.GetMouseButtonUp(1))
        {
            isRotation = false;
        }

        if (!isDownControl)
            return;

        offsetX = Input.mousePosition.x - downPos.x;
        offsetY = Input.mousePosition.y - downPos.y;

        if (isRotation && (Mathf.Abs(offsetX) > dragThreshold || Mathf.Abs(offsetY) > dragThreshold))
        {
            tempVec3.z = downRotation.z - offsetY * rotationFactor;
            tempVec3.y = downRotation.y - offsetX * rotationFactor;
            target.rotation = Quaternion.Euler(tempVec3);
        }

        if (isScale)
        {
            if (Mathf.Approximately(0, offsetX))
            {
                tempVec3 = downScale;
            }
            else if (offsetX > 0)
            {
                float scale = 1 + offsetX * scaleFactor;
                tempVec3 = downScale * scale;
                if (tempVec3.x > originScale.x * maxScaleMultiple)
                {
                    tempVec3 = originScale * maxScaleMultiple;
                }
            }
            else if (offsetX < 0)
            {
                float scale = 1 + Mathf.Abs(offsetX) * scaleFactor;
                tempVec3 = downScale * 1 / scale;
                if (tempVec3.x < originScale.x * minScaleMultiple)
                {
                    tempVec3 = originScale * minScaleMultiple;
                }
            }

            target.localScale = tempVec3;
        }
    }

    public void SetTarget(Transform target)
    {
        this.target = target;
        this.originScale = target.localScale;
    }

    public void SetTarget(Transform target, float minScaleMultiple, float maxScaleMultiple)
    {
        this.target = target;
        this.originRotation = target.rotation;
        this.originScale = target.localScale;
        this.minScaleMultiple = minScaleMultiple;
        this.maxScaleMultiple = maxScaleMultiple;
    }
}


2222.gif

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号