Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved] Loading Image From StreamingAssets

Discussion in 'Scripting' started by EdGunther, Jul 28, 2019.

  1. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Hello World,

    I seem to be unable to load images from StreamingAssets during runtime. Any ideas?

    I was doing this in Resources and simply using Resources.Load(), but I want users to be able to customize the images they can use. Anyway to load an image from StreamingAssets???

    Thanks,
    Ed
     
    Fenikkel likes this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Thanks for the reply! I'm unsure how to use that to get an image asset from file path.

    This is what I have:
    Code (CSharp):
    1.     public Image myNameImage;
    2.  
    3.     string folderPath;
    4.     string[] filePaths;
    5.  
    6.     int n;
    7.  
    8.     private void Awake()
    9.     {
    10.         ImageLoader();
    11.     }
    12.  
    13.     void ImageLoader()
    14.     {
    15.         n = gameObject.GetComponent<ScoreButtonScript>().myNumber;  //For Choosing a file
    16.  
    17.         folderPath = Application.streamingAssetsPath + "/NameImages/GameNames/";  //Get path of folder
    18.         filePaths = Directory.GetFiles(folderPath, "*.png"); // Get all files of type .png in this folder
    19.  
    20.         myNameImage.sprite = Resources.Load<Sprite>(filePaths[n]);  //This is what I need help with
    21.  
    22.         //Forexample, filePaths[n]= C:/Users/me/game/Assets/StreamingAssets/NameImages/GameNames/filename.png
    23.     }
    I understand Resources.Load() wont work with the StreamingAssets file path, but I don't understand how ImageConversion.LoadImage() can help me.



    Cheers!
    Ed
     
    Last edited: Jul 29, 2019
    Powzone likes this.
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    This method allows to convert a byte-array, that represents the data of a png file, into an Unity Texture2D object.

    In order to load the file, or byte data, you can do:
    Code (CSharp):
    1. bytes[] pngBytes = System.IO.File.ReadAllBytes(path);
    Then pass this data to ImageConversion.LoadImage:
    Code (CSharp):
    1. Texture2D tex = new Texture2D(2, 2);
    2. tex.LoadImage(pngBytes);
    Looking at the code you posted, it seems it's used for the UI. The Image Component expects a Sprite, but you can also use the "RawImage" Component which expects a Texture2D, so you don't have to create a Sprite at runtime for that Texture.

    Hope it helps.
     
  5. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Thank you Peter for your time. My game is essentially done now that this works.

    I am considering this solved and posting my script below

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.IO;
    4.  
    5. public class NameImageFinder : MonoBehaviour
    6. {
    7.     public Image myNameImage;
    8.  
    9.     string folderPath;
    10.     string[] filePaths;
    11.  
    12.     int n;
    13.  
    14.     private void Awake()
    15.     {
    16.         //Designation for which file is to be loaded
    17.         n = gameObject.GetComponent<ScoreButtonScript>().myNumber;
    18.  
    19.         ImageLoader();
    20.     }
    21.  
    22.     void ImageLoader()
    23.     {
    24.         //Create an array of file paths from which to choose
    25.         folderPath = Application.streamingAssetsPath + "/NameImages/GameNames/";  //Get path of folder
    26.         filePaths = Directory.GetFiles(folderPath, "*.png"); // Get all files of type .png in this folder
    27.  
    28.         //Converts desired path into byte array
    29.         byte[] pngBytes = System.IO.File.ReadAllBytes(filePaths[n]);
    30.  
    31.         //Creates texture and loads byte array data to create image
    32.         Texture2D tex = new Texture2D(2, 2);
    33.         tex.LoadImage(pngBytes);
    34.  
    35.         //Creates a new Sprite based on the Texture2D
    36.         Sprite fromTex = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    37.  
    38.         //Assigns the UI sprite
    39.         myNameImage.sprite = fromTex;
    40.     }
    41. }
    42.  
    Should I make the filePaths array in a parent object rather than in every instance of this script? Yes.
    Will I? No.

    Did I do this?
    Also no, didn't really get how.

    Does my code work? Yes.
     
  6. Fenikkel

    Fenikkel

    Joined:
    Sep 24, 2019
    Posts:
    20
    My code. Yo can copy paste and will work. Just fill the inspector and magic happends :D

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class StreamingAssetsImageLoader : MonoBehaviour
    5. {
    6.     public Image[] m_EditableImages;
    7.     public string[] m_ImagesNames;
    8.  
    9.  
    10.     private void Awake()
    11.     {
    12.         if (m_EditableImages.Length == m_ImagesNames.Length) //Si es 0 dejamos las que estaban ya
    13.         {
    14.  
    15.             LoadImages();
    16.  
    17.         }
    18.         else {
    19.             Debug.LogWarning("==Failed to load images==");
    20.         }
    21.  
    22.     }
    23.  
    24.     private void LoadImages()
    25.     {
    26.  
    27.         for (int i = 0; i < m_EditableImages.Length; i++)
    28.         {
    29.             if (m_EditableImages[i] != null)
    30.             {
    31.                 string path = GetImagePath(m_ImagesNames[i]);
    32.                 m_EditableImages[i].sprite = GetSpritefromImage(path);
    33.                 m_EditableImages[i].preserveAspect = true;
    34.             }
    35.             else {
    36.                 Debug.LogWarning("Null references in EditableImages");
    37.             }
    38.         }
    39.  
    40.     }
    41.  
    42.     private string GetImagePath(string imgName) {
    43.  
    44.         //Create an array of file paths from which to choose
    45.         string folderPath = Application.streamingAssetsPath + "/Imatges/";  //Get path of folder
    46.         //string[] filePaths = Directory.GetFiles(folderPath, "*.png"); // Get all files of type .png in this folder
    47.         string path = folderPath + imgName + ".png"; //han de ser formato png
    48.  
    49.         return path;
    50.     }
    51.  
    52.     private Sprite GetSpritefromImage(string imgPath) {
    53.  
    54.         //Converts desired path into byte array
    55.         byte[] pngBytes = System.IO.File.ReadAllBytes(imgPath);
    56.  
    57.         //Creates texture and loads byte array data to create image
    58.         Texture2D tex = new Texture2D(2, 2);
    59.         tex.LoadImage(pngBytes);
    60.  
    61.         //Creates a new Sprite based on the Texture2D
    62.         Sprite fromTex = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    63.  
    64.         return fromTex;
    65.  
    66.     }
    67.  
    68. }
     
  7. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
    I don't understand. If they're just going to use normal IO, what are they going to do with a separate folder name?
     
  8. unity_F749414654ED6768BEA8

    unity_F749414654ED6768BEA8

    Joined:
    Apr 7, 2021
    Posts:
    1
    I have to use streaming assets for loading the textures on 3d Model material and i have no idea to how to access them for android devices.
    Any help!!
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
  10. ismaelnascimentoash

    ismaelnascimentoash

    Joined:
    Apr 2, 2017
    Posts:
    30
    Solved this problem, load texture with streaming assets too !!