- Home /
Dynamic texture is visible in editor but pink when is built
I know this thread may sound 'repetitive', but I've been researching in other posts trying to follow possible solutions with no luck...
I have a small script for creating a really basic terrain procedurally, in order to archive this first I create a mesh with its respectives vertices, uvs and triangles (I simply want a flat 2D rectangle that I could use as a level background for my game). Then I create a Texture2D with a 1024x1024 resolution that I apply to it, generating random green pixels.
I apply, scale, and finally add a shader (Unlit/Transparent), in order to remove the dark from my mesh.
When I run my game in the editor everything works fine, mesh and texture is rendered as desired, however, when I build my project and run the executable the mesh is pink, this is my code:
using UnityEngine;
using System.Collections;
public class TerrainScript : MonoBehaviour {
void Start () {
const ushort Width = 1024;
const ushort Height = 1024;
const byte MinColor = 128;
const byte MaxColor = 200;
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {
new Vector3( 1, 1, 0),
new Vector3( 1, -1, 0),
new Vector3(-1, 1, 0),
new Vector3(-1, -1, 0)
};
mesh.uv = new Vector2[] {
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(0, 0)
};
mesh.triangles = new int[] {0, 1, 2, 2, 1, 3};
mesh.RecalculateNormals();
Texture2D Texture = new Texture2D(Width, Height, TextureFormat.ARGB32, false);
//Texture.filterMode = FilterMode.Point;
ushort i, j;
for (i = 0; i < Width; i++) {
for (j = 0; j < Height; j++) {
Texture.SetPixel(i, j, new Color(0.0f, Random.Range(MinColor, MaxColor) / 255.0f, 0.0f));
}
}
Texture.Apply();
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<Renderer>().material.mainTexture = Texture;
GetComponent<Renderer>().material.shader = Shader.Find("Unlit/Transparent");
transform.localScale = new Vector3(20.0f, 20.0f, 0.0f);
}
void Update () {
}
}
I read that when this happens is because Unity has a problem locating the resource or the mesh itself is causing the problems. However, I tried by going to Edit > Project Settings > Graphics and adding one index to the shaders array with the used shader Unlit/Transparent as value, built again, same result.
I tried also by commenting the line where I apply the texture so it would look darker but ignore the shader, still pink, so I suspect it may be a problem with the mesh or the texture, I tried by creating an external texture, loading it in the script, resizing it and changing the pixels and I get the same.
If anybody has an idea of what I'm doing wrong or knows if there's a better way to archive what I want I will appreciate it...
Thank you...
Answer by Paul-Jan · Apr 15, 2016 at 12:45 PM
From the documentation (emphasis mine):
Note that a shader might be not included into the player build if nothing references it! In that case, Shader.Find will work only in the editor, and will result in pink "missing shader" materials in the player build. Because of that, it is advisable to use shader references instead of finding them by name. To make sure a shader is included into the game build, do either of: 1) reference it from some of the materials used in your scene, 2) add it under "Always Included Shaders" list in ProjectSettings/Graphics or 3) put shader or something that references it (e.g. a Material) into a "Resources" folder.
I'm a bit confused by your description, if you mean to say you already added the shader to Always Included Shaders... could you please post a screenshot of the GraphicsSettings inspector so we can verify you did this correctly?
Hello Paul, thank you for your answer,
These are my current graphics settings:
As I wrote in my description, it doesn't seem like a shader problem, I believe it's more related with the texture/mesh itself, because when I comment the line where I apply the shader (IOW, I just ignore it and shouldn't be imported), and build my project, the resulting shape is still pink, and when I build with the shader it's white.
Your answer
Follow this Question
Related Questions
creating a mesh procedurally 4 Answers
How to resave generated meshes with Editor Scripts? 0 Answers
Starting mesh generation thread before changing chunk transform position 1 Answer
Realtime lighting with generated mesh causes light bleed at seams / edges (minecraft clone) 2 Answers
Mesh building changes in Unity 3.5.6 0 Answers