当前位置:   article > 正文

Unity运行状态下动态修改材质球透明度_unity 动态修改材质球

unity 动态修改材质球

1、动态修改材质球透明度

    private void ModelMaterialBlur(GameObject _goModel)
    {
        Transform[] _trans = _goModel.GetComponentsInChildren<Transform>();
        if (_trans != null && _trans.Length != 0)
        {
            for (int i = 0; i < _trans.Length; i++)
            {
                MeshRenderer _meshs = _trans[i].GetComponent<MeshRenderer>();
                if (_meshs != null)
                {
                    foreach (Material mat in _meshs.materials)
                    {
                        if (!mat.HasProperty("_Color")) continue;
                        mat.SetFloat("_Mode", 2);
                        Color color = mat.color;
                        color.a = 0.2f;
                        mat.SetColor("_Color", color);
                        mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                        mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                        mat.SetInt("_ZWrite", 0);
                        mat.DisableKeyword("_ALPHATEST_ON");
                        mat.EnableKeyword("_ALPHABLEND_ON");
                        mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                        mat.renderQueue = 3000;
                    }
                }
            }
        }
    }
  • 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

2、还原材质球透明度

private void UnModelMaterialBlur(GameObject _goModel)
{
    Transform[] _trans = _goModel.GetComponentsInChildren<Transform>();
    if (_trans != null && _trans.Length != 0)
    {
        for (int i = 0; i < _trans.Length; i++)
        {
            MeshRenderer _meshs = _trans[i].GetComponent<MeshRenderer>();
            if (_meshs != null)
            {
                foreach (Material mat in _meshs.materials)
                {
                    if (!mat.HasProperty("_Color")) continue;
                    mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    mat.SetInt("_ZWrite", 1);
                    mat.DisableKeyword("_ALPHATEST_ON");
                    mat.DisableKeyword("_ALPHABLEND_ON");
                    mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    mat.renderQueue = -1;
                }
            }
        }
    }
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/126950
推荐阅读
相关标签
  

闽ICP备14008679号