using DG.Tweening; using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; /// /// 柜子点击脚本 /// public class ClickEvent : MonoBehaviour { /// /// 物体是否透明 /// public bool isTransparentGlow = false; /// /// 放大状态 /// public bool My_magnifyState = false; private List meshRenderers; /// /// 自身的 /// private List materials; /// /// 复制出来的 /// public List empty = new List(); public Shader TransparentGlow_Shader; /// /// 坐标偏移值 /// //[SerializeField] Vector3 yiDong_pos = new Vector3(1.5f, 0.5f, 0); [SerializeField] Vector3 yiDong_pos = new Vector3(2.1f, 0.2f, 0); /// /// 角度偏移值 /// [SerializeField] Vector3 fangDa_rot = new Vector3(0, -90, 0); public List MeshRenderers { get { if (meshRenderers == null) meshRenderers = GetComponentsInChildren().ToList(); return meshRenderers; } } public List Materials { get { if (materials == null) { materials = new List(); MeshRenderers.ForEach(render => { Array.ForEach(render.materials, (_mat) => { if (!materials.Contains(_mat)) { materials.Add(_mat); Material _emp = new Material(_mat); empty.Add(_emp); } }); }); } return materials; } } // Start is called before the first frame update void Start() { TransparentGlow_Shader = Resources.Load("TransparentGlow"); } // Update is called once per frame void Update() { if (!EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0) && TransparentGlowManage.Inst.gameObject.activeSelf) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Menu.M_数字孪生_场景管理 == CabinetUIManager.Instance.current_menu && GameManager.Inst.magnifyState) { if (Physics.Raycast(ray, out hit, 100, 1 << 8)) { if (hit.collider.gameObject == gameObject) { GameManager.Inst.nowDevice = null; GameManager.Inst.nowDeviceID = null; Debug.Log("点击到配置" + gameObject.name); PatternChoose.Inst.transform.Find("设备类/设备配置").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("设备类/设备新增").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("端口类/端口配置").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("端口类/端口新增").gameObject.SetActive(false); GameManager.Inst.search_box.SetActive(false); var d = gameObject.GetComponent(); DragController drag = GameManager.Inst.gameObject.GetComponent().dragController.GetComponent(); if (d) { if (string.IsNullOrEmpty(d.deviceList.id)) { GameManager.Inst.lastDevice = gameObject; GameManager.Inst.lastDeviceID = null; GameManager.Inst.nowDevice = gameObject; PatternChoose.Inst.transform.Find("设备类/设备新增").gameObject.SetActive(true);//√ } else { GameManager.Inst.lastDevice = gameObject; GameManager.Inst.lastDeviceID = d.deviceList.id; GameManager.Inst.nowDeviceID = d.deviceList.id; GameManager.Inst.nowDevice = gameObject; if (drag) { drag.uPosManger = GameManager.Inst.FindParent(gameObject, GameManager.Inst.IsFindParent_UPosManger).GetComponent(); GameManager.Inst.gameObject.GetComponent().uposManger = drag.uPosManger; //drag.oriObjectPrefab = gameObject.GetComponent(); gameObject.GetComponent().OnselfClick(); } //PatternChoose.Inst.transform.Find("设备类/设备配置").gameObject.SetActive(true);//√ } } } } } else if (Menu.M_数字孪生_线缆连接_配置 == CabinetUIManager.Instance.current_menu && GameManager.Inst.magnifyState) { if (Physics.Raycast(ray, out hit, 100, 1 << 11)) { if (hit.collider.gameObject == gameObject) { GameManager.Inst.nowDevice = null; GameManager.Inst.nowDeviceID = null; Debug.Log("点击到配置" + gameObject.name); PatternChoose.Inst.transform.Find("设备类/设备配置").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("设备类/设备新增").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("端口类/端口配置").gameObject.SetActive(false); PatternChoose.Inst.transform.Find("端口类/端口新增").gameObject.SetActive(false); var p = gameObject.GetComponent(); if (p) { MeshRenderer renderer = p.hight.GetComponent(); Material[] materials = renderer.materials; Material[] newMaterials = materials.Clone() as Material[]; if (string.IsNullOrEmpty(p.portList.id)) { if (GameManager.Inst.lastDevice) { if (GameManager.Inst.lastDevice != gameObject && GameManager.Inst.lastDevice.GetComponent()) { MeshRenderer renderer1 = GameManager.Inst.lastDevice.GetComponent().hight.GetComponent(); Material[] materials1 = renderer1.materials; Material[] newMaterials1 = materials1.Clone() as Material[]; newMaterials1[0] = Resources.Load("Materials/4Tou"); renderer1.materials = newMaterials; } //GameManager.Inst.lastDevice.GetComponent().hight.GetComponent().materials[0] = Resources.Load("Materials/4Tou"); } GameManager.Inst.lastDevice = gameObject; GameManager.Inst.lastDeviceID = null; GameManager.Inst.nowDevice = gameObject; newMaterials[0] = Resources.Load("Materials/1Tou"); renderer.materials = newMaterials; //p.hight.GetComponent().materials[0] = Resources.Load("Materials/1Tou"); PatternChoose.Inst.transform.Find("端口类/端口新增").gameObject.SetActive(true);//√ } else { if (GameManager.Inst.lastDevice) { if (GameManager.Inst.lastDevice != gameObject && GameManager.Inst.lastDevice.GetComponent()) { MeshRenderer renderer1 = GameManager.Inst.lastDevice.GetComponent().hight.GetComponent(); Material[] materials1 = renderer1.materials; Material[] newMaterials1 = materials1.Clone() as Material[]; newMaterials1[0] = Resources.Load("Materials/4Tou"); renderer1.materials = newMaterials; } //GameManager.Inst.lastDevice.GetComponent().hight.GetComponent().materials[0] = Resources.Load("Materials/4Tou"); } GameManager.Inst.lastDevice = gameObject; GameManager.Inst.lastDeviceID = p.portList.id; GameManager.Inst.nowDeviceID = p.portList.id; GameManager.Inst.nowDevice = gameObject; newMaterials[0] = Resources.Load("Materials/1Tou"); renderer.materials = newMaterials; //p.hight.GetComponent().materials[0] = Resources.Load("Materials/1Tou"); PatternChoose.Inst.transform.Find("端口类/端口配置").gameObject.SetActive(true);//√ } } } } } #region MyRegion ////点击模型(配置) //if (Physics.Raycast(ray, out hit, 100, 1 << 8)) //{ // if (hit.collider.gameObject == gameObject) // { // if (Menu.M_数字孪生_场景管理 == CabinetUIManager.Instance.current_menu) // { // GameManager.Inst.search_box.SetActive(false); // var d = gameObject.GetComponent(); // if (d) // { // if (string.IsNullOrEmpty(d.deviceList.id)) // { // GameManager.Inst.nowDevice = gameObject; // PatternChoose.Inst.transform.Find("设备类/设备新增").gameObject.SetActive(true);//√ // } // else // { // GameManager.Inst.nowDeviceID = d.deviceList.id; // GameManager.Inst.nowDevice = gameObject; // PatternChoose.Inst.transform.Find("设备类/设备配置").gameObject.SetActive(true);//√ // } // } // } // else if (Menu.M_数字孪生_线缆连接_配置 == CabinetUIManager.Instance.current_menu) // { // var p = gameObject.GetComponent(); // if (p) // { // if (string.IsNullOrEmpty(p.portList.id)) // { // GameManager.Inst.nowDevice = gameObject; // PatternChoose.Inst.transform.Find("端口类/端口新增").gameObject.SetActive(true);//√ // } // else // { // GameManager.Inst.nowDeviceID = p.portList.id; // GameManager.Inst.nowDevice = gameObject; // PatternChoose.Inst.transform.Find("端口类/端口配置").gameObject.SetActive(true);//√ // } // } // } // else // { // PatternChoose.Inst.transform.Find("设备类/设备配置").gameObject.SetActive(false); // PatternChoose.Inst.transform.Find("端口类/端口配置").gameObject.SetActive(false); // } // } //} #endregion //不虚化 else if (Physics.Raycast(ray, out hit, 100, 1 << 6)) { //放大 if (hit.collider.gameObject == gameObject && !My_magnifyState) { GameManager.Inst.nowDeviceID = hit.collider.gameObject.GetComponent().deviceList.id; Change_hide(gameObject, true, true); } //缩小 else if (hit.collider.gameObject == gameObject && My_magnifyState) { Change_hide(gameObject, false, false); } } #region 虚化非点击 ////虚化非点击 //else if (Physics.Raycast(ray, out hit, 100, 1 << 6)) //{ // //点到,本身透明,场景透明模式 // if (hit.collider.gameObject == gameObject && isTransparentGlow && TransparentGlowManage.Inst.is_transparency) // { // ChangeBig_TouMing(); // } // //点到,本身非透明,场景透明模式 // else if (hit.collider.gameObject == gameObject && !isTransparentGlow && TransparentGlowManage.Inst.is_transparency) // { // ChangeSmall_renewALL(); // } // //点到,本身非透明,场景非透明模式 // else if (hit.collider.gameObject == gameObject && !isTransparentGlow && !TransparentGlowManage.Inst.is_transparency) // { // ChangeBig_(); // } //} #endregion } } private void OnMouseEnter() { if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_机柜容量) { DeviceQuery d = null; try { d = GetComponent(); } catch (Exception) { return; } if (d) { if (d.deviceList.type == "1") { VolumeImg v = null; try { v = transform.Find("容量/Canvas").GetComponent(); } catch { return; } if (!v) return; v.Image.gameObject.SetActive(true); v.textMeshProUGUI.text = "剩余" + d.deviceList.residueNum + "个U位置"; } else return; } else return; } else if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_智能巡检) { var d = GetComponent(); if (d.deviceList.type == "1") { if (EventSystem.current.IsPointerOverGameObject()) return; var digital = transform.Find("智能巡检").GetComponent(); digital.ShowUI(); } } } private void OnMouseExit() { if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_机柜容量) { DeviceQuery d = null; try { d = GetComponent(); } catch (Exception) { return; } if (d) { if (d.deviceList.type == "1") { VolumeImg v = null; try { v = transform.Find("容量/Canvas").GetComponent(); } catch { return; } if (!v) return; v.Image.gameObject.SetActive(false); } else return; } else return; } else if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_智能巡检) { var d = GetComponent(); if (d.deviceList.type == "1") { var digital = transform.Find("智能巡检").GetComponent(); digital.HideUI(); } } } /// /// 本身透明 /// [ContextMenu("透明")] public void F1() { isTransparentGlow = true; for (int i = 0; i < Materials.Count; i++) { Materials[i].shader = TransparentGlow_Shader; } } /// /// 本身恢复原材质 /// [ContextMenu("恢复原材质")] public void F2() { isTransparentGlow = false; for (int i = 0; i < Materials.Count; i++) { Materials[i].shader = empty[i].shader; } } /// /// 变大=>场景透明,自身不变 /// public void ChangeBig_TouMing() { ExtendedFlycam.Inst.init_mainCamera_rot(); isTransparentGlow = true; // 物体被点击 Debug.Log("放大点击到" + gameObject.name); //Renderer renderer = GetComponent(); Renderer renderer = null; //if (transform.childCount != 0) // renderer = transform.Find(transform.name).GetComponent(); //else // renderer = transform.GetComponent(); try { renderer = transform.Find(transform.name).GetComponent(); } catch (Exception) { if (!renderer) renderer = transform.GetComponentInChildren(); } Vector3 center = renderer.bounds.center; Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z); Quaternion targetRotation = Quaternion.Euler(fangDa_rot); Camera.main.transform.DOMove(targetPosition, 1f); Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() => { //更新相机初始旋转角度 ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles; }); TransparentGlowManage.Inst.transparencyALL(); F2(); } /// /// 变小=>场景恢复 /// public void ChangeSmall_renewALL() { ExtendedFlycam.Inst.init_mainCamera_rot(); isTransparentGlow = false; // 物体被点击 Debug.Log("缩小点击到" + gameObject.name); Camera.main.transform.DOMove(TransparentGlowManage.Inst.MainCamera_pos, 1f); Camera.main.transform.DORotateQuaternion(Quaternion.Euler(TransparentGlowManage.Inst.MainCamera_rot), 1f).OnComplete(() => { //更新相机初始旋转角度 ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles; }); TransparentGlowManage.Inst.renewALL(); } public void ChangeBig_() { ExtendedFlycam.Inst.init_mainCamera_rot(); isTransparentGlow = true; // 物体被点击 Debug.Log("放大点击到" + gameObject.name); //Renderer renderer = GetComponent(); Renderer renderer = null; //if (transform.childCount != 0) // renderer = transform.Find(transform.name).GetComponent(); //else // renderer = transform.GetComponent(); try { renderer = transform.Find(transform.name).GetComponent(); } catch (Exception) { if (!renderer) renderer = transform.GetComponentInChildren(); } Vector3 center = renderer.bounds.center; Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z); Quaternion targetRotation = Quaternion.Euler(fangDa_rot); Camera.main.transform.DOMove(targetPosition, 1f); Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() => { //更新相机初始旋转角度 ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles; }); TransparentGlowManage.Inst.transparencyALL(); F2(); } /// /// 机柜隐藏+是否拉近镜头 /// /// /// public void Change_hide(GameObject cabine, bool isHide, bool isZoomin) { //放大 if (isZoomin) Zoomin(cabine); //缩小 else Zoomout(cabine); for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++) { GameManager.Inst.Cabinets_go[i].SetActive(!isHide); } if (isHide) gameObject.SetActive(true); } /// /// 放大镜头 /// public void Zoomin(GameObject cabine) { if (Menu.M_数字孪生_线缆连接_配置 == CabinetUIManager.Instance.current_menu) { Array.ForEach(cabine.GetComponentsInChildren(), (itme) => { itme.hight.GetComponent().materials[0] = Resources.Load("Materials/4Tou"); itme.hight.SetActive(true); }); } ExtendedFlycam.Inst.CameraRtDisable(); CameraMgr.Instance.StoreTarget(); //ExtendedFlycam.Inst.init_mainCamera_rot(); // 物体被点击 Debug.Log("放大点击到" + gameObject.name); if (GetComponent()) { GameManager.Inst.nowDevice = gameObject; GameManager.Inst.nowDeviceID = GetComponent().deviceList.id; } else return; Renderer renderer = null; try { renderer = transform.Find(transform.name).GetComponent(); } catch (Exception) { if (!renderer) renderer = transform.GetComponentInChildren(); } Array.ForEach(GameObject.FindObjectsOfType(), itme => { itme.My_magnifyState = false; }); My_magnifyState = true; GameManager.Inst.magnifyState = true; TransparentGlowManage.Inst.is_magnify = true; Vector3 center = renderer.bounds.center; Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z); Quaternion targetRotation = Quaternion.Euler(fangDa_rot); Camera.main.transform.DOMove(targetPosition, 1f); Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() => { //更新相机初始旋转角度 //ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles; CameraMgr.Instance.GotoView(cabine.transform, 2, targetRotation); ExtendedFlycam.Inst.CameraRtEnable(); }); if (cabine && cabine.transform.Find("门把手") && cabine.transform.Find("门把手").GetComponentInChildren()) { var door = cabine.transform.Find("门把手").GetComponentInChildren(); if (door.TextMeshProUGUI.text == "关门") return; GameManager.Inst.power_open(cabine, "全开"); door.TextMeshProUGUI.text = "关门"; door.isopen = !door.isopen; } } /// /// 缩小镜头 /// /// public void Zoomout(GameObject cabine) { Array.ForEach(cabine.GetComponentsInChildren(), (itme) => { itme.hight.SetActive(false); }); ExtendedFlycam.Inst.CameraRtDisable(); GameManager.Inst.nowDeviceID = null; My_magnifyState = false; GameManager.Inst.magnifyState = false; TransparentGlowManage.Inst.is_magnify = false; //ExtendedFlycam.Inst.init_mainCamera_rot(); // 物体被点击 Debug.Log("缩小点击到" + gameObject.name); Camera.main.transform.DOMove(TransparentGlowManage.Inst.MainCamera_pos, 1f); Camera.main.transform.DORotateQuaternion(Quaternion.Euler(TransparentGlowManage.Inst.MainCamera_rot), 1f).OnComplete(() => { //更新相机初始旋转角度 //ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles; CameraMgr.Instance.ResumeView(); ExtendedFlycam.Inst.CameraRtEnable(); }); if (cabine && cabine.transform.Find("门把手") && cabine.transform.Find("门把手").GetComponentInChildren()) { var door = cabine.transform.Find("门把手").GetComponentInChildren(); if (door.TextMeshProUGUI.text == "开门") return; GameManager.Inst.power_close(cabine, "全关"); door.TextMeshProUGUI.text = "开门"; door.isopen = !door.isopen; } } }