通常我们淡入一个模型只会传入这个模型的GameObject,所以这里自写一个类来处理这个模型淡入的一些事件。FadeModel 代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class FadeModel
{
private GameObject model;//传入的模型
private float fadeTime = 2f;//默认淡入时间为2s
private List materials = new List();
public FadeModel(GameObject model, float fadeTime = 2f)
{
this.model = model;
this.fadeTime = fadeTime;
MeshRenderer[] meshRenderers = model.GetComponentsInChildren();
foreach (MeshRenderer mr in meshRenderers)
{
Material[] materals = mr.materials;
foreach (Material m in materals)
{
if (!materials.Contains(m))
{
materials.Add(m);
}
}
}
}
//隐藏模型的淡隐效果
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 不然 经过一次显示他会一直显示为0
setMaterialRenderingMode(m, RenderingMode.Fade);
m.DOColor(new Color(color.r, color.g, color.b, 0), fadeTime).OnComplete(()=> {
if (i == materials.Count - 1)
model.SetActive(false);
});
}
}
//当我们隐藏完后还需要设置回来 不然他下次显示使用就是透明状态
public void ShowModel()
{
for (int i = 0; i < materials.Count; i++)
{
Material m = materials[i];
Color color = m.color;
setMaterialRenderingMode(m, RenderingMode.Opaque);
}
model.SetActive(true);
}
public enum RenderingMode
{
Opaque,
Cutout,
Fade,
Transparent
}
//设置材质的渲染模式
private void setMaterialRenderingMode(Material material, RenderingMode renderingMode)
{
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;
}
}
}
用的Dotween来做的淡出效果,然后我们就可以在我们需要的地方来调用淡出效果了
public class Test : MonoBehaviour {
public GameObject model;
FadeModel fadeModel;
// Use this for initialization
void Start ()
{
fadeModel = new FadeModel(model);
fadeModel.HideModel();
}
private void OnDisable()
{
fadeModel.ShowModel();
}
}
Tips:
在用人物做这个淡出效果测试时发现效果很不好。半透明效果有时候会让后面的辫子渲染到前面了。所以对于精细显示的效果我们还是去求助我们的shader程序员吧,让他写一个贴图消融的shader。但是对于工业级别的粗糙显示,我们这个方案还是完全够用的。
还有一个,我们在获取一个物体下的所有组件时,使用的代码是:
MeshRenderer[] meshRenderers = model.GetComponentsInChildren();
当时当我们model下的子物体setactive是false的时候是不能被找到的。如果我们也想找到这些物体,我们就需要在上面代码括号里加一个true。如下:
MeshRenderer[] meshRenderers = model.GetComponentsInChildren(true);