- Home /
Using Resources.LoadAll with arrays
I seem to be having problems with loading a folder of images into an array. Here is the related code:
using UnityEngine;
using System.Collections;
public class AnimateImageOnPlane : MonoBehaviour
{
private Object[] frames;
private float framesPerSecond = 30.0f;
private Texture holder;
void Start()
{
frames = Resources.LoadAll("Textures/Social", typeof(Texture));
Debug.Log("Frame Length: " + frames.Length);
}
void Update ()
{
if(frames.Length > 0)
{
int index = (int)(Time.time * framesPerSecond);
index = index % frames.Length;
if(index < frames.Length)
{
holder = frames[index] as Texture;
gameObject.renderer.materials[0].mainTexture = holder;
}
}
}
/sigh
Sorry - First post, forgot to output error. Initial Error: array length of 0 every time.
Answer to that is:
I needed to change my folder Hierarchy in unity, ie: Assets/Resources/Textures/Social I had them in Assets/Textures/Social. The documentation seemed implicit on the directory structure.
I converted your answer to a comment. On UA Answer means Solution and not Reply. There's a Add New Comment button hidden on the right of the screen. Also your comments do not need moderation.
Ah - Thank you. I was confused (and slightly frustrated) by the moderation on comment.
Answer by whydoidoit · Jul 20, 2012 at 06:29 PM
I think your problem is that you need to specify the type as Texture2D rather than texture.
By the way you could auto cast the returned array:
using System.Linq;
...
Texture2D myArray = Resources.LoadAll("some path", typeof(Texture2D)).Cast<Texture2D>().ToArray();
Your answer
Follow this Question
Related Questions
Resources.LoadAll(); GameObject[] Array 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
if a string has certain letter(s) or number(s) 2 Answers
How to display a list of methods and allow a choice of one of them 1 Answer