62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace ZXL.Scripts.UI
|
|
{
|
|
public class ModelControls : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
private bool isCanControl;
|
|
public Transform parent;
|
|
public float speed = 2;
|
|
|
|
private void Update()
|
|
{
|
|
if (isCanControl)
|
|
{
|
|
if (Input.GetMouseButton(0)) // 左键
|
|
{
|
|
var x = -Input.GetAxis("Mouse Y") * speed;
|
|
var y = Input.GetAxis("Mouse X") * speed;
|
|
|
|
if (x > 0 && parent.eulerAngles.x >= 90 && parent.eulerAngles.x < 180)
|
|
{
|
|
x = 0;
|
|
parent.eulerAngles = new Vector3(90, parent.eulerAngles.y, 0);
|
|
}
|
|
else if (x < 0 && (parent.eulerAngles.x <= 0 || parent.eulerAngles.x > 180))
|
|
{
|
|
x = 0;
|
|
parent.eulerAngles = new Vector3(0, parent.eulerAngles.y, 0);
|
|
}
|
|
|
|
parent.eulerAngles += new Vector3(x, y, 0);
|
|
}
|
|
else if (Input.GetMouseButton(1)) // 右键
|
|
{
|
|
var x = -Input.GetAxis("Mouse Y");
|
|
var y = Input.GetAxis("Mouse X");
|
|
parent.position += new Vector3(-y, x, 0);
|
|
}
|
|
|
|
parent.Translate(new Vector3(0, 0, Input.mouseScrollDelta.y));
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
isCanControl = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isCanControl = false;
|
|
}
|
|
|
|
public void ResetCamera()
|
|
{
|
|
parent.position = Vector3.zero;
|
|
parent.rotation = Quaternion.identity;
|
|
}
|
|
}
|
|
} |