2
\$\begingroup\$

We are creating a 2D top down rpg, and we have multiple textures, and we need to be able to change which sprite sheet its reading from in order to change the look of the walls and floor in each room. It has the same basic tiles just they need to look different. Like the corner of a cabin, to the corner of an office building.

At the moment we are taking the sprite sheet, splitting it up, and making prefabs out of it for placement by script in the game. Is there a way to do this automatically because we have a lot of textures, and its too many to make into prefabs.

\$\endgroup\$
3
\$\begingroup\$

If the sprites are in your Assets/Resources directory, you can load them programmatically as follows:

Sprite[] spriteSheetSprites = Resources.LoadAll<Sprite>("spriteSheetName);

This will yield a Sprite array containing all of the sprites from your spritesheet, indexed by their order on the sheet.

A requirement to use this is that the image's import settings must be set to Sprite (2D and UI) and Multiple. The other settings should not matter. Also, the sprites must be somewhere inside of your Assets/Resources directory, though they can be in a subfolder. To access/load sprites in a subfolder, just append the name of the folder to the beginning of the string, as follows (this assumes that your tiles are in the Assets/Resources/Tiles directory:

Sprite[] roadSprites = Resources.LoadAll<Sprite>("Tiles/roadSpriteSheet");

Note that loading resources like this is quite heavy on hardware usage, so do not do this many times in your game. A way around this is to have a static SpriteManager class, or similar, that keeps references to sprites inside of itself, which classes can request, so that each sprite is loaded a maximum of once.

static class SpriteManager 
{
    private Sprite[] _officeSprites;
    static SpriteManager {}
    public Sprite[] OfficeSprites 
    {
        if(_officeSprites == null)
            _officeSprites = Resources.LoadAll<Sprite>("Tiles/officeSpriteSheet");
        return _officeSprites;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Is there a way to set sprites default import settings to multiple rather than single? \$\endgroup\$ – ThatsATen Aug 27 '15 at 14:45
  • \$\begingroup\$ @ThatsATen As far as I know, there is unfortunately not by default. However, you should be able to write a short editor script to achieve the same goal. An example can be found here: forum.unity3d.com/threads/…, though that one regards models. \$\endgroup\$ – Pip Aug 27 '15 at 14:53
  • \$\begingroup\$ Well we are trying to make a game where a texture can be added by the user the same way a retexture works. Only its rooms specific, allowing the user to add their own rooms to the game. Would the editor script still work after the game is built and distributed? \$\endgroup\$ – ThatsATen Aug 27 '15 at 15:02
  • \$\begingroup\$ @ThatsATen it would not, since the editor script only works... in the editor. However, you could probably implement something to load sprites manually from file. I've never done this before, but maybe you want to ask another question either here or on Unity Answers. \$\endgroup\$ – Pip Aug 27 '15 at 15:43
0
\$\begingroup\$

pick your instantiated prefab, say

GameObject go = GameObject.Instantiate(yourPrefab) as GameObject;

and get for example a sprite

Sprite yourSprite = Resources.Load("sprite" typeOf(Sprite));

and change the prite

go.GetComponent<SpriteRenderer>().sprite = yourSprite;
\$\endgroup\$
0
\$\begingroup\$

In my project, I create ScriptableObject to store the sprite, and load the created asset at runtime, it quite convenient than prefab.

AssetDatabase.LoadAllAssetsAtPath("pathToPngfile")

This will load all the sprite at the specific path

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.