using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 注意:词典 AllHide那部分还可以优化,恢复材质后清空
/// </summary>
public class TransparentManager : MonoBehaviour
{
RaycastHit hitInfo;
Material transparent;//隐身材质 里面的shader是隐身的
Dictionary<string, Material[]> AllMat = new Dictionary<string, Material[]>();//所有材质
Dictionary<GameObject, bool> AllHide = new Dictionary<GameObject, bool>();
Transform playerTrans;//
// bool isGround;//
List<GameObject> list1 = new List<GameObject>();
float timer = 0;
float timeInterval = 0.3f;
Material Transparent;//透明材质
void Start()
{
Transparent = Resources.Load<Material>("Material/" + "Transparent1");
}
void FixedUpdate()
{
if (RPlayer.I == null)//赋值角色
return;
else
{
if (!playerTrans)
playerTrans = RPlayer.I.trans;
}
if (timer < Time.time) //时间判断
{
timer += timeInterval;
}
else
return;
if (playerTrans == null)//表示还没赋值
{
return;
}
#region 参照代码
/* if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if(Physics.Raycast(ray,out hitInfo))
{
Debug.DrawLine(ray.origin, hitInfo.point);//划出射线,只有在scene视图中才能看到
GameObject gameObj = hitInfo.collider.gameObject;
xuqi.Log("click object name is " + gameObj.name);
if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
{
xuqi.Log("pick up!");
}
}
}*/
#endregion
Vector3 dir = Camera.main.transform.position - playerTrans.position;
list1.AddRange(AllHide.Keys);
foreach (var k in list1)//全部变成false
{
//bool b;
//AllHide.TryGetValue(k, out b);
//b = false;
AllHide[k] = false;
}
if (Physics.Raycast(playerTrans.position, dir, out hitInfo, 200.0f, 1 << LayerMask.NameToLayer("Wall")))//
{
RaycastHit[] rts = Physics.RaycastAll(playerTrans.position, dir, 200.0f, 1 << LayerMask.NameToLayer("Wall"));
int ALL = rts.Length;
for (int i = 0; i < ALL; i++)
{
/ xuqi.Log("hitInfo.collider.name:" + " " + i + " " + rts[i].collider.name);
GameObject go = rts[i].collider.gameObject;//获取该物体
// xuqi.Log("扫到 go:" + go.name);
if (go.GetComponent<MeshRenderer>() == null && go.GetComponent<SkinnedMeshRenderer>() == null)
{// xuqi.Log("没找到" + go.name);
continue;
}
else
{
// xuqi.Log("找到mesh:"+go.name);
bool b;
// AllHide.TryGetValue(go, out b);
if (!AllHide.ContainsKey(go))
{
// xuqi.Log("添加 词典 表示需要隐藏的物品");
AllHide.Add(go, true);//添加所有的
}
else
{
// xuqi.Log("词典修改值为true");
// AllHide.TryGetValue(go, out b);//此路不通
// b = true;
AllHide[go] = true;
}
}
Material[] mts;//nowmts多余 shit
if (go.GetComponent<MeshRenderer>() != null)
{
mts = go.GetComponent<MeshRenderer>().materials;
}
else mts = go.GetComponent<SkinnedMeshRenderer>().materials;
Material[] nowmts = new Material[mts.Length];
// if(mts.le)
if (!AllMat.ContainsKey(go.name))
AllMat.Add(go.name, mts);//加入材质词典
// var nowmts = go.GetComponent<MeshRenderer>().materials;
for (int j = 0; j < mts.Length; j++)
{
Texture temp = mts[j].mainTexture;//主材质
if (temp == null) continue;
transparent = new Material(Transparent);//创建新材质
transparent.mainTexture = temp;//更换材质
nowmts[j] = transparent;//更换材质求
// xuqi.Log("j:" + j + " 更换材质球 :" + transparent.name + "贴图名称:" + temp.name);
}
// go.GetComponent<Renderer>().materials = nowmts;
if (go.GetComponent<MeshRenderer>() != null)
{
go.GetComponent<MeshRenderer>().materials = nowmts;
}
else go.GetComponent<SkinnedMeshRenderer>().materials = nowmts;
}
#region 废除 射线怎么画来着
// 重点在 out RaycastHit hitInfo
// hehe();
// xuqi.Log("hitInfo.collider.name:" + hitInfo.collider.name);//
// 在场景视图中绘制射线
//Debug.DrawLine(hitInfo., hitInfo.point, Color.red);
#endregion
// isGround = true;
}
else
{
// isGround = false;
}
RecoverMt();//
}
void RecoverMt()
{
//xuqi.Log("恢复所有替换shader的");
//没有任何视线阻挡我们才切换回来材质
int all = AllHide.Count;//所有的隐藏的需要回复回来的
// xuqi.Log("需要恢复个数:" + all);
if (all == 0) return;//表示没有需要回复的
foreach (var a in AllHide)
{
// xuqi.Log(" 现在AllHide辞典中 key:" + a.Key + " value: " + a.Value);
if (a.Value == false)//我们要恢复false并且删除该词典
{
Material[] mts;
GameObject go = a.Key;
// xuqi.Log("恢复的对象名称为:" + goTemp.name);
AllMat.TryGetValue(go.name, out mts);//查找出来材质
if (mts.Length == 0)
{
xuqi.Log("一个材质都没有????");
}
// goTemp.GetComponent<Renderer>().materials = mts;//回到最初 //去掉Mesh
if (go.GetComponent<MeshRenderer>() != null)
{
// xuqi.Log("确定替换回去1");
go.GetComponent<MeshRenderer>().materials = mts;
}
else
{
go.GetComponent<SkinnedMeshRenderer>().materials = mts;
// xuqi.Log("mtsmts mtsmts:"+ mts[0].name);
// xuqi.Log("确定替换回去2");
}
}
//else
// continue;
// AllHide.Remove(a.Key);//删除了该词典
}
}
void hehe()
{
//RaycastHit[] rts = Physics.RaycastAll(this.transform.position, dir, 200.0f, 1 << LayerMask.NameToLayer("Wall");
}
}
阻挡角色的墙面替换为透明shader的材质替换操作
最新推荐文章于 2023-01-30 13:15:30 发布