Search Unity

Resources.LoadAll returns empty Array

Discussion in 'Scripting' started by Funinvegas, Mar 4, 2014.

  1. Funinvegas

    Funinvegas

    Joined:
    Mar 3, 2014
    Posts:
    3
    I am stuck. I'm trying to create sprites from a "Tiled" json exported file. I can read the JSON just fine, but I can't load the textures. In my Assets folder I have a PublicDomain folder. In my PublicDomain folder I have a tiles_12.png file. I imported the file as SpriteMode:Mulitple. So I have each tile nicely listed as tiles_12_0 thru tiels._12_236.

    However, when I try to load these textures, LoadAll returns an empty Array. I've tried every Path string I can think off, and I've tried a few other load functions from the documentation. I've tried prefixing Application.dataPath. I've looked at other question posts, and understanding the Path seems to be assumed in those answers. I don't want to load individual files with WWW if they are already available assets.

    Any help appreciated thanks,
    Josh.

    Code (csharp):
    1.  
    2.         var resourceName = "Assets/PublicDomain/tiles_12";
    3.         var textures = Resources.LoadAll(resourceName, Texture2D);
    4.         Debug.Log(resourceName);
    5.         Debug.Log(textures);
    6.         Debug.Log(textures.Length);
    7.         textures = Resources.LoadAll(resourceName, Sprite);
    8.         Debug.Log(resourceName);
    9.         Debug.Log(textures);
    10.         Debug.Log(textures.Length);
    11.  
    12.  
     
    Last edited: Mar 4, 2014
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Need to be in a folder called Resources, look it up.
     
  3. Funinvegas

    Funinvegas

    Joined:
    Mar 3, 2014
    Posts:
    3
    I definitely didn't know there was a special directory called Resources.
    I now have a folder in Assets called "Resources"
    And in that folder I put "PublicDomain"
    And in that is tiles_12.

    Code (csharp):
    1.  
    2. var resourceName = "PublicDomain/tiles_12";
    3. textures = Resources.LoadAll(resourceName, Texture2D); // Returns Array size 1
    4. textures = Resources.LoadAll(resourceName, Sprite); // Returns Array size 237
    5.  
    I'm unblocked now, thanks!
     
  4. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    514
    The correct way to do it is like this

    Setup a folder in your project for example /Resources/TextureTemplates
    Then put some files inside it for example abs.bytes sdfd.bytes

    UnityEngine.Object[] textureTemplates = Resources.LoadAll("TextureTemplates", typeof(TextAsset));

    All you do is simply set the typeof to the type of asset you desire

    Then for example

    foreach (TextAsset ta in textureTemplates)
    {
    Debug.Log(ta.bytes.Length);
    }