Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Loading Images as Textures at runtime Streaming Assetts

Discussion in 'Scripting' started by pantang, Sep 16, 2020.

  1. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    I am trying to load images from the streamingassetts folder at runtime I can get the paths fine but it does not load up any textures/data any ideas where I am going wrong?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5. using System.Linq;
    6. using System;
    7.  
    8. public class LoadResources : MonoBehaviour
    9. {
    10.  
    11.     public string[] posterPaths;
    12.     public string[] billboardPaths;
    13.  
    14.     public Texture[] posters;
    15.     public Texture[] billboards;
    16.  
    17.     public AudioClip[] music;
    18.  
    19.     public void GetImages()
    20.     {
    21.         //find our files
    22.         posterPaths = Directory.GetFiles(Application.streamingAssetsPath + "/Images/Posters/", "*.png");
    23.         billboardPaths = Directory.GetFiles(Application.streamingAssetsPath + "/Images/Billboards/", "*.png");
    24.        
    25.         //Match arrays
    26.         Array.Resize(ref posters, posterPaths.Length);
    27.         Array.Resize(ref billboards, billboardPaths.Length);
    28.  
    29.         //load the data
    30.         for (int i = 0; i > posters.Length; i++)
    31.         {
    32.             //Get file
    33.             byte[] imageData = System.IO.File.ReadAllBytes(posterPaths[i]);
    34.  
    35.             //Create texture
    36.             Texture2D tex = new Texture2D(2, 2);
    37.             tex.LoadImage(imageData);
    38.             posters[i] = tex;
    39.         }
    40.         for (int i = 0; i > billboards.Length; i++)
    41.         {
    42.             LoadImageFromFile(billboards[i], billboardPaths[i]);
    43.         }
    44.     }
    45.  
    46.     void LoadImageFromFile(Texture newImage, string path)
    47.     {
    48.         //Loadfile
    49.         byte[] imageData = System.IO.File.ReadAllBytes(path);
    50.  
    51.         //Create texture
    52.         Texture2D tex = new Texture2D(2, 2);
    53.         tex.LoadImage(imageData);
    54.         newImage = tex;
    55.     }
    56.  
    57.     public void GetMusic()
    58.     {
    59.         //load audio tracks
    60.     }
    61. }
    Any tips on how to load wav/mp3 would be awesome as well.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    The newImage parameter in this method is local variable, so at call site your array is left unmodified. You posters however should be loaded correctly if your file reading code is correct (don't remember if Directory.GetFiles returns just file names, or full paths).

    The simplest way would be to use UnityWebRequest passing file:// URI:
    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestMultimedia.GetAudioClip.html
     
    pantang likes this.
  3. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Sadly neither get loaded, I get all the correct files but the images for the textures get left blank sadly
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    There is UnityWebRequest API for textures as well, though you need to debug your code, I can't spot anything in particular incorrect from the general use.