把原来光标隐藏 找一个图片放在中间移动完毕真实鼠标光标显示出来
//******************************************************************
//
// 文件名(File Name): ViewControl.cs
//
// 功能描述(Description): 光标不动旋转摄像机
//
// 作者(Author): sqw
//
// 日期(Create Date): 2019.10.10
//
//******************************************************************
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ViewControl:MonoBehaviour
{
public static extern int SetCursorPos(int x, int y);
enum RotationAxes
{
MouseXAndY,
MouseX,
MouseY
}
RotationAxes axes = RotationAxes.MouseXAndY;
float sensitivityX = 10;
float sensitivityY = 10;
float minimumY = -60;
float maximumY = 60;
private float rotationY = 0;
public Image cursor;
private void Start()
{
}
public void Update()
{
if (Input.GetMouseButton(0))
{
Cursor.lockState = CursorLockMode.Locked;//锁定指针到视图中心
Cursor.visible = false;//隐藏指针
Cursor.visible = false;
cursor.enabled = true;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = Camera.main.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Camera.main.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
Camera.main.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Camera.main.transform.localEulerAngles = new Vector3(-rotationY, Camera.main.transform.localEulerAngles.y, 0);
}
}
if (Input.GetMouseButtonUp(0))
{
Cursor.visible = true;//显示指针
Cursor.lockState = CursorLockMode.None;//锁定指针到视图中心
cursor.enabled = false;
}
}
}