Hi I want to load all images of a folder into a Texture2D array. And the folder shoud be selectable from the inspector. Is this possible? When yes, how? An example would be helpfull. Thanks realm_1
Ok Thanks I have about 300 images and I want to load them in sequence into a gui but when I have to make for each image a variable then its very complex. Do you have an idea to do that in a easy way?
I believe this is what dreamora was saying is impossible (without writing a custom inspector). However, you can use a string to do it if you really want... Code (csharp): public class ExampleLoader { public string _filePath = ""; void Awake () { Object[] textures = Resources.LoadAll(_filePath, typeof(Texture2D)); } } Obviously you would want to do some sort of validation to ensure _filePath is a valid file path. If your project looks like this... Assets/Resources/AwesomeImages/DesiredImages/ You would want _filePath to = "AwesomeImages/DesiredImages" They *have* to be in the Resources folder though.
Many thanks. But it stills one problem: I added your script to my project and it gives my an array out of range exception out. Do you know why? Heres my code I use (I've modified it a little bit): var _filePath : String = ""; var e = 1; var textures : Object[]; function Update () { } function Awake() { textures = Resources.LoadAll(_filePath, typeof(Texture2D)); } function OnGUI() { GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),textures[e],ScaleMode.StretchToFill,true,0); e++; if(e >= textures.length-1) { e = 1; } }
are you testing with one file by chance? textures[e] and you start e = 1, arrays start at 0 so if your testing with one file, textures[1] wouldnt exist - even if thats not it, tagging this post cause i wanna know how you fix it What does this show: Debug.Log(textures.length)
It doesnt work even if I start with e = 0. Debug.Log(textures.length) gives me 0 out, but there are 20 images. I set this variable to: var _filePath : String = "Resources/test"; In the project folder I created a folder Resources and in it a folder test. I hope this is correct. so myproject/Assets/Resources/test and there are the images.
Code (csharp): using UnityEngine; using System.Collections; public class TextureTest : MonoBehaviour { public string _imagePath; Texture2D[] _typedTextures; int _displayed = 0; // Use this for initialization void Start () { Object[] textures = Resources.LoadAll(_imagePath, typeof(Texture2D)); _typedTextures = new Texture2D[textures.Length]; for (int i = 0; i < textures.Length; i++){ _typedTextures[i] = (Texture2D)textures[i]; } Debug.Log("Textures Loaded: " + _typedTextures.Length); } // Update is called once per frame void Update () { if (_typedTextures != null _typedTextures.Length > 0){ _displayed++; if (_displayed >= _typedTextures.Length){ _displayed = 0; } } } void OnGUI(){ if (_typedTextures != null _typedTextures.Length > 0){ GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height), _typedTextures[_displayed], ScaleMode.StretchToFill, true); } } } // This code is a bit of a mess, I know >.> I just tested that and it worked fine. You definitely don't want _filePath to == Assets/Resources/test - in that case you would just want it to == test. In my test all 5 textures were in Assets/Resources/ImageTest/Examples/ and my _imagePath variable was set to "ImageTest/Examples" Do arrays in JS start at 1 instead of 0? I think in JS you would do Resources.LoadAll(_filePath, Texture2D) instead of typeof(Texture2D). Also, make sure you rotate the images in Update and not in OnGUI - OnGUI gets called multiple times per frame - you probably want to rotate them once per frame.
Yeah thats really amazing! It works fine. Many many thanks. I think that arrays in Java also starts with 0.
But I have one last question: its require to do somthing special with the selected folder with the images so that it embblemed in the build?
Nope, that's why it has to be in that specific Assets/Resources folder. Everything in Assets/Resources is included with the build, and accessible via Resources.Load/LoadAll in the final build. If targeting web or iOS in particular, it's pretty important to make sure you only include things in the resource folder you need to have there for that reason.
this one is working: private var textures:Object[]; function Start () { textures=Resources.LoadAll("directory") as Object[]; } thanks.
cenk5355 how do you use an Object? I can't use it as a texture2d[] array. KyleStaves how do I assign a path? I don't use c# can I make the resulting texture2d[] array static so I can call it from other scripts? This all looks nice, but I can't see a way to use any of it. I'm trying to load user created textures at runtime in JavaScript. my build can create and edit files in the Application.dataPath but I haven't got jpg images working yet and I want to handle a few of them in an array.