Unity 物体 模型的 放大缩小功能代码(多种情况)

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

Unity 物体 模型的 放大缩小功能代码(多种情况)_第1张图片


1.pc端通过鼠标中键调整相机的FieldOfView属性的值(会发生形变不建议)

  void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
                Camera.main.fieldOfView += 2;
            Camera.main.fieldOfView = Camera.main.fieldOfView >= 90 ? 90 : Camera.main.fieldOfView;

                if (Camera.main.orthographicSize <= 20)
                Camera.main.orthographicSize += 0.5F;
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
           
                Camera.main.fieldOfView -= 2;
            Camera.main.fieldOfView = Camera.main.fieldOfView <= 30 ? 30 : Camera.main.fieldOfView;

            if (Camera.main.orthographicSize >= 1)
                Camera.main.orthographicSize -= 0.5F;
        }
        // 鼠标轴控制相机的远近
        //if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
        //{
        //    Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
        //}
}

 

2.移动端通过控制模型的Scale属性

 public static Vector3 currentScale;

    private Touch oldTouch1;
    private Touch oldTouch2;
 
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            //单指滑动旋转
            if (Input.touchCount == 1)
            {
                Touch touch = Input.GetTouch(0);
                Vector2 deltaPos = touch.deltaPosition;
                transform.Rotate(Vector3.down * deltaPos.x, Space.Self);
            }

            if (Input.touchCount == 2)
            {
                //缩放
                Touch newTouch1 = Input.GetTouch(0);

                Touch newTouch2 = Input.GetTouch(1);

                if (newTouch2.phase == TouchPhase.Began)
                {
                    oldTouch2 = newTouch2;
                    oldTouch1 = newTouch1;

                    return;
                }
                float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
                float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

                float offset = newDistance - oldDistance;

                float scaleFactor = offset / 200f;

                Vector3 localScale = transform.localScale;

                Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor);
                //限制最低最高值
                if ((scale.x >= 0.5f && scale.x <= 3) && (scale.y >= 0.5f && scale.y <= 3f) && (scale.z >= 0.5f && scale.z <= 3f))
                {
                    transform.localScale = scale;
                    currentScale = scale;
                }
                oldTouch1 = newTouch1;
                oldTouch2 = newTouch2;
            }
        }
    }

3.还有一种移动相机与对象的距离

 public Transform target;
 Vector3 offset;
 void Start()
    {
        offset = transform.position - target.position;
    }
 void Update()
    {
        transform.position = target.position + offset;
          float dis = offset.magnitude;
        dis += Input.GetAxis("Mouse ScrollWheel") * 5;
        Debug.Log("dis=" + dis);
        if (dis < 10 || dis > 40)
        {
            return;
        }
        offset = offset.normalized * dis;
    }

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

Unity 物体 模型的 放大缩小功能代码(多种情况)_第2张图片

你可能感兴趣的:(c#,/java,Unity移动端相关技术,工具类)