最近项目上要实现一个模型渐隐渐现的效果,研究了一下,分享出来。
这个主要是用到了Shader(但不用学)和DoTween动画插件。
文中示例代码简单明了,感兴趣的小伙伴可以参考一下。
界面没啥好说的:
代码:
登录后复制
using DG.Tweening;using System.Collections.Generic;using UnityEngine;
using UnityEngine.UI;public class TestDissolve : MonoBehaviour{ public GameObject model;
private List<Material> materials = new List<Material>(); public Button Btn_FadeOut;
public Button Btn_FadeInt; private void Start() { //获取材质 GetMeterials();
Btn_FadeOut.onClick.AddListener(FadeOut); Btn_FadeInt.onClick.AddListener(FadeIn);
} private void GetMeterials() { Material[] materals;
MeshRenderer[] meshRendererer = model.GetComponentsInChildren<MeshRenderer>();
foreach (var item in meshRendererer) { materals = item.materials;
foreach (Material m in materals) { if (!materials.Contains(m))
{ materials.Add(m); } } }
for (int i = 0; i < materials.Count; i++) { Material m = materials[i];
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0); m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON"); m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000; } } private void FadeOut() {
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, 0), 3f);
} } private void FadeIn() { 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), 3f); } }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.
15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.
49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.
效果:
这个消融的shader看起来还是有点粗糙的,如果想要实现精细的shader效果,可以找shader程序员,不过对于虚拟仿真工业级别的显示还是可以用的。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删