当前位置: 首页 > article >正文

Unity控制物体透明度的改变

目录标题

  • 效果图
  • 代码
  • 调用
  • 注意事项

效果图

在这里插入图片描述
请添加图片描述

代码

注意:在控制全部的模型进行透视时,已经隐藏的子物体仍然要处理。

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<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  不然 经过一次显示他会一直显示为0
            setMaterialRenderingMode(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就可以。
请添加图片描述

这里是井队,天高任鸟飞,海阔凭鱼跃,点个关注不迷路,我们下期再见。


http://www.kler.cn/a/373190.html

相关文章:

  • Unreal Engine 5 C++(C#)开发:使用蓝图库实现插件(一)认识和了解Build.cs
  • 排序算法汇总
  • Jmeter实际应用
  • mysql 5.7实现组内排序(连续xx天数)
  • EM@组合数学@朱世杰恒等式@曲棍球恒等式Hockey-stick identity
  • 前端面试题(实习版)
  • Matplotlib 网格线
  • PostgreSQL 删除角色
  • 面向对象高级-static
  • 为什么选择 Spring data hadoop
  • 蓝牙BLE开发——红米手机无法搜索蓝牙设备?
  • 编程小白如何成为大神?大学新生的最佳入门攻略
  • QT 12.自定义信号、信号emit、信号参数注册_ev
  • 【Python · Pytorch】人工神经网络 ANN(中)
  • Agile敏捷方法
  • 内存马浅析
  • 关于深度学习方向学习的一些建议
  • 计算机低能儿从0刷leetcode | 33.搜索旋转排列数组
  • 10.30Python_异常文件操作json正则
  • 12. MapReduce全局计数器
  • Vue3实现地球上加载柱体
  • 如何将 Excel 数据转换为 SQL 脚本:从入门到实战
  • C# 将批量图片转为PDF文件
  • ts:模块导入、导出的简单使用(export、import)
  • 【Vue3】第二篇
  • 2024年“炫转青春”山东省飞盘联赛盛大开赛——临沭县青少年飞盘运动迅速升温