Author Topic: Dynamically adding sprites to UIAtlas  (Read 3679 times)

arkytoothis

  • Guest
Dynamically adding sprites to UIAtlas
« on: January 06, 2013, 02:13:58 PM »
Hey guys,
I've done some searching and couldn't really find any clear answers. What I need to do is add all of the images in a folder to a UIAtlas at run time. I need to do this because I want to make my game as modifiable as possible. I have a UIatlas that contains all of the icons for items, spells etc and I want users to be able to add new icons or change old icons which are added to buttons in the inventory screen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamically adding sprites to UIAtlas
« Reply #1 on: January 06, 2013, 02:19:11 PM »
It's more challenging to do at run-time as you can't actually save the result. I suggest you look inside the atlas maker script file to see what it does inside. Basically you will need to read all your textures (creating Texture2D from each), then using PackTextures method to create an atlas texture. This also gives you all the rectangles in an array -- rectangles are UV rects that you use to create sprites.

arkytoothis

  • Guest
Re: Dynamically adding sprites to UIAtlas
« Reply #2 on: January 06, 2013, 02:26:57 PM »
Awesome thanks for the quick reply. Ill give that a shot.

BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: Dynamically adding sprites to UIAtlas
« Reply #3 on: January 06, 2013, 03:08:02 PM »
You could keep a list of folders containing your textures to go in the atlas in some ScriptableObject. Then extend AssetPostProcessor and if the asset is a texture, and was contained in one of the folders, trigger the atlas rebuild. It's not at runtime, but should always stay up to date.

arkytoothis

  • Guest
Re: Dynamically adding sprites to UIAtlas
« Reply #4 on: January 07, 2013, 01:59:27 PM »
I solved this pretty easily by writing my own static AtlasMaker class, building a list of textures then using the AtlasMaker UpdateAtlas function and it works perfectly. As an added bonus, since Im using a pre-made atlas prefab, the sprites are properly saved. Thanks for the tips guys.

BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: Dynamically adding sprites to UIAtlas
« Reply #5 on: January 07, 2013, 02:07:38 PM »
Are you triggering the rebuild on Start()? Seems unnecessary if you can do it whenever the textures change.

arkytoothis

  • Guest
Re: Dynamically adding sprites to UIAtlas
« Reply #6 on: January 08, 2013, 12:21:02 AM »
Ya I rebuild the atlas completely when the program starts for now. I will add the ability to only add a texture if it doesn't already exist (which should be trivial) once I am positive that everything works correctly.