2D Toolkit Forum

2D Toolkit => Support => Topic started by: gabriel on September 16, 2015, 04:15:35 am

Title: managing instanced materials
Post by: gabriel on September 16, 2015, 04:15:35 am
Code: [Select]
private void setBrightness(tk2dSpriteCollection coll, float brightness_value) { //on the source material
tk2dSpriteDefinition spriteDef = coll.spriteCollection.FirstValidDefinition;
if (spriteDef != null) {
Material sharedMat = spriteDef.materialInst;
sharedMat.SetFloat("_Brightness", brightness_value);
}
}

It works correctly in the editor but there is some unspecified crash when I try it on iOS. I'm suspecting a precise SpriteCollection of causing it simply by being referred to in a public variable (my setBrightness() is not called initially). Any thought of where to look?

The problematic sprite collection also have difficulty to get its own Atlas Width/Height from inside the Sprite collection UI. I wonder if there is some kind of corruption here.
Title: Re: managing instanced materials
Post by: gabriel on September 17, 2015, 09:00:21 pm
I figured out most of these answers.
"atlas0 material" is the way to go, and there is no difference with animation or tilemap.
also, my problem was derived from the use of tk2dSpriteCollection instead of tk2dSpriteCollectionData. In using the latter, everything works both in the editor and on iOS. I still do not understand, however, the fact that caching a reference to a tk2dSpriteCollection was provoking a silent crash in a case and worked fine in the other...

Lastly, I would still be interested in knowing if looking up the coll.spriteCollection.FirstValidDefinition is the correct and safe way to go to catch the shared material. What should be done if no tk2dSpriteDefinition exist at that time? should the atlas0 be modified to ensure that generated sprites will have the correct parameter?
Title: Re: managing instanced materials
Post by: unikronsoftware on September 17, 2015, 11:18:07 pm
Hi,

"atlas0 material" is what you need ,and yes you should link tk2dSpriteCollectionData.
tk2dSpriteCollection will include all the additional editor data even if it worked, so avoid that at all costs.

tk2dSpriteCollectionData.FirstValidDefinition will give you the first sprite. You can also get tk2dSpriteCOllectionData.materials / materialInsts, but if you have only one material in a collection FirstValidDefition will work fine.
Title: Re: managing instanced materials
Post by: gabriel on September 18, 2015, 01:19:03 am
Ok thank you.
Thanks also for the great asset!