You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.8 KiB
130 lines
3.8 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class RotateRound : MonoBehaviour, IPointerDownHandler,IDragHandler,IPointerUpHandler,IPointerEnterHandler,IPointerExitHandler
|
|
{
|
|
public Transform _camera;
|
|
public Transform target;
|
|
|
|
private Vector2 mouseClickPos = Vector2.zero;
|
|
private Vector2 mouseLastPos = Vector2.zero;
|
|
private Vector3 cameraPos;
|
|
private float _viewDistance;
|
|
float speed = 15f;
|
|
int roteDir = 0;
|
|
|
|
public bool mouseWheelOpen = false;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
cameraPos.x = _camera.localPosition.x;
|
|
cameraPos.y = _camera.localPosition.y;
|
|
cameraPos.z = _camera.localPosition.z;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
|
|
if (mouseWheelOpen)
|
|
{
|
|
//鼠标滚轮控制远近
|
|
if (Input.GetAxis("Mouse ScrollWheel") != 0)
|
|
{
|
|
_viewDistance = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 100f*50;
|
|
_viewDistance = Mathf.Clamp(_viewDistance, -100, 100);
|
|
|
|
cameraPos =_camera.localPosition+ _camera.forward.normalized * _viewDistance;
|
|
|
|
_camera.localPosition = cameraPos;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
|
|
mouseClickPos = Input.mousePosition;
|
|
mouseLastPos = mouseClickPos;
|
|
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
//鼠标左键控制左右,上下轴旋转
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
Vector2 TempMousePos = Input.mousePosition;
|
|
float x_Rote = TempMousePos.x - mouseClickPos.x;
|
|
float y_Rote = TempMousePos.y - mouseClickPos.y;
|
|
|
|
if (Mathf.Abs(x_Rote) > 5f&&roteDir==0)
|
|
{
|
|
//方向锁
|
|
roteDir = 1;
|
|
|
|
}else if (Mathf.Abs(y_Rote) > 5f && roteDir == 0)
|
|
{
|
|
//方向锁
|
|
roteDir = 2;
|
|
}
|
|
|
|
switch (roteDir)
|
|
{
|
|
case 1:
|
|
x_Rote = TempMousePos.x - mouseLastPos.x;
|
|
_camera.transform.RotateAround(target.position, Vector3.up, x_Rote * speed * Time.deltaTime);
|
|
mouseLastPos.x = TempMousePos.x;
|
|
break;
|
|
case 2:
|
|
y_Rote = TempMousePos.y - mouseLastPos.y;
|
|
_camera.transform.RotateAround(target.position, Vector3.right, y_Rote * speed * Time.deltaTime);
|
|
|
|
mouseLastPos.y = TempMousePos.y;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
//鼠标滚轮平移视角
|
|
if (Input.GetMouseButton(2))
|
|
{
|
|
Vector2 TempMousePos = Input.mousePosition;
|
|
float x_move = TempMousePos.x - mouseClickPos.x;
|
|
float y_move = TempMousePos.y - mouseClickPos.y;
|
|
|
|
if(Mathf.Abs(x_move)>2f|| Mathf.Abs(y_move) > 2f)
|
|
{
|
|
x_move = TempMousePos.x - mouseLastPos.x;
|
|
y_move = TempMousePos.y - mouseLastPos.y;
|
|
_camera.transform.localPosition += new Vector3(x_move * Time.deltaTime * speed*3, -y_move * Time.deltaTime * speed*3, 0);
|
|
mouseLastPos.x = TempMousePos.x;
|
|
mouseLastPos.y = TempMousePos.y;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
mouseClickPos = Vector2.zero;
|
|
mouseLastPos = Vector2.zero;
|
|
roteDir = 0;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
mouseWheelOpen = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
mouseWheelOpen = false;
|
|
}
|
|
}
|