ISpriteProvider?

So I’ve been trying for a few hours trying to programmatically add a sprite to my scene. Below is my code. I’ve left my comments and failed attempts in the code (commented).

I’m thinking I need to make some object that has an ISpriteProvider and set my SpriteComponent.SpriteProvider to that, but at this point I don’t know anymore.

//Get the scene
Scene scene = SceneSystem.SceneInstance.RootScene;
			
//Make a new entity
Entity entity = new Entity();
//entity's transform component
TransformComponent transform = new TransformComponent();
//entity's sprite component
SpriteComponent sprite = new SpriteComponent();
//enable sprite component
sprite.Enabled = true;

//set source?? .. doesn't work...
//sprite.source = Game.Content.Get<SpriteSheet>("/SpriteTest/SpriteSheet");

//in editor it uses SpriteSheet or Texture for the source... let's try that
Texture texture = Game.Content.Load<Texture>("SpriteTest/testSprites");

//can't set source in class; maybe it's in the constructor? doesn't work either...
//sprite = new SpriteComponent(texture);

//background has almost the same selection box so let's try to make one of those. (just can't change the source class)
BackgroundComponent background = new BackgroundComponent(); //no constructors.
background.Texture = texture; //but it does have a texture property.

//maybe I can set Texture property of sprite? nope...
//sprite.Texture = texture;

//so this means my problem is with the selection of SpriteSheet or Texture it seems.
ISpriteProvider provider = sprite.SpriteProvider; //maybe the sprite provider? I need to make a new one...
			
//cant just make a new one...
//provider = new ISpriteProvider();

//maybe get an ISpriteProvider from a Texture or a SpriteSheet.. or Sprite? nope.. 
SpriteSheet spriteSheet = Game.Content.Get<SpriteSheet>("/SpriteTest/SpriteSheet");
//provider = spriteSheet.SpriteProvider;
//provider = texture.SpriteProvider;
Sprite mySprite = new Sprite();
//provider = mySprite.SpriteProvider;

//add components to entity
entity.Add(transform);
entity.Add(sprite);

//add entity to scene
scene.Entities.Add(entity);

return;

Sadly this was asked before, but they just said ‘samples’. I’ve looked at the documentation but apparently I’m not as good at reading documentation. It would be nice to get an answer on the forum.

In the GUI I added an entity into my scene then assigned it a SpriteComponent and added a sprite etc.

Then I put the scene’s entities into a variable var plzWork = scene.Entities; then I made a break point. When I started debugging I looked through the entities and found the entity with the sprite component I made.

It looks like ISpriteProvider comes from a class called SpriteFromSheet; or it’s counterpart SpriteFromTexture.

These two classes are found in the API.

This the bare minimum to add sprites from code (in a script):

var spriteFromTexture = new Entity()
{
    new SpriteComponent
    {
        SpriteProvider =  new SpriteFromTexture
        {
            Texture = Content.Load<Texture>("SpritesTexture"),
        },
    }
};

//change transform if you like
spriteFromTexture.Transform.Position = new Vector3(0, 0, 0);

SceneSystem.SceneInstance.RootScene.Entities.Add(spriteFromTexture);

var spriteFromSpriteSheet = new Entity()
{
    new SpriteComponent
    {
        SpriteProvider = new SpriteFromSheet
        {
            Sheet =  Content.Load<SpriteSheet>("SpriteSheet")
        }
    }
};

//change transform if you like
spriteFromSpriteSheet.Transform.Position = new Vector3(0, 0, 0);

SceneSystem.SceneInstance.RootScene.Entities.Add(spriteFromSpriteSheet);

If you are not using assets in scenes make sure to mark them as “Include in build as root asset”.