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 () {
}
}
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 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.
}
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.