Ive set up my game to be able to change a texture by inputing a file path. How can I make it so that if i put a folder as a file path all the images in that folder are stored in an array of textures?
Here is:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class MyClass : InGameScriptCS
{
Texture[] myTexs = new Texture[0];
void Start ()
{
myTexs = GetTexturesInFolder("/Users/myuser/Pictures");
}
Texture[] GetTexturesInFolder (string path)
{
List<Texture> textures = new List<Texture>();
List<string> texExtensions = new List<string>() { ".png", ".jpg", ".jpeg", ".psd", ".tiff", ".dds" };
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
// Skip non-texture files
if (!texExtensions.Contains(Path.GetExtension(file)))
continue;
try
{
Texture2D tex = new Texture2D(1,1);
tex.LoadImage(File.ReadAllBytes(file));
textures.Add(tex);
}
catch
{
}
}
return textures.ToArray();
}
void OnGUI()
{
if (myTexs.Length > 0)
GUI.DrawTexture(new Rect(0, 0, myTexs[0].width, myTexs[0].height), myTexs[0]);
}
}