Unity里Shader的加载使用,AB包加载使用遇到一个坑

这里直接说坑吧:AB包中的shader没有加入到ProjectSetting==>Graphics==>Always Included Shaders列表中的,使用Assetbundle加载使用后会看不到效果,我也是这方面的菜鸟,实验了好久才得出结论。心塞啊,不知道是不是Unity的问题,更改了shader内容居然没起作用?难道是要我启动重启大法,将电脑重启一次???(我之前有些试验过程貌似都做过,没起效果,但是关机后放了个假再来试试就好了,我怀疑是不是编辑器没有刷新,下次再有结果来补上说明),不吐槽了,添加一下shader的加载代码编辑器模式:
//加载非Resources下的shader(注意Resource和Resources的区别)    protected override Shader DoLoadShaderAtPath(string path)    {        string shaderPath = string.Format("Assets/Resource/Shader/{0}.shader", path);        Shader shader = null;#if UNITY_EDITOR        //Shader.Find("Custom/MaskGray2")        shader = UnityEditor.AssetDatabase.LoadAssetAtPath<Shader>(shaderPath);#endif        return shader;    }//这是Resources目录下的shader加载    protected override Shader DoLoadShader(string path)    {//        string shaderPath = string.Format("Custom/{0}", path);//#if UNITY_EDITOR//        Shader shader = Shader.Find(shaderPath);//#endif//        return shader;        string shaderPath = string.Format("Shader/{0}.shader", path);#if UNITY_EDITOR        Shader shader = Resources.Load<Shader>(shaderPath);#endif        return shader;    }
AB加载模式:
//这是Resources目录下的shader加载    protected override Shader DoLoadShader(string path)    {        string bundleName = "shader.unity3d";        Shader shader = null;        LoadedAssetBundle lab = LoadAssetBundleSync(bundleName);        if (lab != null)        {            string assetName = string.Format("Assets/Resources/Shader/{0}.shader", path).ToLower();            shader = lab.m_AssetBundle.LoadAsset<Shader>(assetName);        }        return shader;    }//这是非Resources目录下的shader加载    protected override Shader DoLoadShaderAtPath(string path)    {        string bundleName = "shader.unity3d";        Shader shader = null;        LoadedAssetBundle lab = LoadAssetBundleSync(bundleName);        if (lab != null)        {            string assetName = string.Format("Assets/Resource/Shader/{0}.shader", path).ToLower();            shader = lab.m_AssetBundle.LoadAsset<Shader>(assetName);        }        return shader;    }

这里主要不讲代码,主要记录坑:AB加载的shader都要加入ProjectSetting==>Graphics==>Always Included Shaders列表中。

来源:https://www.icode9.com/content-4-817851.html

(0)

相关推荐