遮挡透明若没有渐变实现方法:
1、透明中物体存在list中
2、每过一段时间(可以每帧,但是流畅性会降低)摄像机发送一条射线向玩家,out hitInfo
3、list与hitInfo比对,将在list中但是没有在hitInfo中的物体转变成不透明,list.remove;将在hitInfo中但是没有在list中转变成透明,list.add
若有渐变,实现比较麻烦一点,物体有两个状态:转变成透明过程中、转变成不透明过程中,添加两个list(InTransparent,outTransparent)对应两种状态,那么维护两个list即可:
1、未遮挡->遮挡,查看outTransparent,如果有该物体,从outTransparent移入InTransparent,outTransparent remove;如果没有添加新的加入InTransparent
2、遮挡->未遮挡,查看InTransparent,如果有该物体,从InTransparent移入outTransparent,InTransparent remove;如果没有添加新的加入outTransparent
摄像机CameraFollow.cs:
1 public class CameraFollow : MonoBehaviour 2 { 3 4 public static CameraFollow instance; 5 public Transform Player; 6 7 public List<CameraMaskInfo> Mask; 8 public List<CameraMaskInfo> InTransparentList; 9 public List<CameraMaskInfo> OutTransparentList; 10 11 RaycastHit[] hitInfo; 12 13 void Awake() 14 { 15 instance = this; 16 } 17 void Start() 18 { 19 StartCoroutine(IEMask()); 20 } 21 public void ClearMask() 22 { 23 Mask.Clear(); 24 InTransparentList.Clear(); 25 OutTransparentList.Clear(); 26 } 27 28 IEnumerator IEMask() 29 { 30 while (true) 31 { 32 hitInfo = Physics.CapsuleCastAll(FollowTarget.position + Vector3.up * 0.2f, FollowTarget.position - Vector3.up * 0.1f, 0.2f, (transform.position - FollowTarget.position).normalized, 100f, layerMask); 33 bool flag; 34 for (int i = Mask.Count - 1; i >= 0; i--) 35 { 36 flag = false; 37 for (int j = 0; j < hitInfo.Length; j++) 38 { 39 if (Mask[i].GetObj() == hitInfo[j].collider.gameObject) 40 { 41 flag = true; 42 break; 43 } 44 } 45 if (!flag) 46 { 47 Mask[i].PutOutTransparent(); 48 Mask.RemoveAt(i); 49 } 50 } 51 52 for (int i = 0; i < hitInfo.Length; i++) 53 { 54 flag = false; 55 for (int j = 0; j < Mask.Count; j++) 56 { 57 if (hitInfo[i].collider.gameObject == Mask[j].GetObj()) 58 { 59 flag = true; 60 break; 61 } 62 } 63 if (!flag) 64 { 65 CameraMaskInfo maskInfo = new CameraMaskInfo(hitInfo[i].collider.gameObject); 66 maskInfo.PutInTransparent(); 67 Mask.Add(maskInfo); 68 } 69 } 70 } 71 yield return new WaitForSeconds(0.2f); 72 } 73 74 void Update() 75 { 76 for (int i = InTransparentList.Count - 1; i >= 0; i--) 77 { 78 InTransparentList[i].Sub(); 79 } 80 81 for (int i = OutTransparentList.Count - 1; i >= 0; i--) 82 { 83 OutTransparentList[i].Add(); 84 } 85 } 86 }
遮挡物体CameraMaskInfo.cs:
using UnityEngine; using System.Collections; public class CameraMaskInfo { private GameObject obj; private Shader[] preMaterialShader; public bool isDestroy; #region get public GameObject GetObj() { return obj; } public Shader[] GetpreMaterialShader() { return preMaterialShader; } #endregion public CameraMaskInfo(GameObject obj) { this.obj = obj; } public void PutInTransparent() { int id = -1; for (int i = 0; i < CameraFollow.instance.OutTransparentList.Count; i++) { if (CameraFollow.instance.OutTransparentList[i].GetObj() == obj) { id = i; //如果在out里面有,那么说明该物体的shader已经被改变,只能从之前的获得 preMaterialShader = CameraFollow.instance.OutTransparentList[i].GetpreMaterialShader(); CameraFollow.instance.InTransparentList.Add(CameraFollow.instance.OutTransparentList[i]); CameraFollow.instance.OutTransparentList.RemoveAt(i); break; } } if (id==-1) { preMaterialShader = new Shader[obj.renderer.materials.Length]; for (int i = 0; i < obj.renderer.materials.Length; i++) { preMaterialShader[i] = obj.renderer.materials[i].shader; obj.renderer.materials[i].shader = Shader.Find("Transparent/Diffuse"); } CameraFollow.instance.InTransparentList.Add(this); } } public void PutOutTransparent() { int id = -1; for (int i = 0; i < CameraFollow.instance.InTransparentList.Count; i++) { if (CameraFollow.instance.InTransparentList[i].GetObj() == obj) { id = i; CameraFollow.instance.OutTransparentList.Add(CameraFollow.instance.InTransparentList[i]); CameraFollow.instance.InTransparentList.RemoveAt(i); break; } } if (id == -1) { CameraFollow.instance.OutTransparentList.Add(this); } } public void Add() { for (int i = 0; i < obj.renderer.materials.Length; i++) { obj.renderer.materials[i].color = new Color(obj.renderer.materials[i].color.r, obj.renderer.materials[i].color.g, obj.renderer.materials[i].color.b, obj.renderer.materials[i].color.a + Time.deltaTime * 2f); } if (obj.renderer.materials[0].color.a >= 0.95f) { for (int i = 0; i < obj.renderer.materials.Length; i++) { obj.renderer.materials[i].color = new Color(obj.renderer.materials[i].color.r, obj.renderer.materials[i].color.g, obj.renderer.materials[i].color.b, 1f); } SetPreShader(); CameraFollow.instance.OutTransparentList.Remove(this); } } public void Sub() { for (int i = 0; i < obj.renderer.materials.Length; i++) { obj.renderer.materials[i].color = new Color(obj.renderer.materials[i].color.r, obj.renderer.materials[i].color.g, obj.renderer.materials[i].color.b, obj.renderer.materials[i].color.a - Time.deltaTime * 2f); } if (obj.renderer.materials[0].color.a <= 0.3f) { CameraFollow.instance.InTransparentList.Remove(this); } } private void SetPreShader() { for (int i = 0; i < obj.renderer.materials.Length; i++) { obj.renderer.materials[i].shader = preMaterialShader[i]; } } }
效果: