- Home /
load all png files as resource but NOT from resource folder
I have an issue. I need to be able to load all the *.png files at run time from a folder under steamingassets, these are dynamic images and can changed after build. here is a sample of my code.
using UnityEngine;
using System.IO;
using System.Collections;
public class loadallimages : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
string folderpath = Application.streamingAssetsPath + @"/Tiles/";
// string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
Object[] textures = Resources.LoadAll(folderpath);
int fred = Random.Range(0, textures.Length);
Debug.Log(string.Format("fred is crurrently:{0}", fred));
Debug.Log(textures.Length);
Debug.Log(textures[fred].name);
Texture2D texture = (textures[fred]) as Texture2D;
go.GetComponent<Renderer>().material.mainTexture = texture;
}
}
Resources.LoadAll wont load them at all (fred=0).
Resources.load wont load, I get a list of null objects :(.
code to load each file (note: you have to remove the ".png" from the file name to get the Resource.load to load them)(but it doesn't)
string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
Object[] textures = new Object[FileList.Length];
for (int ten =0; ten> FileList.Length;ten++)
{
textures[ten] = Resources.Load(FileList[ten].Substring(0,FileList.Length-4)); // removes the ".png" from the end of the file name
}
I would like the ability to load all the tile images at start up form a streamingassets/tiles folder location to use them then discard them to free up memory.
I understand that placing them in the resources folder and then calling Resources.loadAll("Tiles") would load them however the files will be changed after release hence the need to use the steamingassets folder. if there is an option them please help cheers D2
Answer by dittytwo · May 06, 2015 at 12:48 PM
I actually fixed my own issue with the code above
Change -->Texture2D texture = new Texture2D(32,32); to -->Texture2D tex = new Texture2D(x, y, TextureFormat.DXT5, false);
The TextureFormat is the key here as this sets the texture to a compressed texture; see link text for details. this then freed up some more memory for the processing
@dittytwo: About how many images are we talking about? If you want to read them all in you should process one at a time. It makes not much sense to store the raw bytes for every image in a dictionary as the raw data isn't needed anymore once you load it into an Texture.
So it's much more memory friendly to read in file by file and directly create your Texture. Also note that Texture2D is a "tracked object" in Unity. That means it doesn't get garbage collected once all references to it are gone. You can still use FindObjectsOfType to retrieve any texture loaded. If you don't need a dynamically created texture anymore you should use Destroy() on it.
Answer by Baste · May 05, 2015 at 02:32 PM
Resources.Load is specifically for Resources, which is defined as "stuff in the resources folder when you build"
As far as I can tell, you'll have to handle stuff in the streamingassets in a much more low-level manner. If you have -.pngs, you should read the bytes of the files (System.IO.ReadAllBytes), and use the Texture2D method LoadImage which converts a byte array into an image.
In general, if you're going to be supplying arbitrary data to your application at runtime, you'll have to write the code that handles parsing that data yourself.
I have tried this.
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class loadallimages : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
string folderpath = Application.strea$$anonymous$$gAssetsPath + @"/Tiles/";
string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
Debug.Log("readingin the files into byte");
Dictionary<string, byte[]> readinPictures = returnreadinpictures(FileList);
Debug.Log("read all the files in");
Texture2D texture = new Texture2D(32,32);
Debug.Log("adding the bytes as a texture");
texture.LoadImage(readinPictures[FileList[5]]);
Debug.Log("texture added");
Debug.Log(string.Format("this is the file name {0} and this is the bytes{1}", FileList[5], readinPictures[FileList[5]].ToString()));
go.GetComponent<Renderer>().material.mainTexture = texture;
}
/// <summary>
/// returns the list of files pasted as filename , bytes read in
/// </summary>
/// <param name="filelist">pass a list of strings of the file location</param>
/// <returns>Dictionary<string,byte> ("filelocation", bytes of read file)</returns>
private Dictionary<string, byte[]> returnreadinpictures(string[] filelist)
{
Dictionary<string, byte[]> fred = new Dictionary<string, byte[]>();
foreach (string claire in filelist)
{
fred.Add(claire, (File.ReadAllBytes(claire)));
}
return fred;
}
}
Basically it caused the ide to crash as it went of 2Gb (unity32.exe) there has to be a better way
Your answer
Follow this Question
Related Questions
Loading user custom image. 2 Answers
Multiple Cars not working 1 Answer
How do I iterate over a large Texture2d quickly? 1 Answer
Change texture of RawImage 2 Answers
Distribute terrain in zones 3 Answers