Search Unity

Spritesheet in runtime

Discussion in '2D' started by alexandros356, Oct 24, 2016.

  1. alexandros356

    alexandros356

    Joined:
    Dec 3, 2012
    Posts:
    105
    Hi guys,

    I have the app's spritesheet in the cloud and the app download it on demand. But, I don't know how to create the sprites programatically. I have the png file(the spritesheet) and the plist file (definition of the sprites) and I can't find how to use it. Or maybe there is something else?

    And then I have to create a 2d animation based on the sprites. Is it possible?

    Any help?
    Thanks!
     
    Last edited: Oct 24, 2016
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    You can split up a sprite sheet, but it will need to be a texture read/write, now unity can produce a second data file(atlas I think), but you will need plus or pro licence for that I beleive, how often are you getting the file for the internet, and why?
     
  3. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
    I'm running into the same issue: my images are generated from a Camera stream and saved on disk. I'm trying to create a SpriteSheet once I got the images (they're post processed by some OpenCV script) and then save it (so it won't be generated again).

    Is there any script (paid or not) to do that?

    @piggybank1974 you mention a plus or pro licence for the atlas. Could you give more informations?
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Can you use an asset bundle for your sprites? These can be downloaded or read from the filesystem.

    https://unity3d.com/learn/tutorials/topics/best-practices/guide-assetbundles-and-resources

    If your sprite sheet isn't dynamic, it is probably easier to create the sprites and animations in Unity and then export them as an asset bundle to be downloaded once needed.

    If your sprite sheet is dynamic or you don't want to use bundles, then download it to your resource folder and use resource load to load it into Unity: https://docs.unity3d.com/ScriptReference/Resources.Load.html. Then build the sprites using: https://docs.unity3d.com/ScriptReference/Sprite.Create.html.

    This is a pretty good resource that I used when I did something similar: http://answers.unity3d.com/questions/1080430/create-animation-clip-from-sprites-programmaticall.html

    And, no, you don't need Unity Pro or Plus.
     
  5. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
    Thanks for the quick answer @PGJ!

    Well my Unity project is almost a blank project (regarding images), they are grabbed at runtime from the Camera and OpenCV (still a Unity process) then saved on disk. The idea is to turn this images into SpriteSheets at runtime and save them on disk so later they won't be regenerated. Since everything is dynamic and Unity Editor isn't running, I think I can't use AssetBundle, right?

    Sprite.Create sounds great to create a Sprite with Sprite Mode set to Single, but not for Multiple or I'm missing something?
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You don't really need to care for Single/Multiple in this case. Do something like this:
    Code (CSharp):
    1.  
    2.        int width = 5;
    3.        int height = 5;
    4.  
    5.         Texture2D mySpriteSheet = Resources.Load("mySpriteSheet") as Texture2D;
    6.  
    7.         List<Sprite> mySprites = new List<Sprite>();
    8.  
    9.        for (int y = 0; y < 5; y++)
    10.        {
    11.             for (int x = 0; x < 5; x++)
    12.             {
    13.                 Sprite newSprite = Sprite.Create(
    14.                     mySpriteSheet,
    15.                     new Rect(x * width, y * height, width, height),
    16.                     new Vector2(width / 2, height / 2));
    17.  
    18.                 mySprites.Add(newSprite);
    19.             }
    20.         }
    21.  
    I haven't really tested the code above, but it should be more or less correct.
     
  7. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
    Ok I see. So yeah, I must develop a packing algorithm from Texture2D.
     
  8. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
  9. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621