using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// FR:hunkxu
/// </summary>
public class MatHideDisplayCtrl : MonoBehaviour
{
public static MatHideDisplayCtrl instance;
public Material translucentMaterial_lan;
private Dictionary<Transform, List<MeshRenderer>> meshRenderersDic_ori = new Dictionary<Transform, List<MeshRenderer>>(); // 原始材质字典
private Dictionary<Transform, Queue<Material>> materialsDic_ori = new Dictionary<Transform, Queue<Material>>(); // Material字典
public List<Transform> objList = new List<Transform>();
private void Awake()
{
instance = this;
//一开始保存所有物体的材质
foreach(Transform tt in objList)
{
saveOriMaterial(tt);
}
}
public int i = 0;
private void Update()
{
//测试,鼠标右键点击,依次显示数组中的物体
if (Input.GetMouseButtonDown(1))
{
if (objList.Count > 0)
{
disPlay(objList[i]);
i++;
if (i == objList.Count)
{
i = 0;
}
}
}
}
//只显示数组中的某个物体,数组中其它物体半透
public void disPlay(Transform trans)
{
foreach (Transform tt in objList)
{
if (tt == trans)
{
ResetMaterial(tt);
}
else
{
Translucent(tt);
}
}
}
//显示数组中所有物体
public void disPlayAll()
{
foreach (Transform tt in objList)
{
ResetMaterial(tt);
}
}
// 恢复物体的原始材质
private void ResetMaterial(Transform trans)
{
if (meshRenderersDic_ori.ContainsKey(trans))
{
List<MeshRenderer> meshRenderers = meshRenderersDic_ori[trans];
Queue<Material> materials = materialsDic_ori[trans];
for (int i = 0; i < meshRenderers.Count; i++)
{
MeshRenderer mesh = meshRenderers[i];
int length = mesh.sharedMaterials.Length;
Material[] oldMaterials = new Material[length];
for (int j = 0; j < length; j++)
{
oldMaterials[j] = materials.Dequeue();
}
mesh.sharedMaterials = oldMaterials;
}
meshRenderersDic_ori.Remove(trans);
materialsDic_ori.Remove(trans);
//注意恢复完,重新保存一下
saveOriMaterial(trans);
}
}
// 设置为透明
private void Translucent(Transform trans)
{
List<Transform> childs = GetAllChilds(trans);
for (int i = 0; i < childs.Count; i++)
{
MeshRenderer mesh = childs[i].GetComponent<MeshRenderer>();
if (null != mesh)
{
int length = mesh.sharedMaterials.Length;
Material[] newMaterials = new Material[length];
for (int j = 0; j < length; j++)
{
newMaterials[j] = translucentMaterial_lan;
}
mesh.sharedMaterials = newMaterials;
}
}
}
// 保存物体的原始材质
private void saveOriMaterial(Transform trans)
{
if (!meshRenderersDic_ori.ContainsKey(trans))
{
List<MeshRenderer> meshRenderers = new List<MeshRenderer>();
Queue<Material> materials = new Queue<Material>();
List<Transform> childs = GetAllChilds(trans);
for (int i = 0; i < childs.Count; i++)
{
MeshRenderer mesh = childs[i].GetComponent<MeshRenderer>();
if (null != mesh)
{
meshRenderers.Add(mesh);
int length = mesh.sharedMaterials.Length;
Material[] oldMaterials = mesh.sharedMaterials;
for (int j = 0; j < length; j++)
{
materials.Enqueue(oldMaterials[j]);
}
}
}
meshRenderersDic_ori.Add(trans, meshRenderers);
materialsDic_ori.Add(trans, materials);
}
}
// 获取全部子物体列表
private List<Transform> GetAllChilds(Transform trans)
{
List<Transform> childs = new List<Transform>();
if (trans.childCount > 0)
{
for (int i = 0; i < trans.childCount; i++)
{
childs.AddRange(GetAllChilds(trans.GetChild(i)));
}
}
childs.Add(trans);
return childs;
}
}
FR:徐海涛(hunk Xu) QQ技术交流群:386476712
|