Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Unity 2D Loading Sprite Atlas from Web and Using it at runtime / dynamically

Discussion in '2D' started by nkhajanchi.mg14, Mar 14, 2014.

  1. nkhajanchi.mg14

    nkhajanchi.mg14

    Joined:
    Mar 6, 2014
    Posts:
    13
    Hi,

    I am new to Unity (came from AS3/Starling) and just started for 2D games in Unity. I have a sprite atlas (many different sized images packed in a single png) which I want to download at runtime (I can use WWW classes for that) in game .

    Than use all those images (in that sprite atlas) and show them up on screen and use them as regular unity sprites, like applying Rigidbody2D or Collider2D etc. I googled for this but mainly what I found is using Unity3D's editor, importing the sprite atlas and using its own sprite editor. Not much on how I would do that in runtime. I am expecting something like what we used to do in Flash or evening in Cocos2d, like we have a related XML file for each sprite atlas and their SDK have functions for reading that sprite in conjunction with that XML.

    Any help would be highly appreciated.
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    I think I know how to do it but first I'll as you: Why would you want to load a sprite atlas from an external image?
     
  3. nkhajanchi.mg14

    nkhajanchi.mg14

    Joined:
    Mar 6, 2014
    Posts:
    13
    @DSSiegeEleven: Thanks for your reply. I want to load a sprite atlas externally because that is how my game is and it is very much dependant on this thing only.
    Waiting for your reply.
     
  4. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    Alright. As you said, you use the WWW class to load an image into unity. When you have this image as a Texture2D you can use the following function to create a new sprite:

    https://docs.unity3d.com/Documentation/ScriptReference/Sprite.Create.html

    Code (csharp):
    1.  
    2. Sprite sprite = Sprite.Create(Texture2D, Rect, Vector2, float)
    3.  
    Calling this function will creature a new sprite from the chosen Texture2D atlas, the Rect is the specific area on the atlas you want the sprite to be, the Vector2 is the pivot point of the sprite and the float is the conversion of pixels to units (how big the sprite appears on screen, should be the same for all sprites).

    You can load a series of sprites this way, all with the same Texture2D but with a different Rect (you can then write a system that reads the value for the Rect from an XML file).

    I hope that helps you a little bit further :) Feel free to come back if you need help with actually putting the sprites on screen (making a new GameObject, adding a SpriteRenderer component and assigning a sprite to it) ;)
     
  5. nkhajanchi.mg14

    nkhajanchi.mg14

    Joined:
    Mar 6, 2014
    Posts:
    13
    Thanks for your reply, I will surely try this tonight.
     
  6. nkhajanchi.mg14

    nkhajanchi.mg14

    Joined:
    Mar 6, 2014
    Posts:
    13
    Hi DSSiege11,

    I tried that and it really helped. Got my stuff working.

    Thanks.