using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
Color color = Color.blue;
Color defaultcolor;
private float fileviewdefault = 60;
//-----------------------------------
public float eulery;
public float eulerx;
Vector3 angler;
//Texture2D这里是为了更改鼠标图标所以在设置图片时 Texture2D类型选择Cursor(如果Texture2D为空那么鼠标就是默认的小箭头)
public Texture2D normalCursor,touch;
// Use this for initialization
void Start () {
//首先获取该物体的默认渲染颜色
defaultcolor= transform.GetComponent<MeshRenderer>().sharedMaterial.color;
angler = transform.eulerAngles;
eulery = angler.y;
eulerx = angler.x;
}
//鼠标接触
private void OnMouseEnter()
{
transform.GetComponent<MeshRenderer>().material.color = color;
}
//鼠标离开
private void OnMouseExit()
{
transform.GetComponent<MeshRenderer>().material.color = defaultcolor;
Cursor.SetCursor(touch, Vector2.zero, CursorMode.Auto);
}
//鼠标持续接触 持续更新
private void OnMouseOver()
{
// Cursor是鼠标图标的一个类 这里可以设置替换鼠标图标
Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto);
//fieldOfView是主摄像机上的视距实现的功能是 鼠标放在该物体上通过滚轮调节视距
Camera.main.fieldOfView = Camera.main.fieldOfView + Input.GetAxis("Mouse ScrollWheel") * 20;
//当一直按下鼠标右键 eulery eulerx会随鼠标的移动数值发生变化
if (Input.GetMouseButton(1))
{
eulery += Input.GetAxis("Mouse X") * 10;
eulerx += Input.GetAxis("Mouse Y") * 10;
}
//当按下 F1视距拉回原来的默认视距
if (Input.GetKeyDown(KeyCode.F1))
{
Camera.main.fieldOfView = fileviewdefault;
}
}
//之所以用协程是因为 unity所有线程必须在主协程调用,否怎你试试unity会卡死在 while的循环中
IEnumerator OnMouseDown()
{
Vector3 wts = Camera.main.WorldToScreenPoint(transform.position);
Vector3 stw = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, wts.z));
var offets = transform.position - stw;
//当一直按下鼠标左键时执行||此时实现的功能是左键拖动物体移动
while (Input.GetMouseButton(0))
{
Vector3 v1 = Camera.main.WorldToScreenPoint(transform.position);
Vector3 v2 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, v1.z));
transform.position = v2 + offets;
yield return new WaitForFixedUpdate();
}
}
//这里接收eulery eulerx值,并插入transform 的旋转插值动画中从而实现360鼠标右键拖动旋转
private void LateUpdate()
{
eulerx = Mathf.Clamp(eulerx, -60, 60);
Quaternion q = Quaternion.Euler(eulerx, -eulery, 0);
// transform.rotation = q;
transform.rotation = Quaternion.Lerp(transform.rotation,q,Time.deltaTime*5);
}
}
鼠标控制物体移动360旋转和视距
最新推荐文章于 2023-11-25 14:29:02 发布