Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading a Dynamic Sprite and Animating it...

Discussion in '2D' started by BrienKing, Mar 7, 2020.

  1. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    So with my game, I want to make it modable by the end users, so with that in mind I'm putting all the graphics, etc... as files on disk.

    I can load a single sprite easy enough and assign it to a game object.

    Code (CSharp):
    1.            
    2.             fileData = System.IO.File.ReadAllBytes(fileName);
    3.  
    4.             Image = new Texture2D(ImageWidth, ImageHeight, TextureFormat.BGRA32, false);
    5.             Image.LoadImage(fileData);
    6.  
    I then later create the sprite on the game object as:

    Code (CSharp):
    1.  
    2.             renderer = gameObject.GetComponent<SpriteRenderer>();
    3.  
    4.             renderer.sprite = Sprite.Create(Image, rect, new Vector2(0.5f, 0.5f));
    5.  
    This issue I'm trying to overcome is if I have an image that contains multiple cells of an animated sprite.

    So I have an image that has a 32 x 32 sprite. The Image might be 128 x 32 which would give me 4 frames of animation for the 32 x 32 sprite.

    Basically, I would have an animation speed on my class, what frame it should be currently showing and a reference to the large image with all the frames.

    Do I manually load the full image in, then create multiple images as a collection, or is there a more efficient way to do it in Unity with the single image?

    Thanks!
     
  2. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    No one is loading sprites from disk and animating them?