Search Unity

Sprite distributor

Discussion in '2D' started by Thimble2600, Oct 20, 2020.

  1. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    I've an inventory system, 500+ unique sprites.
    What's a good way to fetch a sprites?

    Currently I just cycle through a bunch of spritesheets and throw all their sprites in a dictionary like this

    Code (CSharp):
    1.         string root = "UI/Inventory/Items/";
    2.  
    3.         for ( int i = 0; i < 17; i++ )
    4.         {
    5.             Sprite[] s = Resources.LoadAll<Sprite>( root + i.ToString() );
    6.  
    7.             for ( int j = 0; j < s.Length; j++ )
    8.             {
    9.                 sprites.Add( i + ", " + j, s[j] );
    10.             }
    11.         }
    And get the information like this

    Code (CSharp):
    1. internal Sprite Get( int subcategory, int  categoryIndex  )
    2.     {
    3.         try
    4.         {
    5.             Debug.Log( string.Format( "Fetching {0}[{1}]", subcategory, v ) );
    6.             return sprites[subcategory + ", " +categoryIndex ];
    7.         }
    8.         catch ( Exception )
    9.         {
    10.             Debug.LogError( string.Format( InventoryItemRoot + "{0}[{1}] not found.", subcategory, categoryIndex ) );
    11.             throw;
    12.         }
    13.     }
    But it feels pretty impractical having 500 odd sprites loaded all the time.
     
    Last edited: Oct 20, 2020
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    instead of pre loading all use only resources load on the sprite you want to get.