using UnityEngine;
using UnityEngine.UI;
public class MouseLook : MonoBehaviour
{
//UI窗口
public GameObject UI = null;
float speed = 0.2f;
float x = 0f;
float y = 0f;
float minY = -75f;
float maxY = 75f;
Touch touch;
float moveX = 0f;
float moveY = 0f;
void Update()
{
if (Input.touchCount >= 1)
{
touch = Input.GetTouch(0);
}
//GetMouseButton(0) 可用于判断touch(0)
if (Input.GetMouseButton(0))
{
#region 判断touch在UI窗口内
//不用的话注释掉这一段
moveX = Mathf.Abs(touch.position.x - UI.transform.position.x);
moveY = Mathf.Abs(touch.position.y - UI.transform.position.y);
if (moveX >= 480)
{
return;
}
if (moveY >= 240)
{
return;
}
#endregion
x = transform.localEulerAngles.y + touch.deltaPosition.x * speed;
y += touch.deltaPosition.y * speed;
//角度限制
y = Mathf.Clamp(y, minY, maxY);
transform.localEulerAngles = new Vector3(y * -1f, x, 0);
}
}
}
窗口内的触摸用来旋转Camera,UI是一个RawImage贴上RenderTexture,同步了Camera的画面