Hello Guest

Author Topic: managing instanced materials  (Read 4704 times)

gabriel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Feudal Feud
managing instanced materials
« on: September 16, 2015, 04:15:35 am »
    I have a problem and a few questions on materials and sprite collections

    • What is the proper way to assign a custom shader to all sprites from a tk2dSpriteCollection? Is modifying "atlas0 material" in Data folders the correct solution?
    • If I want to modify a value on this material at runtime, what would be the way to go? (I do want to modify ALL the referenced sprites as efficiently as possible)
    • Is refering to sprite collections by a "public List<tk2dSpriteCollection>" correct?
    • Finally, is anything should be done differently about tilemaps and animations?

    This is the way i currently try to do it:

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.

gabriel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Feudal Feud
Re: managing instanced materials
« Reply #1 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: managing instanced materials
« Reply #2 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.

gabriel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Feudal Feud
Re: managing instanced materials
« Reply #3 on: September 18, 2015, 01:19:03 am »
Ok thank you.
Thanks also for the great asset!