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

Pass texture2d array as function parameter in Enumerator

Discussion in 'Scripting' started by slaga, May 28, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    i have this enumerator that loads a folders images asynchronously and i would like to be able ta pass the array as a parameter, but when i try to do it even though i get no error the array (source) does not get populated.

    Code (CSharp):
    1.  
    2. private IEnumerator GetTextureFromPath(string _dirname) // here it would be ,Texture2D _source
    3.     {
    4.         var fileNames = Directory.GetFiles(Application.persistentDataPath + "/" + _dirname, "*.jpg");
    5.         source = new Texture2D[fileNames.Length]; // this source array would be referenced in the params.
    6.         Array.Sort(fileNames, (s1, s2) => Path.GetFileName(s1).CompareTo(Path.GetFileName(s2)));
    7.         int index = 0;
    8.         foreach (var fileName in fileNames)
    9.         {
    10.             UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture("file://" + fileName);
    11.             while (!webRequest.isDone)
    12.             {
    13.                 yield return webRequest.SendWebRequest();
    14.             }
    15.             if (webRequest.isNetworkError || webRequest.isHttpError)
    16.             {
    17.                 Debug.Log(webRequest.error);
    18.             }
    19.             else
    20.             {
    21.                 source[index] =  LoadTextureFromPath(fileName);
    22.                 source[index].name = fileName;
    23.                 index++;
    24.                 Debug.Log("Ends server comm");
    25.                 Debug.Log("FileNames :" + " " + fileName);
    26.             }
    27.         }
    28.     }
    any ideas please?

    what this function does is get all files from a directory and populate a texture2d array with each image. now this call happens on game load so since i have 4 more arrays (source1 , source2 etc) i would like the array to be in the parameters so i can tell the script as to which array to populate from that directory
     
  2. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    ok it seems its due to it being asynchronous i think because i made the function into a void one with the texture as a ref and it worked, so i will change the title accordingly!


    Code (CSharp):
    1.  public void LoadTextureArray(string _dirname, ref Texture2D[] arr)
    2.     {
    3.         var fileNames = Directory.GetFiles(Application.persistentDataPath + "/" + _dirname + "/", "*.jpg");
    4.         arr = new Texture2D[fileNames.Length];
    5.  
    6.         Array.Sort(fileNames, (s1, s2) => Path.GetFileName(s1).CompareTo(Path.GetFileName(s2)));
    7.  
    8.         //Debug.Log(string.Join("\n", fileNames));
    9.  
    10.         int index = 0;
    11.         foreach (var fileName in fileNames)
    12.         {
    13.             arr[index] = LoadTextureFromPath(fileName);
    14.             arr[index].name = fileName;
    15.             index++;
    16.         }
    17.     }