using System; using System.Collections.Generic; using System.Linq; using UnityEngine; /// /// 透明管理 /// public class TransparentGlowManage : MonoBehaviour { static TransparentGlowManage _inst; public static TransparentGlowManage Inst { get { if (_inst == null) { _inst = FindObjectOfType(); if (_inst == null) { GameObject singletonObject = new GameObject(); _inst = singletonObject.AddComponent(); singletonObject.name = typeof(TransparentGlowManage).ToString() + " (透明管理)"; } } return _inst; } } public TransparentGlow[] transparentGlows; /// /// 可以透明机器 /// public List points = new List(); public ClickEvent[] clickEvents; /// /// 是否进入透明模式 /// public bool is_transparency = false; /// /// 是否进入放大模式 /// public bool is_magnify = false; public Vector3 MainCamera_pos; public Vector3 MainCamera_rot; private void Awake() { if (_inst != null && _inst != this) { Destroy(this.gameObject); } else { _inst = this; DontDestroyOnLoad(this.gameObject); } } // Start is called before the first frame update void Start() { } /// /// 添加组件脚本 /// public void addScript() { transparentGlows = FindObjectsOfType(); //Array.ForEach(FindObjectsOfType(), tr => //{ //}); foreach (var item in points) { ClickEvent clickEvent = item.GetComponent(); BoxCollider boxCollider = item.GetComponent(); DeviceItem deviceItem= item.GetComponent(); //CabinetInfor cabinetInfor = item.GetComponent(); //Transform U = item.Find("U位"); if (item.gameObject.activeSelf) { if (!clickEvent) { //加点击脚本,处理模型名字,new 设备编号管理 item.gameObject.AddComponent(); } Renderer renderer = null; if (!boxCollider) { try { ////复杂设备 //if (item.childCount != 0) // renderer = item.Find(item.name).GetComponent(); ////简单设备 //else // renderer = item.GetComponent(); try { renderer = item.Find(item.name).GetComponent(); } catch (Exception) { renderer = item.GetComponent(); } if (renderer != null) { var initrot = item.rotation; item.rotation = Quaternion.identity; var bounds = renderer.bounds; var a = item.gameObject.AddComponent(); a.isTrigger = false; a.center = bounds.center - item.transform.position; //a.center = new Vector3( // a.center.x * AdjustColliderSize(10, 10, 10).x, // a.center.y * AdjustColliderSize(10, 10, 10).y, // a.center.z * AdjustColliderSize(10, 10, 10).z); a.size = bounds.size; //a.size = new Vector3( // a.size.x * AdjustColliderSize(10, 10, 10).x, // a.size.y * AdjustColliderSize(10, 10, 10).y, // a.size.z * AdjustColliderSize(10, 10, 10).z); //a.transform.rotation = item.rotation; //重置其旋转为默认值 item.rotation = initrot; } } catch (Exception e) { Debug.Log(item); continue; } } if (!deviceItem) item.gameObject.AddComponent(); } } clickEvents = FindObjectsOfType(); for (int i = 0; i < clickEvents.Length; i++) { if (clickEvents[i].Materials.Count >= 0) { for (int j = 0; j < clickEvents[i].Materials.Count; j++) { break; } } } } /// /// 获取正确的缩放比例 /// /// /// /// /// private Vector3 AdjustColliderSize(float x, float y, float z) { Vector3 scaleRatio = new Vector3(1f / x, 1f / y, 1f / z); return scaleRatio; } // Update is called once per frame void Update() { if (!is_transparency && !is_magnify) { MainCamera_pos = Camera.main.transform.localPosition; MainCamera_rot = Camera.main.transform.localEulerAngles; } } /// /// 透明全部物体 /// [ContextMenu("透明全部物体")] public void transparencyALL(bool is_transparency = true) { for (int i = 0; i < transparentGlows.Length; i++) { transparentGlows[i].F1(); } for (int i = 0; i < clickEvents.Length; i++) { clickEvents[i].isTransparentGlow = true; } this.is_transparency = is_transparency; } /// /// 恢复全部物体 /// [ContextMenu("恢复全部物体")] public void renewALL(bool is_transparency = false) { for (int i = 0; i < transparentGlows.Length; i++) { transparentGlows[i].F2(); } for (int i = 0; i < clickEvents.Length; i++) { clickEvents[i].isTransparentGlow = false; } this.is_transparency = is_transparency; } /// /// 透明全部物体 /// /// public void transparencyALL(TransparentGlow[] transparentGlows) { for (int i = 0; i < transparentGlows.Length; i++) { transparentGlows[i].F1(); } for (int i = 0; i < clickEvents.Length; i++) { clickEvents[i].isTransparentGlow = true; } this.is_transparency = true; } /// /// 恢复全部物体 /// /// public void renewALL(TransparentGlow[] transparentGlows) { for (int i = 0; i < transparentGlows.Length; i++) { transparentGlows[i].F2(); } for (int i = 0; i < clickEvents.Length; i++) { clickEvents[i].isTransparentGlow = false; } this.is_transparency = false; } }