Search Unity

Set/change sprite (runtime) with direct path

Discussion in '2D' started by Chrisbii, Jan 6, 2015.

  1. Chrisbii

    Chrisbii

    Joined:
    Oct 28, 2014
    Posts:
    9
    Hi.

    I have an atlas with a lot of sprites. If I could avoid any publics it would be nice.
    I'am referring to the sprites names in my game. Can I somehow set the sprites with a path like "Textures/Atlas/AtlasDir/SpriteName"


    Thanks
     
  2. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    137
    You can create a folder called Resources (must be called Resources) and put your graphics in sub-folders there if you like. I typically will create a Resources folder with GFX/Sprites sub-directory. Move your spritesheet to the appropriate directory in Resources. I assume you have imported your sprite sheet as a multi sprite and sliced them yes? If so, then the solution is simple to access them in game.

    Code (CSharp):
    1. Sprite[] sprites = Resources.LoadAll<Sprite>(@"GFX/Sprites/spriteSheetName);
    Each of your sprites in that sprite sheet will be an index in the sprites array.
     
    Mikenseer likes this.