Unity Shader.Find 和 AB
2024/05
31
16:05
将Shader放置于AB中,然后在设备上,使用Shader.Find去找,会出现找不到的情况
方案一:
将Shader文件放置于Resources目录中
方案二:
将Shader文件放置在Graphics Settings的Always Include中
方案三:
以上2个方案都对统一打包方案不友好,故可以做一个转换
public static class ShaderHelper
{
public delegate Shader FindShaderFunc(string shaderName);
private static FindShaderFunc mFindFunction = null;
public static void SetFindShaderFunc(FindShaderFunc f)
{
mFindFunction = f;
}
public static Shader Find(string shaderName)
{
if (mFindFunction != null)
{
var obj = mFindFunction(shaderName);
if (obj != null)
{
return obj;
}
}
return Shader.Find(shaderName);
}
}
public class ShaderLoader : MonoBehaviour
{
public UnityEngine.Shader[] mShader;
public void Start()
{
ShaderHelper.SetFindShaderFunc( (shaderName) =>
{
if (mShader != null)
{
foreach(var shader in mShader)
{
if (shader != null && shader.name == shaderName)
{
return shader;
}
}
}
return null;
});
}
void OnDestroy()
{
ShaderHelper.SetFindShaderFunc(null);
}
}
1、代码中的Shader.Find修改为ShaderHelper ,包括第3方插件
2、把ShaderLoader打成Prefab,将常用的Shader配置上去,启动时加载它即可
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From 老刘@开发笔记
Post Link: Unity Shader.Find 和 AB
Post Link: Unity Shader.Find 和 AB