首页 > 代码库 > unity给子物体添加Shader

unity给子物体添加Shader

分享两个自制Shader:http://pan.baidu.com/s/1nuRcF2L

 

Shader存放路径:\Assets\Resources\Shader\

定义Shader类型:

public enum EnumFbxType
{
    Default,
    TouMing,
    GaoLiang,
}

加载指定Shader:

    private static Shader myTouMing = Shader.Find("My/TouMing");
    private static Shader myDiffuse = Shader.Find("My/Diffuse");

设置物体的Shader:

/// <summary>
    /// 设置模型材质
    /// </summary>
    /// <param name="_obj">模型</param>
    /// <param name="_fbxType">材质类型</param>
    public static void SetObjFbxShader(GameObject _obj, EnumFbxType _fbxType)
    {
        if (_obj == null) return;
        Renderer r = _obj.GetComponent<Renderer>();
        if (r == null) return;
        Material[] mater = r.materials;
        switch (_fbxType)
        {
            case EnumFbxType.Default:
                foreach (Material m in mater)
                {
                    if (m.color.a == 1 || m.color.a == 0.123f)
                    {
                        m.shader = myDiffuse;
                        m.SetFloat("_Glossiness", 0.3f);
                        m.SetFloat("_Metallic", 0);
                        m.SetColor("_Emission", Color.black);
                    }
                    else
                    {
                        m.shader = myTouMing;
                        m.SetColor("_Emission", Color.black);
                    }
                }
                break;
            case EnumFbxType.TouMing:
                foreach (Material m in mater)
                {
                    m.shader = myTouMing;
                    m.SetColor("_Emission", Color.black);
                    if (m.color.a == 1)
                    {
                        Color color = m.color;
                        color.a = 0.123f;
                        m.color = color;
                    }
                }
                break;
            case EnumFbxType.GaoLiang:
                foreach (Material m in mater)
                {
                    m.shader = myDiffuse;
                    m.SetFloat("_Glossiness", 1);
                    m.SetFloat("_Metallic", 0.2f);
                    m.SetColor("_Emission", Color.red);
                }
                break;
        }
    }

 

unity给子物体添加Shader