Hello Guest

Author Topic: Changing material at run time.  (Read 2364 times)

castor

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Changing material at run time.
« on: October 14, 2014, 07:47:17 am »
I want to change material used by a sprite to the other one during run time.

While searching for the solution I have came across this topic :

http://2dtoolkit.com/forum/index.php?topic=356.0

It basically has a bit of work around but it doesn't cover the basic feature of the tool.

Situation :

I have sprite collection with couple of materials on them. Meaning, that some sprite uses material_A and others uses material_B etc..
The above material settings are done in the sprite collection editor.

Now during the run time I want to change the material of specific sprite to the one I want , causing them to become local and just modify it without affecting the other sprites.

The above link solution does work for changing the run time case, but that causes the other usual assignment of the different materials by the editor not working anymore for the existing sprite prefabs.

This , ofcourse is because, the work around code is ignoring the change and only allocates one when a sprite is being created. So for the ones that are already created, it can't change.

Is there any other updated workaround for this issue?

Thanks. :o

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Changing material at run time.
« Reply #1 on: October 14, 2014, 09:39:47 am »
That is a basic feature of the tool, allowing different shaders and materials within the same sprite collection. If you want to change material properties on the instance, use the MaterialPropertyBlock on the sprites renderer. This is the most common use case and is supported directly in unity - it only works in runtime though.

If you want to change materials altogether, you'll have to use the workaround, or make tk2d not change the material once its set - you can do that if you know that you'll not require a material change subsequently. Also, if you only have 2 materials, why not just create 2 copies of the sprites in the sprite collection using an editor script - that will make sure everything works properly.

castor

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Changing material at run time.
« Reply #2 on: October 14, 2014, 10:56:49 am »
Alright, I need to change material's some custom float property on instance so anything that affects collections is not a good method to use.

Good news is that I have come up with the work around, based on the topic I have posted above.

Instead of making bool ignoreChangeMaterial as static global stuff, I made it locally and exposed in the editor by modifying the editor script itself.

So when I check the ignoreChangeMaterial on the sprite instance, it only affects that particular sprite and not the others. And when I change material on that sprite, it makes the material local instance.

It just means if I update the 2dtoolkit in the future , I may forget about it and will need to make the changes again.

I wonder if this is worth while little addition to the sprite base class however. ( a little check box indicating if this sprite should ignore Material Change Update )

What do you think?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Changing material at run time.
« Reply #3 on: October 14, 2014, 11:54:19 am »
If you need to change a custom float property, use the material property block, thats what its there for and you dont need to modify the material.