GQ_Communicate/GQ_TongXin/Assets/script/透明/TransparentGlowManage.cs

217 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
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 ClickEvent[] clickEvents;
/// <summary>
/// 是否进入透明模式
/// </summary>
public bool is_transparency = 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()
{
transparentGlows = FindObjectsOfType<TransparentGlow>();
Array.ForEach(FindObjectsOfType<TransparentGlow>(), tr =>
{
});
foreach (var item in points)
{
ClickEvent clickEvent = item.GetComponent<ClickEvent>();
BoxCollider boxCollider = item.GetComponent<BoxCollider>();
//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>();
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);
}
}
//if (!cabinetInfor)
//{
// item.gameObject.AddComponent<CabinetInfor>();
// cabinetInfor.cabinet.name = item.name;
//}
//else
//{
// cabinetInfor.cabinet.name = item.name;
//}
if (!U)
{
var go = GameObject.Instantiate(Resources.Load<Transform>("机柜/U位"));
go.name = "U位";
var bounds = renderer.bounds;
Vector3 bottomPosition = bounds.min;
go.position = bottomPosition + new Vector3(0, 0, 0.063f);
go.SetParent(item);
}
}
}
clickEvents = FindObjectsOfType<ClickEvent>();
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;
}
}
}
}
/// <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)
{
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.Length; i++)
{
clickEvents[i].isTransparentGlow = true;
}
this.is_transparency = is_transparency;
}
/// <summary>
/// 恢复全部物体
/// </summary>
[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;
}
}