GQ_Communicate/GQ_URP/GQ/Assets/script/透明/TransparentGlowManage.cs

281 lines
8.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// 透明管理
/// </summary>
public class TransparentGlowManage : MonoBehaviour
{
static TransparentGlowManage _inst;
public static TransparentGlowManage Inst
{
get
{
if (_inst == null)
{
_inst = FindObjectOfType<TransparentGlowManage>();
if (_inst == null)
{
GameObject singletonObject = new GameObject();
_inst = singletonObject.AddComponent<TransparentGlowManage>();
singletonObject.name = typeof(TransparentGlowManage).ToString() + " (透明管理)";
}
}
return _inst;
}
}
public TransparentGlow[] transparentGlows;
/// <summary>
/// 可以透明机器
/// </summary>
public List<Transform> points = new List<Transform>();
public List<ClickEvent> clickEvents = new List<ClickEvent>();
/// <summary>
/// 可以透明的蓄电池
/// </summary>
public List<Transform> points_battery = new List<Transform>();
/// <summary>
/// 是否进入透明模式
/// </summary>
public bool is_transparency = false;
/// <summary>
/// 是否进入放大模式
/// </summary>
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()
{
}
/// <summary>
/// 添加组件脚本
/// </summary>
public void addScript()
{
transparentGlows = FindObjectsOfType<TransparentGlow>();
//Array.ForEach(FindObjectsOfType<TransparentGlow>(), tr =>
//{
//});
foreach (var item in points)
{
ClickEvent clickEvent = item.GetComponent<ClickEvent>();
//BoxCollider boxCollider = item.GetComponent<BoxCollider>();
DeviceItem deviceItem = item.GetComponent<DeviceItem>();
//CabinetInfor cabinetInfor = item.GetComponent<CabinetInfor>();
//Transform U = item.Find("U位");
if (item.gameObject.activeSelf)
{
if (!clickEvent)
{
//加点击脚本,处理模型名字,new 设备编号管理
item.gameObject.AddComponent<ClickEvent>();
}
//Renderer renderer = null;
//if (!boxCollider)
//{
// try
// {
// ////复杂设备
// //if (item.childCount != 0)
// // renderer = item.Find(item.name).GetComponent<Renderer>();
// ////简单设备
// //else
// // renderer = item.GetComponent<Renderer>();
// try
// {
// renderer = item.Find(item.name).GetComponent<Renderer>();
// }
// catch (Exception)
// {
// renderer = item.GetComponent<Renderer>();
// }
// if (renderer != null)
// {
// var initrot = item.rotation;
// item.rotation = Quaternion.identity;
// var bounds = renderer.bounds;
// var a = item.gameObject.AddComponent<BoxCollider>();
// 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<DeviceItem>();
}
}
clickEvents = FindObjectsOfType<ClickEvent>().ToList();
//for (int i = 0; i < clickEvents.Count; i++)
//{
// if (clickEvents[i].Materials.Count >= 0)
// {
// for (int j = 0; j < clickEvents[i].Materials.Count; j++)
// {
// break;
// }
// }
//}
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
{
var glow = GameManager.Inst.Cabinets_go[i].GetComponent<TransparentGlow>();
glow.meshRenderers = null;
glow.materials = null;
//for (int j = 0; j < glow.Materials.Count; j++)
// break;
}
}
/// <summary>
/// 获取正确的缩放比例
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 透明全部物体
/// </summary>
[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.Count; i++)
{
clickEvents[i].isTransparentGlow = true;
}
this.is_transparency = is_transparency;
}
/// <summary>
/// 恢复全部物体
/// </summary>
[ContextMenu("恢复全部物体")]
public void renewALL(bool is_transparency = false)
{
List<TransparentGlow> l = Array.FindAll(points_battery.ToArray(), (item) => { return (item.GetComponent<TransparentGlow>()); }).Select(x => x.GetComponent<TransparentGlow>()).ToList();
List<TransparentGlow> TransparentGlowS = transparentGlows.ToList();
TransparentGlowS.AddRange(l);
for (int i = 0; i < TransparentGlowS.Count; i++)
{
TransparentGlowS[i].F2();
}
for (int i = 0; i < clickEvents.Count; i++)
{
clickEvents[i].isTransparentGlow = false;
}
this.is_transparency = is_transparency;
}
/// <summary>
/// 透明全部物体
/// </summary>
/// <param name="transparentGlows"></param>
public void transparencyALL(TransparentGlow[] transparentGlows)
{
for (int i = 0; i < transparentGlows.Length; i++)
{
transparentGlows[i].F1();
}
for (int i = 0; i < clickEvents.Count; i++)
{
clickEvents[i].isTransparentGlow = true;
}
this.is_transparency = true;
}
/// <summary>
/// 恢复全部物体
/// </summary>
/// <param name="transparentGlows"></param>
public void renewALL(TransparentGlow[] transparentGlows)
{
var _transparentGlows = transparentGlows.ToList();
Array.ForEach(_transparentGlows.ToArray(), (item) =>
{
if (item == null)
_transparentGlows.Remove(item);
});
transparentGlows = _transparentGlows.ToArray();
for (int i = 0; i < transparentGlows.Length; i++)
{
transparentGlows[i].F2();
}
for (int i = 0; i < clickEvents.Count; i++)
{
clickEvents[i].isTransparentGlow = false;
}
this.is_transparency = false;
}
}