How to load a folder of textures automatically to an array of Raw Images?
edit: preferably in C#
I’m trying to load multiple images from a folder, non-specifically, so that no matter what the contents are, it automatically populates into the images in the scene.
Currently, I’m using a script that allows an image to be inserted from the inspector, via a public Texture. See Image Class script attached.
For the purposes of my app, I’d like it to take any folder and assign the images to a series of Raw Image objects within a single Canvas object.
I’ve been messing around with sort array/manager script but can’t seem to figure anything out. Does anyone have any idea for how to achieve this?
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ImageClass : MonoBehaviour {
public GameObject ImageOnPanel; ///set this in the inspector
public Texture NewTexture;
private RawImage img;
void Start () {
img = (RawImage)ImageOnPanel.GetComponent<RawImage>();
img.texture = (Texture)NewTexture;
}
// Update is called once per frame
void Update () {
}
}
Answer by xxmariofer · Feb 18, 2019 at 09:32 AM
first you need to access the folder with the textures, you can use for example application.datapath for that or whaatever folder like streaming assets or persistent data path. i will share with your a code i used once for creating textures that were downloaded at runtime, tell me if you need any explnation
for (int i = 0; i < desserts.paths.GetLength(0) - 1; i++)//iterate over the textures
{
Texture2D texture = new Texture2D(1, 1); //generates a texture with that dimension
WWW www = new WWW("file://" + exportPath + "/" + desserts.paths[i]);//i am using www since it is an android project and textures were saved into jar
while (!www.isDone)
{
yield return new WaitForSeconds(0.1f);//waits untill it is done thge download of the textre
}
www.LoadImageIntoTexture(texture); //www method for loading texture
listOfSprites.Add(Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f)); //generate a sprite and add it, you dont need this if you are only usnig textures. just added ot amy list of sprites.
}
Answer by panoramabeats · Feb 20, 2019 at 02:25 AM
Hey, Thank you so much for your answer. I'm sure this gets me on the right track. - How did you assign the variable exportPath? This is the name of the folder? Does 'file://' go to the root folder on Android? I'm assuming that's the jar or 'exportPath' is the string to the directory? maybe - sorry I'm novice at programming. I wish I could follow - An explanation would be nice but I may be too much noob anyway.
hello, go step by step, first try it going first in the editor using application.datapath. but my export path is the path to the persistentdata path. you should check whats the best option for you, persistentdatapath strea$$anonymous$$gassets datapath etc... here is how i got the exportPath value
string applicationPath;
if (Application.platform == RuntimePlatform.Android)
{
applicationPath = Application.persistentDataPath;
}
else
{
applicationPath = Application.dataPath;
}
importPath = applicationPath + "/download.zip";
exportPath = applicationPath + "/DownloadFile";
the path points to a folder called DownloadFile in the persistentDataPath in android and dataPath in windows(i did this just for testing) are you targeting android?