0
\$\begingroup\$

I'm working on a game which involves loading pictures from a server and rendering them as sprites. The images are stored as PNGs and open normally from on the server. I get no errors when downloading. And as a test I saved the images to files and they seem to save properly.

But, when I try rendering the sprites in the game, Unity doesn't understand what they are and just shows red questionmarks. The sprites load fine if I try loading them from Resouces/Assets folder.

Here's how I'm downloading the pictures: Using UnityWebRequest, images are downloaded as Texture2D objects, converted to PNGs and then saved to a static-class called GameData.

Here's what the code for the image download looks like:

foreach (String imgName in imgNames) {

        using( UnityWebRequest imgRequest =  UnityWebRequestTexture.GetTexture(pathName+imgName)) {
                
           yield return imgRequest.SendWebRequest();

         if(imgRequest.isNetworkError || imgRequest.isHttpError) { 
             ...
         }
           

          else  if(imgRequest.isDone) {
            Texture2D texture = DownloadHandlerTexture.GetContent(imgRequest);
            byte[] spriteBytes = texture.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "/debug_sprites/"+imgName+".png" , spriteBytes );
            var rect  = new Rect(0, 0, 8, 8);
            Sprite spriteS = Sprite.Create(texture, rect, new Vector2(0.5f,0.5f));
            GameData.SetImg(imgName, spriteS);
                    
          }         
          downloadStatus.text = "Downloading file "+ ++i +" of "+(imgNames.Count+audioNames.Count);
   }

The code for where the image is saved in GameData:

public static void SetImg(string imgName, Sprite img) {

             imgDict.Add(imgName, img);

}

And here's the code where the sprites are supposed to get rendered to the game:

firstImage = GameData.GetImg(choice1.text);
//firstImage = Resources.Load<Sprite> (choice1.text);
GameObject go = GameObject.Find ("ChoiceOneTarget");
SpriteRenderer spriteR = go.GetComponent<SpriteRenderer> ();
spriteR.sprite = firstImage;

Note: I've tried downloading both with and without the extension name, but it didn't change anything.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome. Go to Application.dataPath + "/debug_sprites/" and try to open what your game wrote to disk. If it doesn't open, install any free hex editor and look at the first bytes. Does it have a valid PNG header? PNGs always start with bytes 89 50 4E 47 0D 0A 1A 0A. If that signature is missing, it's not a PNG. Either server disallows hotlinking or something else is happening. But we can start there. If your game is being debugged in a real device, like Android phone, you may lack required permissions. If server disallows hotlinking, you may be downloading a html text file with a message. \$\endgroup\$ – Hatoru Hansou Oct 24 '20 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.