• Unity
  • Set float on material doesn't affect my spine object?

  • Изменено

I'm using a simple custom shader to tint a spine model. Then I use the code below to set a property of it.
It works for SpriteRenderers, but not on the MeshRenderer that spine uses.

The shader inspector says "MaterialPropertyBlock is used to modify these values", on the SpriteRenderer. Not sure if that makes any difference. Or isn't this supported? If so, how can I change the shader on the Spine object?

private static readonly int _weight = Shader.PropertyToID("_weight");
// ...

// Doesn't change the material
meshRenderer.material.SetFloat(_weight,1);

// Changes the sprite renderer material
spriteRenderer.material.SetFloat(_weight,1);

However, when i change the weight parameter of the shader's inspector on the object when the game is running, it changes.

Any ideas what's wrong here?


I found this code linked from a forum post about this topic:
https://gist.github.com/pharan/7a06e2a5239f3893d79b8852c249642f

And tried this code:

block = new MaterialPropertyBlock();
meshRenderer.SetPropertyBlock(block);
block.SetFloat(_weight,1);
Debug.Log("block.GetFloat = " + block.GetFloat(_weight));

The debug outputs 1, but the image and the shader still remains unchanged..

Also the shader on the spine meshrenderer doesn't say "MaterialPropertyBlock is used to modify these values" like it does on SpriteRenderer which seem to indicate i'm doing something wrong...

nicmar написал

And tried this code:

block = new MaterialPropertyBlock();
meshRenderer.SetPropertyBlock(block);
block.SetFloat(_weight,1);
Debug.Log("block.GetFloat = " + block.GetFloat(_weight));

The debug outputs 1, but the image and the shader still remains unchanged..

You are applying the property block, and then modify the float value after the (default unchanged) block was applied. Please note that order matters here 🙂. Also you typically re-use your property block as shown in the MaterialPropertyBlockExample script in example scene Per Instance Material Properties.

Thanks a lot, that worked of course! 🙂

Glad to hear, thanks for letting us know! 🙂