其他分享
首页 > 其他分享> > Unity3D中模型的渐隐渐现效果实现

Unity3D中模型的渐隐渐现效果实现

作者:互联网

一、前言
最近项目上要实现一个模型渐隐渐现的效果,研究了一下,分享出来。

这个主要是用到了Shader(但不用学)和DoTween动画插件。

using UnityEngine;
using System.Collections;
public class TestClass : MonoBehaviour {
private float AlphaValue = 1.0f; private float time = 0.0f; private bool state = false;
void Update() { time += Time.deltaTime; if(time > 0.2f){ state = true; time = 0; } if(state) { AlphaValue -= 0.1f; } GameObject.Find(“YourGameObject”).renderer.material.color = new Color(0.5f, 0.3f, 1, AlphaValue); print(AlphaValue); } }

  

二、效果展示及源码下载

源码下载:

https://download.csdn.net/download/q764424567/20079897

三、正文
界面没啥好说的:

代码:

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);
        }
    }
}

 

四、后言
这个消融的shader看起来还是有点粗糙的,如果想要实现精细的shader效果,可以找shader程序员,不过对于虚拟仿真工业级别的显示还是可以用的。

标签:Unity3D,渐隐,color,void,渐现,private,UnityEngine,materials,using
来源: https://www.cnblogs.com/guangzhiruijie/p/16692599.html