• Unity
  • Changing material at runtime

Greetings,

Can someone advise me on how I can change materials at runtime?

I've added the define I found on github:

#define SPINE_OPTIONAL_MATERIALOVERRIDE

Code that runs at runtime

var meshRenderer = skeletonAnimation.GetComponent<MeshRenderer>();
originalMaterialInstance = meshRenderer.material;
slowDownMaterialInstance.SetTexture( "_MainTex", originalMaterialInstance.GetTexture( "_MainTex" ) );
skeletonAnimation.CustomMaterialOverride.Add( originalMaterialInstance, slowDownMaterialInstance );

Nothing seems to change and I am a bit stumped

Related Discussions
...
  • Изменено

Check out the MaterialReplacementExample.cs that is included with the spine-unity runtime. It shows how to set the material as well as apply values from a Material Property Block :yes:

Also - you shouldn't need that #define. I've never heard of that one though, so I'm not sure the context.

Ah, I don't seem to have that .cs file. We are using version 3.8, perhaps this was introduced in a newer version?

WiseKodama написал

Ah, I don't seem to have that .cs file. We are using version 3.8, perhaps this was introduced in a newer version?

Possibly yeah, maybe the example was added later. You can download the newest runtime and just grab the script out of it and see if it works - I feel pretty confident that it'd still work with 3.8.

That did the trick. Thanks so much!

WiseKodama написал

That did the trick. Thanks so much!

:cooldoge:

Glad you've figured it out, thanks very much Jamez0r for helping out! 8)

Can someone advise me on how I can change materials at runtime?

The first place to look should always be the spine-unity documentation pages:
spine-unity Runtime Documentation: Materials
There is also an FAQ section for frequent questions.

Ah, I don't seem to have that .cs file. We are using version 3.8, perhaps this was introduced in a newer version?

Actually it was present in 3.7 already:
https://github.com/EsotericSoftware/spine-runtimes/blob/3.7/spine-unity/Assets/Spine%20Examples/Scripts/MaterialReplacementExample.cs
Perhaps you didn't import everything from the Spine Examples directory of the unitypackage.

I've added the define I found on github:

You don't need to set any defines. If that were necessary, it would be mentioned on the spine-unity documentation page. 🙂