using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using DG.Tweening; using static WebInteraction; using System; using System.Collections.Generic; /// /// 上帝视角 /// [AddComponentMenu("ExtendedFlycam/上帝视角")] public class ExtendedFlycam : MonoBehaviour { static ExtendedFlycam _inst; public CameraTypes cameraTypes = CameraTypes.全景; public Room room = Room.None; public List jiFang; public List peiDianShi; //屋顶 public GameObject jiFangD; public GameObject peiDianShiD; //地面(正常) public GameObject jiFang_ground; public GameObject peiDianShi_ground; //接地网或者线缆展示地面 public GameObject jiFang_JDW_ground; public GameObject peiDianShi_JDW_ground; //接地网需显示埋线 public List jiFang_JDW_Cable_ground_show; public List peiDianShi_JDW_Cable_ground_show; //接地网、线缆展示、智能巡检需隐藏的地面 public List Cable_ground_hide; public Image xunJian_img; public List sprites; public Image room_img1; public Image room_img2; 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("当前是否不动" + 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() { //Debug.Log("版本号 1.1.1"); Cursor.visible = true; //initialRotationEulerAngles = transform.localEulerAngles; // 获取相机的Transform组件 cameraTransform = GetComponent(); // 初始化上一帧的位置和旋转 lastPosition = cameraTransform.position; lastRotation = cameraTransform.rotation; //Debug.Log("版本号 1.1.1"); } private void OnEnable() { //Debug.Log("版本号 1.1.1"); transform.rotation = Quaternion.Euler(initialRotationEulerAngles); //Debug.Log("版本号 1.1.1"); } 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() { //if (mask_img.activeSelf) return; //else if (!GameManager.Inst.isLoading) return; BoolMonitor.Value = ismove(); if (!isvalid) { init_mainCamera_rot(); 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, -45, 45); //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 CameraRT CameraRT; public void CameraRtEnable() { CameraRT.enabled = true; //Debug.Log("RT_OP"); } public void CameraRtDisable() { CameraRT.enabled = false; //Debug.Log("RT_CL"); } /// /// 镜头全景 /// public void QuanJing() { if (PatternChoose.Inst.current_menu == CabinetUIManager.Instance.current_menu) { for (int i = 0; i < CabinetUIManager.Instance.cabine_hights_now.Count; i++) { CabinetUIManager.Instance.cabine_hights_now[i].SetActive(PatternChoose.Inst.current_menu == CabinetUIManager.Instance.current_menu); } } CabinetUIManager.Instance.cabine_hights_now.Clear(); if (GameManager.Inst.objectToShow_add.activeSelf) { var SN1 = GameManager.Inst.objectToShow_add.GetComponent(); SN1.hide_menu(); GameManager.Inst.objectToShow_add.SetActive(false); } CameraRtEnable(); CameraRT.InitPanoramic(); return; Array.ForEach(GameObject.FindObjectsOfType(), itme => { itme.My_magnifyState = false; }); //Array.ForEach(GameObject.FindObjectsOfType(), itme => //{ // itme.F2(); //}); PatternChoose.Inst.sb_bj_page.SetActive(false); PatternChoose.Inst.sb_xz_page.SetActive(false); PatternChoose.Inst.dk_bj_page.SetActive(false); PatternChoose.Inst.dk_xz_page.SetActive(false); GameManager.Inst.magnifyState = false; Inst.init_mainCamera_rot(); Inst.transform.DOLocalMove(new Vector3(4.73694f, 20.1847f, -4.738012f), 1); Inst.transform.DORotateQuaternion(Quaternion.Euler(Inst.ClampAngle(67.072f, -179.714f, -0.025f, Vector3.zero)), 1f).OnComplete(() => { //更新相机初始旋转角度 Inst.initialRotationEulerAngles = new Vector3(67.072f, -179.714f, -0.025f); }); } /// /// 镜头鸟瞰 /// public void NiaoKan() { CameraRtEnable(); CameraRT.InitBirds_Eye(); return; Array.ForEach(GameObject.FindObjectsOfType(), itme => { itme.My_magnifyState = false; }); PatternChoose.Inst.sb_bj_page.SetActive(false); PatternChoose.Inst.sb_xz_page.SetActive(false); PatternChoose.Inst.dk_bj_page.SetActive(false); PatternChoose.Inst.dk_xz_page.SetActive(false); GameManager.Inst.magnifyState = true; Inst.init_mainCamera_rot(); Inst.transform.DOLocalMove(new Vector3(18.35136f, 2.717596f, -10.86447f), 1); Inst.transform.DORotateQuaternion(Quaternion.Euler(Inst.ClampAngle(22.6f, -91.1f, 0, Vector3.zero)), 1f).OnComplete(() => { //更新相机初始旋转角度 Inst.initialRotationEulerAngles = new Vector3(22.6f, -91.1f, 0); }); } 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; } public enum CameraTypes { 全景, 鸟瞰 } public enum Room { None, 机房, 配电室 } public void JingTou(int index) { if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_智能巡检 || CabinetUIManager.Instance.current_menu == Menu.M_远程指导) { for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(false); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(false); } if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_智能巡检) xunJian_img.gameObject.SetActive(true); else { xunJian_img.gameObject.SetActive(false); } return; } else { xunJian_img.gameObject.SetActive(false); } GameObject createLine = PatternChoose.Inst.xianlan.gameObject; if (CabinetUIManager.Instance.jieDiWang.activeSelf) { if (!jiFang[1].activeInHierarchy) { CabinetUIManager.Instance.jieDiWang.SetActive(false); } else { jiFang[4].SetActive(false); } } else if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_接地网) { jiFang[4].SetActive(false); if (jiFang[1].activeInHierarchy) { CabinetUIManager.Instance.jieDiWang.SetActive(true); } } if (CabinetUIManager.Instance.current_menu != Menu.M_数字机房_线缆连接_展示 || room == Room.配电室) { for (int i = 0; i < createLine.transform.childCount; i++) { createLine.transform.GetChild(i).gameObject.SetActive(false); } } //for (int i = 0; i < createLine.transform.childCount; i++) //{ // createLine.transform.GetChild(i).gameObject.SetActive(false); //} bool isgo = true; reminder((x, list, prompt) => { isgo = x; if (!isgo || SecondConfirmPanel.Instance.transform.GetChild(0).gameObject.activeInHierarchy) return; //if (Vector3.Distance(Camera.main.transform.position, new Vector3(5.47000027f, 19.2399025f, -4.70852375f)) <= 1f) // return; GameManager.Inst.RemoveEmptyElements(); //PatternChoose.Inst.FindDeletedDevices(list); if (index == 0) { QuanJing(); } else if (index == 99) { return; } else { NiaoKan(); } }); } /// /// 切换模式 /// /// 选择状态 public void QieHuan(Menu menu, int state = 1) { if (menu == Menu.None) state = 1; CabinetUIManager.Instance.current_menu = menu; //菜单栏(不切换) if (state == 1) { if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_智能巡检) { room = Room.机房; } else if (CabinetUIManager.Instance.current_menu == Menu.M_全景监控_现场作业) room = Room.机房; //else if (CabinetUIManager.Instance.current_menu == Menu.M_全景监控_摄像头) // room = Room.机房; else { room = Room.None; } if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_接地网 || CabinetUIManager.Instance.current_menu == Menu.M_数字机房_线缆连接_展示) { jiFang_JDW_ground.SetActive(true); peiDianShi_JDW_ground.SetActive(true); } showRoom(); } //房间状态变(会切换) else if (state == 2) { if (Room.None == room) { cameraTypes = CameraTypes.全景; room = Room.机房; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(false); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(true); } jiFang_JDW_ground.SetActive(true); peiDianShi_JDW_ground.SetActive(false); } else if (Room.机房 == room) { cameraTypes = CameraTypes.全景; room = Room.配电室; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(true); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(false); } jiFang_JDW_ground.SetActive(false); peiDianShi_JDW_ground.SetActive(true); } else if (Room.配电室 == room) { cameraTypes = CameraTypes.全景; room = Room.None; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(true); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(true); } jiFang_JDW_ground.SetActive(true); peiDianShi_JDW_ground.SetActive(true); } jiFangD.SetActive(false); peiDianShiD.SetActive(false); //if (CabinetUIManager.Instance.current_menu != Menu.M_数字孪生_接地网) //{ // jiFang_JDW_ground.SetActive(false); // peiDianShi_JDW_ground.SetActive(false); //} if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_线缆连接_展示) { jiFang_ground.SetActive(false); peiDianShi_ground.SetActive(false); } JingTou(((int)cameraTypes)); } // 房间状态显示(无转换) else { showRoom(state); } if (room == Room.机房) { room_img1.sprite = sprites[1]; room_img2.sprite = sprites[1]; } else if (room == Room.None) { room_img1.sprite = sprites[0]; room_img2.sprite = sprites[0]; } else if (room == Room.配电室) { room_img1.sprite = sprites[2]; room_img2.sprite = sprites[2]; } ground_hide_show(); } /// /// 展示房间 /// void showRoom(int _none = 1) { if (Room.None == room) { cameraTypes = CameraTypes.全景; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(true); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(true); } jiFang_JDW_ground.SetActive(true); peiDianShi_JDW_ground.SetActive(true); } else if (Room.机房 == room) { cameraTypes = CameraTypes.全景; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(false); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(true); } jiFang_JDW_ground.SetActive(true); peiDianShi_JDW_ground.SetActive(false); } else if (Room.配电室 == room) { cameraTypes = CameraTypes.全景; for (int i = 0; i < peiDianShi.Count; i++) { peiDianShi[i].SetActive(true); } for (int i = 0; i < jiFang.Count; i++) { jiFang[i].SetActive(false); } jiFang_JDW_ground.SetActive(false); peiDianShi_JDW_ground.SetActive(true); } jiFangD.SetActive(false); peiDianShiD.SetActive(false); //if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_接地网|| CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_线缆连接_展示) //{ // jiFang_JDW_ground.SetActive(true); // peiDianShi_JDW_ground.SetActive(true); //} if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_线缆连接_展示) { jiFang_ground.SetActive(false); peiDianShi_ground.SetActive(false); } if (_none == 1) JingTou(((int)cameraTypes)); else { cameraTypes = CameraTypes.鸟瞰; JingTou(((int)cameraTypes)); } } /// /// 提示暂存箱 /// /// 是否成功,记录删除的设备,接口返回信息 /// public void reminder(Action, string> callback) { List recordDeletedDevices = new List();// 记录删除的设备 if (Menu.M_数字机房_场景管理 == CabinetUIManager.Instance.current_menu && CabinetUIManager.Instance.Stagin.childCount > 0) { SecondConfirmPanel.DeleteConform((delete) => { if (delete) { var d = PatternChoose.Inst.transform.Find("设备类").GetComponent(); d.mybody.ids.Clear(); for (int z = CabinetUIManager.Instance.Stagin.childCount - 1; z >= 0; z--) { var s = CabinetUIManager.Instance.Stagin.GetChild(z).GetComponent().deviceList.id; if (!string.IsNullOrEmpty(s)) { recordDeletedDevices.Add(CabinetUIManager.Instance.Stagin.GetChild(z).gameObject); d.mybody.ids.Add(s); } } StartCoroutine(d.SaveJsonCoroutine((x, prompt) => { if (x) { for (int z = CabinetUIManager.Instance.Stagin.childCount - 1; z >= 0; z--) { DestroyImmediate(CabinetUIManager.Instance.Stagin.GetChild(z).gameObject); } var content = GameManager.Inst.stagingPanel.transform.Find("Viewport/Content"); for (int i = content.childCount - 1; i >= 0; i--) { DestroyImmediate(content.GetChild(i).gameObject); } GameManager.Inst.stagingPanel.SetActive(false); callback.Invoke(true, recordDeletedDevices, prompt); } else { callback.Invoke(false, null, prompt); } })); } else { callback.Invoke(false, null, null); } }, "是否清空暂存箱设备(点击取消可对暂存箱子设备进行编辑)");// 请处理暂存箱内存存放的设备。是否删除暂存箱中所有设备(取消:可对暂存箱子设备进行拖拽编辑) } else { if (CabinetUIManager.Instance.Stagin.childCount == 0) { var content = GameManager.Inst.stagingPanel.transform.Find("Viewport/Content"); for (int i = content.childCount - 1; i >= 0; i--) { DestroyImmediate(content.GetChild(i).gameObject); } } GameManager.Inst.stagingPanel.SetActive(false); callback.Invoke(true, null, null); } } /// /// 接地网和线缆展示模式下地下显隐 /// public void ground_hide_show() { if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_线缆连接_展示 || CabinetUIManager.Instance.current_menu == Menu.M_数字机房_接地网 || CabinetUIManager.Instance.current_menu == Menu.M_数字机房_智能巡检) { for (int i = 0; i < Cable_ground_hide.Count; i++) { Cable_ground_hide[i].gameObject.SetActive(false); } } for (int i = 0; i < jiFang_JDW_Cable_ground_show.Count; i++) { jiFang_JDW_Cable_ground_show[i].SetActive(false); } for (int i = 0; i < peiDianShi_JDW_Cable_ground_show.Count; i++) { peiDianShi_JDW_Cable_ground_show[i].SetActive(false); } if (CabinetUIManager.Instance.current_menu == Menu.M_数字机房_接地网) { jiFang_JDW_Cable_ground_show[0].transform.parent.gameObject.SetActive(true); switch (room) { case Room.None: { for (int i = 0; i < jiFang_JDW_Cable_ground_show.Count; i++) { jiFang_JDW_Cable_ground_show[i].SetActive(true); } for (int i = 0; i < peiDianShi_JDW_Cable_ground_show.Count; i++) { peiDianShi_JDW_Cable_ground_show[i].SetActive(true); } } break; case Room.机房: { for (int i = 0; i < jiFang_JDW_Cable_ground_show.Count; i++) { jiFang_JDW_Cable_ground_show[i].SetActive(true); } for (int i = 0; i < peiDianShi_JDW_Cable_ground_show.Count; i++) { peiDianShi_JDW_Cable_ground_show[i].SetActive(false); } } break; case Room.配电室: { for (int i = 0; i < jiFang_JDW_Cable_ground_show.Count; i++) { jiFang_JDW_Cable_ground_show[i].SetActive(false); } for (int i = 0; i < peiDianShi_JDW_Cable_ground_show.Count; i++) { peiDianShi_JDW_Cable_ground_show[i].SetActive(true); } } break; default: break; } } } }