Adam's Lair Forum

game development and casual madness
It is currently 2018/10/16, 23:39

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2018/06/06, 15:29 
Novice Member
Novice Member

Joined: 2017/08/13, 16:10
Posts: 12
Role: Hobbyist
When I instantiate a prefab I wish to populate its SpriteRenderer component with a sprite from a png-file, not from a resource. I want to be able to write something like this:

Code:
public void PlayCardFromHand()
{
  GameObject card= CardPrefab.Res.Instantiate(transform.Pos, transform.Angle);
  card.GetComponent<SpriteRenderer>().Sprite = Load(@"\path\to\cardimage.png");
}

Is something like this possible?

I'm adapting a card game to Duality, and have over 1000 card images, so having all the cards as resources is not a viable solution.


Top
 Profile  
 
PostPosted: 2018/06/06, 19:45 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 880
Location: Italy
Role: Hobbyist
Of course it's possible :D

there you go

Code:
using (Stream s = FileOp.Open(@"Y:\our\Path\Here.png", FileAccessMode.Read))
{
  IImageCodec codec = ImageCodec.GetRead(ImageCodec.FormatPng);
  PixelData pdata = codec.Read(s);
  Pixmap pmap = new Pixmap(pdata);
  Texture tex = new Texture(pmap /* extra parameters */);
  Material mat = new Material(DrawTechnique.Mask, ColorRgba.White, tex);
}


Please keep in mind that you can only load Textures while in the main thread loop of the game, otherwise you'll get a memory access exception. No shenaningans in the InitPlugin or separate threads.

EDIT: with this I mean that only the
Code:
Texture tex = new Texture(pmap /* extra parameters */);

line (and possibly the Material.. forgot) has to be performed in the main thread, due to OpenGL having to load the texture data in memory; the actual reading of the stream (the expensive part of the process), up to the instantiation of the Pixmap can very well be done in a separate thread.

Good luck :mrgreen:

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2018/06/07, 11:53 
Novice Member
Novice Member

Joined: 2017/08/13, 16:10
Posts: 12
Role: Hobbyist
Whoa, thank you sir! I've tried it and it works perfect. :1+:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group