目录标题
- 效果图
- 代码
- 调用
- 注意事项
效果图
代码
注意:在控制全部的模型进行透视时,已经隐藏的子物体仍然要处理。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class FadeModel {private GameObject model;//传入的模型private float fadeTime = 2f;//默认淡入时间为2sprivate List<Material> materials = new List<Material>();private float touMingDu = 0.02f;//透明度public FadeModel(GameObject model,float fadeTime=1f,float touMingDu=0.02f){this.model = model;this.fadeTime = fadeTime;this.touMingDu = touMingDu;materials.Clear();Dictionary<string, MeshRenderer> dic = FindInactiveChildrenRecursive(model.transform);foreach (KeyValuePair<string, MeshRenderer> item in dic){Material[] materals = item.Value.materials;foreach (Material m in materals){if (!materials.Contains(m)){materials.Add(m);}}}//MeshRenderer[] meshRenderers = model.GetComponentsInChildren<MeshRenderer>();//foreach (MeshRenderer mr in meshRenderers)//{// Material[] materals = mr.materials;// foreach (Material m in materals)// {// if (!materials.Contains(m))// {// materials.Add(m);// }// }// Debug.Log(mr.gameObject.name);//}}//这里新增修改,递归查询所有子物体,孙物体。Dictionary<string,MeshRenderer> FindInactiveChildrenRecursive(Transform parent){Dictionary<string, MeshRenderer> mrdic = new Dictionary<string, MeshRenderer>();MeshRenderer mrp = parent.GetComponent<MeshRenderer>();if (mrp != null){mrdic.Add(parent.name, mrp);}int childCount = parent.childCount;for (int i = 0; i < childCount; i++){Transform child = parent.GetChild(i);MeshRenderer mr = child.GetComponent<MeshRenderer>();if (mr !=null){mrdic.Add(child.name, mr);}if (child.childCount > 0){FindInactiveChildrenRecursive(child);}}return mrdic;}//隐藏模型的淡隐效果public void HideModel(){for(int i=0;i< materials.Count;i++){Material m = materials[i];Color color = m.color;m.color = new Color(color.r, color.g, color.b, 1);//这里一定要重新设置下Fade模式下的color a值 为1 不然 经过一次显示他会一直显示为0setMaterialRenderingMode(m,RenderingMode.Transparent);m.DOColor(new Color(color.r, color.g, color.b, touMingDu), fadeTime);}}//当我们隐藏完后还需要设置回来 不然他下次显示使用就是透明状态public void ShowModel(){for (int i = 0; i < materials.Count; i++){Material m = materials[i];Color color = m.color;m.DOColor(new Color(color.r, color.g, color.b, 1), fadeTime).OnComplete(()=> { setMaterialRenderingMode(m, RenderingMode.Opaque); });}}public enum RenderingMode{Opaque,Cutout,Fade,Transparent}//设置材质的渲染模式private void setMaterialRenderingMode(Material material, RenderingMode renderingMode){switch (renderingMode){case RenderingMode.Opaque:material.SetOverrideTag("RenderType", "");material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);material.SetInt("_ZWrite", 1);material.DisableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");material.renderQueue = -1;break;case RenderingMode.Cutout:material.SetOverrideTag("RenderType", "TransparentCutout");material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);material.SetInt("_ZWrite", 1);material.EnableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;break;case RenderingMode.Fade:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.EnableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;case RenderingMode.Transparent:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.EnableKeyword("_ALPHAPREMULTIPLY_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;}//switch (renderingMode)//{// case RenderingMode.Opaque:// material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);// material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);// material.SetInt("_ZWrite", 1);// material.DisableKeyword("_ALPHATEST_ON");// material.DisableKeyword("_ALPHABLEND_ON");// material.DisableKeyword("_ALPHAPREMULTIPLY_ON");// material.renderQueue = -1;// break;// case RenderingMode.Cutout:// material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);// material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);// material.SetInt("_ZWrite", 1);// material.EnableKeyword("_ALPHATEST_ON");// material.DisableKeyword("_ALPHABLEND_ON");// material.DisableKeyword("_ALPHAPREMULTIPLY_ON");// material.renderQueue = 2450;// break;// case RenderingMode.Fade:// material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);// material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);// material.SetInt("_ZWrite", 0);// material.DisableKeyword("_ALPHATEST_ON");// material.EnableKeyword("_ALPHABLEND_ON");// material.DisableKeyword("_ALPHAPREMULTIPLY_ON");// material.renderQueue = 3000;// //material.SetFloat("" _Mode & quot;", 2); // break;// case RenderingMode.Transparent:// material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);// material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);// material.SetInt("_ZWrite", 0);// material.DisableKeyword("_ALPHATEST_ON");// material.DisableKeyword("_ALPHABLEND_ON");// material.EnableKeyword("_ALPHAPREMULTIPLY_ON");// material.renderQueue = 3000;// break;//}}}
调用
foreach (Transform item in DiZhiKuai.transform)
{//需要传入变化的时间,以及透明度FadeModel fm = new FadeModel(item.gameObject, 0.5f, toumingdu);if (DiZhiTouShi.isOn){fm.ShowModel();}else{fm.HideModel();}
}
注意事项
经过测试,在电脑Pc端,手机apk完全没问题,但是一旦发布webgl网页端后,效果就失效了,当时也是查了好多办法,各种改属性参数就行不行,还好最后找到办法,那就是提前将模型的材质球的Rendering Mode先改成Transparent就可以。
这里是井队,天高任鸟飞,海阔凭鱼跃,点个关注不迷路,我们下期再见。