Im trying to apply a simple texture to a Gameobject (plane) that i have created during runtime. Im choosing a simple jpeg image stored in my “Resources” folder, loading it and trying to apply it as a texture.
Heres the following code snippet in my Gameobject class:
Texture2D staticTex = (Texture2D)Resources.Load("test");
Debug.Log("ULog Texture Size Width " + staticTex.width + "ULog texture Size Height " + staticTex.height);
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
renderer.material.mainTexture = staticTex;
This works fine on the editor, but i get a pink texture on the iPhone.
Im using:
Unity : 5.5.0f3
Xcode : 8.0
iOS : 10.0.2
This used to work as expected in previous builds but has just stopped working now.
I have tried:
- Clearing the GI cache
- Making sure shaders are included by adding a test shader.
Not sure what is going wrong, or what has changed. Any help is appreciated!
Update: The behavior is the same on an android device as well. Just tested with a galaxy s7!
After a few days of trying things i finally got the solution to this. It was an issue with unity not finding/compiling the shader for any of the builds. I just had to add a dummy material in my “Resources” folder (doesnt need to be used) and unity picks this up and compiles the shader for the build.
This happened as a result of me re-organizing the folder structure in my project. I organized all my scripts, assets in my unity project and created a “Resources” folder where i add textures and Prefabs. At this point i had no materials here as all my materials were being created and added at run time. Somehow unity considered this is the project not utilizing any materials and that it was not required to compile any shader.
Adding a dummy shader in my “Resources” folder fixed this issue. Now everything works as expected.
This link Textures from local works in editor missing in build - Questions & Answers - Unity Discussions gave me the solution for my problem. Hope this helps anyone in the future facing the same issue.
A simple way to do this is:
- Go to >> Project Settings >> Graphics >> Always Included Shaders
- Add 1 in size (to add another element)
- Select the Shader you’re going to or using it
This way you’re going to compile all needed shaders for your build.
Don’t forget to select the correct platform (iOS/Android/Standalone)
Thank you for giving me a clue to solve a similar problem. In my case, I was trying to add a Video Player component to a plane programmatically, and the video would work in editor and not in the iOS build. Tried both adding dummy shader and dummy material into the Resources folder and it wouldn’t work.
So what I did was to create a dummy plane (with transform scale as 0 and position behind the camera) that has the Video Player component with a video attached. Problem solved!