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

Loading textures using Resources.Load and how to put them into array?

Discussion in 'Scripting' started by eses, Mar 17, 2013.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    First post here - I'm quite new to Unity and JavaScript in general.

    What I'm trying to do - In short, I'd like create an array of textures (100+), loaded from resources folder, based on file name:

    1. Have a list of image file names in text file, in Assets folder
    2. Load the list of file names into Unity, split it into array of name strings
    3. Have a bunch of images, corresponding to list file names, current plan to have them in Resources folder
    4. Load each image corresponding to name as texture from Resources folder
    5. Put loaded texture into array
    6. Assign these textures one at time to objects

    Step 5 is the problem, other steps I already verified by doing Resource.Load for single texture, and assigned it to model OK.

    So my problem is:
    - I can't seem to be able to instantiate textures to be unique textures, array will only contain last texture of for loop.
    - Seems like I only have made reference of some sort?
    - Is it actually possible to do this? There seems to just be GetPixel and create new Texture2D, but it doesn't seem to have way to add image as source?

    Sorry for noobish question, at this point of learning, I'm definitely not sure if this is even usable way to do this, maybe my code has some logic errors too.

    - I went through pile of Answers/forum links and tried to google answers, but didn't quite come up with solutions
    - Is this actually doable? Can I instantiate somehow Texture2D objects?
    - I tried Instantiate, but it says that read-only texture can't be instantiated
    - I have 100+ files to load, so I opted for this alternative instead of manually assigning each file in inspector
    - I might change to mesh objects, then it would be easier get them into Unity using Resources.Load, I think

    Any help appreciated, thanks!

    Here's my code:
    Code (csharp):
    1.  
    2. #pragma strict
    3. import System.IO;
    4.  
    5. var iconMesh: GameObject;
    6. var iconTexArr:Array;
    7.  
    8. function Start ()
    9. {  
    10.     // Start
    11.     iconTexArr = new Array ();
    12.    
    13.     // Get textfile contents
    14.     var sr : StreamReader = new StreamReader(Application.dataPath + "/icons.txt");
    15.     var fileContents = sr.ReadToEnd();
    16.     sr.Close();
    17.    
    18.     // Split each row (file name) to create array of names
    19.     var fileNames:Array = fileContents.Split("\n"[0]);
    20.    
    21.     // Variables
    22.     var nameStr:String;
    23.    
    24.     for (var i:int = 0 ; i < fileNames.length ; i++)
    25.     {    
    26.         // name
    27.         nameStr = (fileNames[i] as String);
    28.         Debug.Log ("name:" + fileNames[i]);
    29.        
    30.         // Make it Texture2D *** NOT WORKING ***
    31.         var loadedMap:Texture2D = UnityEngine.Resources.Load(nameStr,Texture2D);
    32.        
    33.         // Add to array
    34.         iconTexArr.Push(loadedMap);
    35.     }
    36.    
    37.     // Only last one is in array
    38.     Debug.Log ("Arr:" + iconTexArr);
    39. }
    40.  
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    is there any benefit in having a .txt with the names, if the names of the actual textures are the same?

    I guess you should probably just put all these textures in a folder and use Resources.LoadAll(path, Texture2d)
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there, and thank you for reply.

    Not actually, but so far I was under impression that there can be only one Resources folder. But now I read that "Multiple "Resources" folders" can exist. That is why I wanted to have some sort of list to filter out only certain files, but maybe I could just sort them in sub-folders and use LoadAll.

    From manual:
    Code (csharp):
    1.  
    2. var textures : Object[] = Resources.LoadAll("Textures", Texture2D);
    3.  
    So I guess I'll try this first. Seems like loading images as Texture2D is bit complicated at least for me. I also tried www, got some problems with it too.
     
  4. Themp

    Themp

    Joined:
    Nov 8, 2011
    Posts:
    96
    var textures : Object[] = Resources.LoadAll("Textures", Texture2D); isn't really nice

    Try
    Code (csharp):
    1.  
    2. Texture2D[] textures = Resources.LoadAll("Textures") as Texture2D[];
    3.  
    or in javascript (might not work as my javascript is really rusty)

    Code (csharp):
    1.  
    2. var textures:Texture2D[] = Resources.LoadAll("Textures") as Texture2D[];
    3.  
    this way you get access to the properties from texture2D and arent limited to object inheritations
    (also easier to understand for other programmers what you are doing)
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    ValooFX, Themp

    Thank you for help! I got texture loading working.

    However, I couldn't get Texture2D[] working, I had to use Object[]. So This code works, but if I try to just change Object to Texture2D, it won't.

    What comes to arrays as side note, I'm bit too noob with all kinds of Unity supported lists and arrays, I take it these will be fixed-size generic arrays.

    Code (csharp):
    1.  
    2. var textures:Object[];
    3.  
    4. function Start ()
    5. {
    6.     // Start
    7.     count = 0;
    8.     textures = Resources.LoadAll("Textures", Texture2D);
    9.     texCount = textures.Length;
    10.     iconMesh.renderer.sharedMaterial.mainTexture = textures[count];
    11.     Debug.Log(textures.Length);
    12. }
    13.  
     
    Last edited: Mar 18, 2013
  6. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    yes, it has to be object[]
    you have to foreach/for and cast to texture2d

    using system.collections.generic;
    List<Texture2D> textureList = new List<Texture2D>;
    foreach (object o in Resources.LoadAll("Textures", Texture2D)) {
    textureList.Add(o as Texture2D);
    }
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    ValooFX:

    I suspected that type needs to be object, thank you for clarification. Your foreach loop looks very elegant, I'll definitely have to find a JavaScript book, I had no idea that return value of Resources.LoadAll could be iterated like that.

    Thanks!
     
  8. surbhitw

    surbhitw

    Joined:
    Jul 18, 2017
    Posts:
    2
    Hi,

    Is there a way to load only some of the images?

    Like I have all the images in the Resources folder and I have a list of image names( not all the image names are there on this list)? How can I load all those images (whose names are on the list) to an array?