using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
using static WebInteraction;
using System;
///
/// 上帝视角
///
[AddComponentMenu("ExtendedFlycam/上帝视角")]
public class ExtendedFlycam : MonoBehaviour
{
static ExtendedFlycam _inst;
public static ExtendedFlycam Inst
{
get
{
if (_inst == null)
{
_inst = new ExtendedFlycam();
}
return _inst;
}
}
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;
///
/// 旋转X增量
///
[HideInInspector] public float rotationX = 0.0f;
///
/// 旋转Y增量
///
[HideInInspector] public float rotationY = 0.0f;
private Vector3 previousMousePosition;
public Vector3 initialRotationEulerAngles; // 保存初始角度
public bool isvalid = true;
private void Awake()
{
_inst = this;
previousMousePosition = Input.mousePosition;
BoolMonitor.ValueChanged += BoolMonitor_ValueChanged;
}
//检测bool是否变化
private static void BoolMonitor_ValueChanged(object sender, EventArgs e)
{
Debug.Log("当前bool" + BoolMonitor.Value);
if (!BoolMonitor.Value)
{
if (!string.IsNullOrEmpty(WebInteraction.Inst.current_videoNumber) && WebInteraction.Inst.isVideoPlay)
WebInteraction.Inst.CloseVideo(WebInteraction.Inst.current_videoNumber);
}
}
private Transform cameraTransform;
private Vector3 lastPosition;
private Quaternion lastRotation;
void Start()
{
Cursor.visible = true;
//initialRotationEulerAngles = transform.localEulerAngles;
// 获取相机的Transform组件
cameraTransform = GetComponent();
// 初始化上一帧的位置和旋转
lastPosition = cameraTransform.position;
lastRotation = cameraTransform.rotation;
}
private void OnEnable()
{
transform.rotation = Quaternion.Euler(initialRotationEulerAngles);
}
public bool HasMouseMoved()
{
float mouseX = Input.GetAxisRaw("Mouse X");
float mouseY = Input.GetAxisRaw("Mouse Y");
if (mouseX != 0 || mouseY != 0)
{
previousMousePosition = Input.mousePosition;
return true;
}
return false;
}
Vector3 ClampAngle(float x, float y, float z, Vector3 v)
{
if (x <= -360)
x += 360;
if (x >= 360)
x -= 360;
if (y <= -360)
y += 360;
if (y >= 360)
y -= 360;
if (z <= -360)
z += 360;
if (z >= 360)
z -= 360;
v.Set(x, y, z);
return v;
}
//检测相机是否移动
public bool ismove()
{
// 检查当前帧的位置和旋转是否与上一帧不同
if (cameraTransform.position != lastPosition || cameraTransform.rotation != lastRotation)
{
// 相机移动了,可以在这里执行相应的操作
//Debug.Log("相机移动了"+ BoolMonitor.Value);
// 更新上一帧的位置和旋转
lastPosition = cameraTransform.position;
lastRotation = cameraTransform.rotation;
return false;
}
return true;
}
public void setisvalid(bool b)
{
isvalid = b;
}
void Update()
{
BoolMonitor.Value = ismove();
if (!isvalid)
{
return;
}
//initialRotationEulerAngles = transform.localEulerAngles;
if (Input.GetMouseButton(1))
{
if (HasMouseMoved())
{
// 累加旋转增量
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity;
rotationY = Mathf.Clamp(rotationY, -90, 90);
//Debug.Log(rotationX + "\n" + rotationX);
// 从initialRotationEulerAngles开始旋转,然后添加旋转增量。
Vector3 rotation = initialRotationEulerAngles + new Vector3(-rotationY, -rotationX, 0);
transform.localRotation = Quaternion.Euler(rotation);
//transform.localEulerAngles = rotation;
}
}
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
//GetComponent().enabled = true;
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (!EventSystem.current.IsPointerOverGameObject())
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//transform.position += transform.up * climbSpeed * Time.deltaTime*100;
if (GetComponent().fieldOfView >= 60)
{
GetComponent().fieldOfView = 60;
return;
}
GetComponent().fieldOfView += Time.deltaTime * 100;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
//transform.position -= transform.up * climbSpeed * Time.deltaTime*100;
if (GetComponent().fieldOfView <= 10)
{
GetComponent().fieldOfView = 10;
return;
}
GetComponent().fieldOfView -= Time.deltaTime * 100;
}
}
if (Input.GetKeyDown(KeyCode.End))
{
Cursor.visible = (Cursor.visible == true) ? false : true;
}
}
///
/// 重置相机角度初始增量
///
public void init_mainCamera_rot()
{
rotationX = 0;
rotationY = 0;
}
public void vvvv()
{
{
Inst.init_mainCamera_rot();
Inst.transform.DOLocalMove(new Vector3(4.7f, 20.2f, -18.3f), 1);
Inst.transform.DORotateQuaternion(Quaternion.Euler(Inst.ClampAngle(62.856f, -0.314f, 0.033f, Vector3.zero)), 1f).OnComplete(() =>
{
//更新相机初始旋转角度
Inst.initialRotationEulerAngles = new Vector3(62.856f, -0.314f, 0.033f);
});
}
}
public void OnDestroy()
{
// 停止并清除所有DOTween动画
transform.DOKill();
// 清除DOTween Tweener和Sequence缓存
DOTween.Clear(true);
}
float GetLimitedAngle(float angle)
{
// 将角度转换为在 0-360 范围内
angle %= 360f;
// 处理负数角度
if (angle < 0f)
{
angle += 360f;
}
return angle;
}
}