Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Enter the 2020.2 Beta Sweepstakes for a chance to win an Oculus Quest 2.
    Dismiss Notice

Dynamically change Sprite2DSequence sprites?

Discussion in 'Project Tiny' started by Subwoof01, Sep 26, 2019.

  1. Subwoof01

    Subwoof01

    Joined:
    Sep 5, 2019
    Posts:
    1
    Is it possible to dynamically change the buffer data in the Sprite2DSequence component in order to load in sprites on runtime?

    I am trying to create a script that will splice an image into different spites and store them in a Sprite2DSequence component so I don't have to create my own animation render system.

    I currently have a SpriteSheetLoader component set up that holds the spritesheet and the columns x rows.

    Here is my OnUpdate() code, the SpriteSheetLoader component only has a single Sprite2D field.

    Code (CSharp):
    1. Entities.ForEach((Entity e, ref LoadSpriteSheet loader) =>
    2.         {
    3.             Debug.Log("Found sprite sheet loader.");
    4.             _source = e;
    5.  
    6.             Image2D image = EntityManager.GetComponentData<Image2D>(loader.sprite.image);
    7.  
    8.             float2 pivot = new float2(0.5f, 0.5f);
    9.             float2 frameSize = new float2(image.imagePixelSize.x / loader.columns, image.imagePixelSize.y / loader.rows);
    10.             //Debug.Log("frameSize " + frameSize.ToString());
    11.  
    12.             //Debug.Log((int)loader.sprite.imageRegion.height - frameSize.y);
    13.             //DynamicBuffer<Sprite2DSequence> spriteSheet = new DynamicBuffer<Sprite2DSequence>();
    14.  
    15.             for (int row = 0; row < image.imagePixelSize.y; row += (int)math.ceil(frameSize.y))
    16.             {
    17.                 for (int column = 0; column < image.imagePixelSize.x; column += (int)math.ceil(frameSize.x))
    18.                 {
    19.                     Debug.Log("row " + row);
    20.                     Debug.Log("column " + column);
    21.                     Entity newSpriteEntity = EntityManager.CreateEntity();
    22.                     Sprite2D newSprite = new Sprite2D();
    23.  
    24.                     newSprite.image = loader.sprite.image;
    25.                     newSprite.imageRegion = new Unity.Tiny.Core2D.Rect(column, row, frameSize.x + column, frameSize.y + row);
    26.                     newSprite.pivot = pivot;
    27.  
    28.  
    29.                     EntityManager.AddComponentData(newSpriteEntity, newSprite);
    30.  
    31.                     _spriteSheet.Add(newSpriteEntity);
    32.                 }
    33.             }
    34.  
    35.             Debug.Log(_spriteSheet.Count);
    36.             DynamicBuffer<Sprite2DSequence> buffer = EntityManager.GetBuffer<Sprite2DSequence>(e);
    37.  
    38.             NativeArray<Sprite2DSequence> spriteSheet = new NativeArray<Sprite2DSequence>(_spriteSheet.Count, Allocator.Temp);
    39.  
    40.             for (int i = 0; i < _spriteSheet.Count - 1; i++)
    41.             {
    42.                 Sprite2D sprite = EntityManager.GetComponentData<Sprite2D>(_spriteSheet[i]);
    43.                 Image2D newImage = EntityManager.GetComponentData<Image2D>(sprite.image);
    44.  
    45.                 Entity newImageEntity = EntityManager.CreateEntity();
    46.                 Image2D newImage2DComponent = new Image2D();
    47.  
    48.                 Entity newSpriteEntity = EntityManager.CreateEntity();
    49.                 Sprite2D newSprite2DComponent = new Sprite2D();
    50.  
    51.                 newSprite2DComponent.image = newImageEntity;
    52.                 EntityManager.SetComponentData(newSpriteEntity, newSprite2DComponent);
    53.  
    54.                 newImage2DComponent = newImage;
    55.                 EntityManager.SetComponentData(newImageEntity, newImage2DComponent);
    56.  
    57.                 //EntityManager.AddBuffer<Sprite2DSequence>(newSpriteEntity);
    58.                 //Entity entity = whyDoesThisNotWork[i].e;
    59.                 //entity = newSpriteEntity;
    60.  
    61.                 Debug.Log(newSpriteEntity.ToString());
    62.  
    63.                 Sprite2DSequence sq = new Sprite2DSequence();
    64.                 sq.e = newSpriteEntity;
    65.                 spriteSheet[i] = sq;
    66.  
    67.                 Debug.Log(spriteSheet[i].e.ToString());
    68.                 Debug.Log("buffer.Length " + buffer.Length);
    69.             }
    70.  
    71.             buffer.AddRange(spriteSheet);
    72.  
    73.             PostUpdateCommands.SetBuffer<Sprite2DSequence>(e);
    74.  
    75.             Debug.Log("buffer.Length " + buffer.Length);
    76.             PostUpdateCommands.RemoveComponent<LoadSpriteSheet>(e);
    77.  
    78.         });
    Thank you very much for your time!
     
    Last edited: Sep 26, 2019
unityunity