Loading Sprite from Sprite Sheet

I have a sprite sheet that contains the various parts of a human. The player can equip/unequip clothes, so the pieces from the basic sprite sheet will be swapped with pieces from another sheet. I need a way to load a single sprite from a sprite atlas.

According to similar questions, using Resources.LoadAll(“File Name”) is the way to go if the sprite atlas is in the Resources folder. I’ve tried this, and the Sprite Atlas always has an array size of 0.

Here is my code:

	public Texture newSkin; //This is the sprite atlas I want the object to look like.

	// Use this for initialization
	void Start () {

		//change out each child
		//	Head = sprite0

		Sprite[] newSprites = Resources.LoadAll<Sprite>(newSkin.name);
		Debug.Log ("Loaded " + newSkin.name + " with length: " + newSprites.Length);
		transform.FindChild ("Head").GetComponent<SpriteRenderer> ().sprite = newSprites[0];

The Debug prints out: Loaded Template_Human1 with length: 0

The last line returns an IndexOutOfRange exception.

  1. You should use some handlers for slots like: head, body, feets etc. it will boost performence.

  2. I found something like that:

    public Sprite GetSprite(string path, string name)
    	Object[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
    	if(sprites == null)
    		return null;
    	for(int i = 0; i < sprites.Length; i++)
    		if(sprites*.name == name)*

_ return (Sprite) sprites*;_

* return null;*
* }*
It is not optimized probably but it works for me.
Inputs example: (“Assets/Textures/SkinAtlas_0”, “Head”)