Hi,
I'd like the application to load all images from a given folder (a subfolder of the installation directory) at the beginning. The reason why I don't use assets is that it should be possible to exchange the images after installation.
I've tried this piece of code so far in C#:
public ArrayList imageBuffer = new ArrayList();
private void LoadImages()
{
//load all Texture2D files in Resources/Images folder
Object[] textures = Resources.LoadAll("Images");
for (int i=0; i < textures.Length; i++)
imageBuffer.Add(textures*);*
*}*
*```*
*<p>Will the resources folder be copied to the installation directory during installation? Or is this not the right way if the contents of the image folder should be exchangeable after installation? How to do it right?</p>*
Sorry, I just didn't want people to forget about my issue, so I bumped ;) And this time I need the "code snippet" functionality, which isn't available in comments...
I don't get around this www stuff...and how to wait at program start for the image to be loaded completely...Can anyone explain it using C#?
In Start() I call LoadImages() which shall load all images in a given folder (C:\Assets\Images). But even if I write just
using System;
using RasterEdge.Imaging.Basic.Core;
using RasterEdge.Imaging.Basic;
public static REImage OpenImageFile(string filePath);
public static void LoadImageFromFileDemo()
{
string fileName = "c:/Sample.png";
REImage reImage = REFile.OpenImageFile(fileName);
}
public static ArrayList textures = new ArrayList();
for (int i = 0; i <= data.contours.Count; i++)
{
textures.Add(Resources.Load(""+i, typeof(Texture2D)));
}
If you don’t want to use the WWW www from Unity, you can just use C#'s System.IO
public Dictionary<string, Sprite> AllSprites = new Dictionary<string, Sprite>(); //Store all Sprites created into this dictionary container
string artPath = "C:\\GameFolder\\Art\\"; //All the PNG's are in my "Art" folder and subfolders.
using System.IO; //This is what we use instead of Unity's WWW. Unity also has this stuff in UnityEngine.Windows.
void LoadAllSpritesFromPngFilesInFolderAndAllSubFolders()
{
//Get all files PNG in "ART" directory
string[] allFilePaths = Directory.GetFiles(artPath, "*.png", SearchOption.AllDirectories);
//SearchOptions.AllDirectories is what gets you all subfolders too. Change this to not use subfolders
//Loop through allFilePaths
foreach (string filePath in allFilePaths)
{
//Ready the PNG file from the harddrive
byte[] newPngFileData;
newPngFileData = File.ReadAllBytes(filePath); //Read the PNG file's bytes. This loads the PNG file into memory.
//Create a Unity TEXTURE from PNG file
Texture2D newTexture2D = new Texture2D(2, 2); //Create a new Texture. Size doesn't matter!
newTexture2D.LoadImage(newPngFileData); //Load the PNG file into a Texture2D. PngData ---> Texture2D
//Create a Unity SPRITE from Texture
Sprite newSprite = Sprite.Create(newTexture2D, new Rect(0, 0, newTexture2D.width, newTexture2D.height), new Vector2(0,0), 1);
string spriteName = Path.GetFileNameWithoutExtension(filePath); //Get the filename of the png image //example: "C:/GameFolder/Art/heroCharacter.png" ---> "heroCharacter"
AllSprites.Add(spriteName, newSprite); //Add the Sprite to the AllSprites Dictionary. Key is the filenameWithoutExtension. Value is the Sprite.
//So if you later want to access the sprite, then you can simply write...
//AllSprites[heroCharacter]; //This will give you the heroCharacter.png sprite!
}
Debug.Log("Finished Loading Sprites! Total Sprites Loaded: " + AllSprites.Count);
}
I also suggest this function for getting the Sprite from the dictionary.
public Sprite GetSprite(string imageName)
{
//Debug.Log("Getting Sprite:'" + imageName + "'");
if (!AllSprites.ContainsKey(imageName))
{
//Trim excess Space
imageName = imageName.Trim();
//Try Again
if (!AllSprites.ContainsKey(imageName))
{
Debug.LogError("SPRITE NOT FOUND in AllSprites:[" + imageName + "]");
return AllSprites["Error"];
}
}
return AllSprites[imageName];
}