Search code examples
c#unityscriptunity-game-engine

Textures loads in editor but not in standalone (appears pink)


I'm using an asset named Simple Obj, that allows me to import an obj, their materials and the textures associate. This works fine in my editor but not in my standalone. My OBJ are not in my resources file, I take it from another file with the WWW method.

Here's how I do it, this download my OBJ, create a Gameobject and place it in my scene :

private IEnumerator DownloadAndImportAllInBackground(string url, Plane newPlane)
{
    string objString = null;
    string mtlString = null;
    Hashtable textures = null;
    GameObject planeObject = null;
    bool gameObjectPerGroup = false;
    bool subMeshPerGroup = false;
    bool usesRightHanded = true;

    yield return StartCoroutine(DownloadFile(url, retval => objString = retval));
        yield return StartCoroutine(DownloadFile(url.Substring(0, url.Length - 4) + ".mtl", retval => mtlString = retval));
        if (mtlString != null && mtlString.Length > 0)
        {
            string path = url;
            int lastSlash = path.LastIndexOf('/', path.Length - 1);
            if (lastSlash >= 0) path = path.Substring(0, lastSlash + 1);
            Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString);
            for (int i = 0; i < mtls.Length; i++)
            {
                if (mtls[i].ContainsKey("mainTexName"))
                {
                    Texture2D texture = null;
                    string texUrl = path + mtls[i]["mainTexName"];
                    yield return StartCoroutine(DownloadTexture(texUrl, retval => texture = retval));
                    if (texture != null)
                    {
                        if (textures == null) textures = new Hashtable();
                        textures[mtls[i]["mainTexName"]] = texture;
                    }
                }
            }
        }

    yield return StartCoroutine(DownloadFile(url, retval => objString = retval));

    if (objString != null && objString.Length > 0)
    {
        yield return StartCoroutine(ObjImporter.ImportInBackground(objString, mtlString, textures, retval => planeObject = retval, gameObjectPerGroup, subMeshPerGroup, usesRightHanded));
        planeObject.transform.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
        if (planeObject == null)
        {
            Debug.Log("Null gameobject");
        }
        planeObject.name = newPlane.Callsign;
        planeObject.transform.position = new Vector3((float)newPlane.X, (float)newPlane.Afl / (3.2808f * 1852f), (float)newPlane.Y);
        planeObject.transform.eulerAngles = new Vector3(0, -180 + newPlane.Heading, 0);
        planeId_Object_Dictionnary.Add(newPlane.Flight, planeObject);
    }

}

And here's what happen in my editor/standalone : enter image description here


Solution

  • Pink appearance means your build have missing material assignment on the mesh renderer component or missing shader in the build.

    If you are assigning materials or shaders at runtime make sure you include the shader in the build.

    You can force unity to include any shader by adding it to the list in Edit/Project Settings/Graphics

    enter image description here