- Home /
Seeking help to load unlit/texture dynamically in Unity 3D using C# and substitute of Shader.Find,Seeking help to load unlit/texture dynamically in Unity 3D using C#, substitute of Shader.Find
While developing an application recently I found a typical problem and seeking your help to resort into a solution. Previously I used to load unlit transparent textures dynamically with the following code:
fileData = File.ReadAllBytes(filePath);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
GetComponent<Renderer>().material.mainTexture = tex;
rend = gameObject.GetComponent<Renderer>();
Shader shader = Shader.Find("Unlit/Texture");
rend.material.shader = shader;
Recently I found that Shader.Find("Unlit/Texture") doesn't work in case of Windows build, specifically when I'm running the exe with my Unity Editor closed. I'm trying to find a substitute to this without any luck so far. I know in the Unity official documentation they have mentioned about the unreliability of Shader.Find, but no substitute to that is mentioned anywhere.
Will eagerly wait for a reply.
Thanks in advance for your time.,While developing an application recently I found a typical problem and seeking your help to resort into a solution. Previously I used to load unlit textures dynamically with the following code:
fileData = File.ReadAllBytes(filePath);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
GetComponent<Renderer>().material.mainTexture = tex;
rend = gameObject.GetComponent<Renderer>();
Shader shader = Shader.Find("Unlit/Texture");
rend.material.shader = shader;
Recently I found that Shader.Find("Unlit/Texture") doesn't work in case of Windows build, specifically when I'm running the exe with my Unity Editor closed. I'm trying to find a substitute to this without any luck so far. I know in the Unity official documentation they have mentioned about the unreliability of Shader.Find, but no substitute to that is mentioned anywhere.
Answer by BastianUrbach · Jul 17, 2021 at 03:58 PM
The problem is that Unity automatically detects which shaders (and other assets) are used in the application and only includes those in the build. In the UnityEditor, everything works fine but in the build, the shader does no longer exist. There are a couple different ways around that. Just pick whichever is the most convenient for you.
Instead of finding the shader dynamically, make a public field of type Shader and assign the shader there via the inspector, then use that field instead of Shader.Find.
Add it to the list of always included shaders (Edit > Project Settings > Graphics > Always Included Shaders)
Place it in a folder called Resources
Use it somewhere in a scene that's part of the build. Doesn't have to be visible, you can just put it on a dummy GameObject and disable it. It's also sufficient to assign it to any public field of a MonoBehaviour, it doesn't need to be on a renderer. The only thing that matters is that Unity thinks you might need it.
Your answer
Follow this Question
Related Questions
Reflective surface/texture bug in build only (works in play mode in editor) 1 Answer
Distribute terrain in zones 3 Answers
Animated Texture based on Movement 0 Answers
Unity - Simple Water 3 Answers