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.