Search Unity

load folder into texture2D array

Discussion in 'Scripting' started by realm_1, May 20, 2011.

  1. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Hi

    I want to load all images of a folder into a Texture2D array. And the folder shoud be selectable from the inspector. Is this possible? When yes, how? An example would be helpfull.
    Thanks

    realm_1
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    No not possisble unless you write an own custom editor for it
     
  3. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Ok
    Thanks
    I have about 300 images and I want to load them in sequence into a gui but when I have to make for each image a variable then its very complex. Do you have an idea to do that in a easy way?
     
  4. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Its maybe possible with TextureImpoter?
     
  5. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    I believe this is what dreamora was saying is impossible (without writing a custom inspector). However, you can use a string to do it if you really want...

    Code (csharp):
    1.  
    2. public class ExampleLoader {
    3.     public string _filePath = "";
    4.    
    5.     void Awake () {
    6.         Object[] textures = Resources.LoadAll(_filePath, typeof(Texture2D));
    7.     }
    8. }
    9.  
    Obviously you would want to do some sort of validation to ensure _filePath is a valid file path. If your project looks like this...

    Assets/Resources/AwesomeImages/DesiredImages/

    You would want _filePath to = "AwesomeImages/DesiredImages"

    They *have* to be in the Resources folder though.
     
  6. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Many thanks.

    But it stills one problem: I added your script to my project and it gives my an array out of range exception out.
    Do you know why?
    Heres my code I use (I've modified it a little bit):


    var _filePath : String = "";
    var e = 1;
    var textures : Object[];

    function Update () {
    }

    function Awake() {
    textures = Resources.LoadAll(_filePath, typeof(Texture2D));
    }

    function OnGUI() {
    GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),textures[e],ScaleMode.StretchToFill,true,0);
    e++;
    if(e >= textures.length-1) {
    e = 1;
    }
    }
     
  7. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    and variable _filePath is Assets/Resources/test

    But it doesn't work, neighter.
     
  8. Zansafer

    Zansafer

    Joined:
    Jan 21, 2011
    Posts:
    26
    are you testing with one file by chance? textures[e] and you start e = 1, arrays start at 0 so if your testing with one file, textures[1] wouldnt exist - even if thats not it, tagging this post cause i wanna know how you fix it :)

    What does this show:
    Debug.Log(textures.length)
     
  9. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    It doesnt work even if I start with e = 0.
    Debug.Log(textures.length) gives me 0 out, but there are 20 images.

    I set this variable to:

    var _filePath : String = "Resources/test";

    In the project folder I created a folder Resources and in it a folder test. I hope this is correct.
    so myproject/Assets/Resources/test and there are the images.
     
  10. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TextureTest : MonoBehaviour {
    6.    
    7.     public string _imagePath;
    8.    
    9.     Texture2D[] _typedTextures;
    10.     int _displayed = 0;
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.         Object[] textures = Resources.LoadAll(_imagePath, typeof(Texture2D));
    15.         _typedTextures = new Texture2D[textures.Length];
    16.         for (int i = 0; i < textures.Length; i++){
    17.             _typedTextures[i] = (Texture2D)textures[i];
    18.         }
    19.         Debug.Log("Textures Loaded: " + _typedTextures.Length);
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (_typedTextures != null  _typedTextures.Length > 0){
    25.             _displayed++;
    26.             if (_displayed >= _typedTextures.Length){
    27.                 _displayed = 0;
    28.             }
    29.         }
    30.     }
    31.    
    32.     void OnGUI(){
    33.         if (_typedTextures != null  _typedTextures.Length > 0){
    34.             GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height), _typedTextures[_displayed], ScaleMode.StretchToFill, true);
    35.         }
    36.     }
    37.    
    38. }
    39.  
    40. // This code is a bit of a mess, I know >.>
    41.  
    I just tested that and it worked fine. You definitely don't want _filePath to == Assets/Resources/test - in that case you would just want it to == test.

    In my test all 5 textures were in Assets/Resources/ImageTest/Examples/ and my _imagePath variable was set to "ImageTest/Examples"

    Do arrays in JS start at 1 instead of 0?
    I think in JS you would do Resources.LoadAll(_filePath, Texture2D) instead of typeof(Texture2D).

    Also, make sure you rotate the images in Update and not in OnGUI - OnGUI gets called multiple times per frame - you probably want to rotate them once per frame.
     
  11. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Yeah thats really amazing!
    It works fine.
    Many many thanks.

    I think that arrays in Java also starts with 0.
     
  12. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    But I have one last question: its require to do somthing special with the selected folder with the images so that it embblemed in the build?
     
  13. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Nope, that's why it has to be in that specific Assets/Resources folder.

    Everything in Assets/Resources is included with the build, and accessible via Resources.Load/LoadAll in the final build. If targeting web or iOS in particular, it's pretty important to make sure you only include things in the resource folder you need to have there for that reason.
     
  14. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Ok thank you.
     
  15. cenghover

    cenghover

    Joined:
    Sep 25, 2012
    Posts:
    9
    this one is working:

    private var textures:Object[];

    function Start () {

    textures=Resources.LoadAll("directory") as Object[];

    }


    thanks.
     
  16. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    cenk5355

    how do you use an Object?
    I can't use it as a texture2d[] array.

    KyleStaves
    how do I assign a path?
    I don't use c#
    can I make the resulting texture2d[] array static so I can call it from other scripts?

    This all looks nice, but I can't see a way to use any of it.

    I'm trying to load user created textures at runtime in JavaScript.

    my build can create and edit files in the Application.dataPath but I haven't got jpg images working yet and I want to handle a few of them in an array.
     
    Last edited: Jan 27, 2014
  17. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    How to rotate a texture2d[] after it been loaded