Reputation: 31
Is it possible in Unity 3D using C# to create an array to load sprites randomly from a folder rather than from a sprite sheet? If it is, what code do I use to refer to the folder? From what I can find, sprites are usually coded to load using random.range with an array using a sprite sheet instead of actually accessing the folder. The only thing even similar to this that I have been able to find is here:
http://docs.unity3d.com/ScriptReference/Resources.html
but as you can see with this you can only load from a folder titled "Resources" in the "Assets" folder, and possibly I'm mistaken but it would also seem that this can only be done with a game object. (?)
Upvotes: 2
Views: 7068
Reputation: 31
Here is what I ended up using:
Sprite[] enemySprites = Resources.LoadAll("Sprites/Enemies");
Upvotes: 1
Reputation: 10701
You are looking at the right docs.
A sprite is a GameObject, just a more specific one meant to be used in 2d games. So you would create your sprites and make prefab of them. Those prefabs go in the Resources folder and here is the code:
GameObject [] objs = (GameObject[])Resources.LoadAll("SpriteFolder");
GameObject randomSprite = objs[Random.Range(0, objs.Length)];
Upvotes: 4