Author Topic: Adding sprites to atlases at runtime  (Read 13792 times)

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Adding sprites to atlases at runtime
« on: June 17, 2013, 07:51:35 AM »
We support silent updates to our UI, so I need the ability to add a sprite to an atlas at runtime. I've got an image downloaded into a texture, but I'm not sure how to add it to my atlas. I also need the ability to define the borders of a sliced sprite at the time of the import.

What is the best way to achieve this?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #1 on: June 17, 2013, 12:31:56 PM »
It's not really made to be done at run time.

I suggest you look into how the atlas is made in the first place. Look in UIAtlas.cs and basically anything with Atlas in the name. ;)

You can write pixels to a texture via Texture2D's SetPixels.

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #2 on: June 21, 2013, 12:20:12 AM »
Thank you for your response. I have my texture created via PackTextures (which also got me sprite coordinates), and I see how I can add sprites to an atlas, but what's the proper way to assign a texture to a newly created UIAtlas? I've tried setting atlas.spriteMaterial.mainTexture, but that didn't seem to work. Do I need to create a material and a shader first?

What's the right way to get my new texture assigned to an atlas?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #3 on: June 21, 2013, 02:58:39 AM »
Create a new material and assign it to the atlas.

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #4 on: June 21, 2013, 09:28:54 AM »
I've tried that, but I think I'm doing it wrong. here's what I've got:

  1. var testTexture = new Texture2D(1, 1);
  2. var coordinates = testTexture.PackTextures(textures.ToArray(), 0);
  3.  
  4. Material material = new Material(Shader.Find("Diffuse"));
  5. material.SetTexture("_MainText", testTexture);
  6.  
  7. var atlas = GameObject.Find("MyAtlas").GetComponent<UIAtlas>();
  8. atlas.spriteMaterial = material;
  9.  
  10. for(int i = 0; i < coordinates.Count; i++)
  11. {
  12.         var coordinate = coordinates[i];
  13.  
  14.         var sprite = new UIAtlas.Sprite();
  15.         sprite.name = names[i];
  16.         sprite.inner = coordinate;
  17.         sprite.outer = coordinate;
  18.         atlas.spriteList.Add(sprite);
  19.  
  20.         atlas.MarkAsDirty();
  21. }

Does that look right? I think I'm pretty close.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #5 on: June 21, 2013, 10:11:51 AM »
Looks fine to me. Make sure the atlas is using the proper coordinate system. Packed textures are in Tex Coords. Atlas defaults to pixels.

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #6 on: June 21, 2013, 10:23:29 AM »
I made this change:

  1. var atlas = GameObject.Find("MyAtlas").GetComponent<UIAtlas>();
  2. atlas.spriteMaterial = material;
  3. atlas.coordinates = UIAtlas.Coordinates.TexCoords;

but I get an error saying:

Quote
Can't switch coordinates until the atlas material has a valid texture

Even if I set coordinates after atlas.MarkAsDirty(), I get that error. Also, without the coordinates line, if I click the sprite button on the atlas, it says "The atlas doesn't have a texture to work with", so clearly I'm doing something wrong concerning setting the texture.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #7 on: June 21, 2013, 10:39:39 AM »
Well what texture does your material have on it? And what shader is it using?

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #8 on: June 21, 2013, 10:56:46 AM »
I'm just using the Diffuse shader, and the texture that I packed with all of my sprites:

  1. var testTexture = new Texture2D(1, 1);
  2. var coordinates = testTexture.PackTextures(textures.ToArray(), 0);
  3.  
  4. Material material = new Material(Shader.Find("Diffuse"));
  5. material.SetTexture("_MainText", testTexture);

The texture renders okay (and it looks like an atlas texture) if I just print it out to the screen.

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Adding sprites to atlases at runtime
« Reply #9 on: June 21, 2013, 10:58:59 AM »
Ha, it was right there in front of me:

  1. material.SetTexture("_MainText", testTexture);

"_MainText" should have been "_MainTex". The atlas looks good now. Thank you very much for your help!

bootalmighty

  • Guest
Re: Adding sprites to atlases at runtime
« Reply #10 on: August 02, 2013, 04:41:19 PM »
I am going through this same process to work on adding profile pictures to a game. I can follow all of this assuming we have a texture list that is called textures in this case. What steps would I take to add a texture to an existing atlas, say I have an atlas with preset default pictures and I load up a texture from the web that I want to add? From what is shown it appears as though I need to completely recreate my atlas every time, is this true?