IndieDoroid

Hi guys,

I'm trying to do a quick material swap on my character at runtime.

It works for certain animations, but other animations it doesn't work. (Same rig, same spine skeletonanimator) So I don't think there is something wrong with the code.

I can see the material swapping in the inspector, but it swaps back to the original material almost instantly.

For example
Animation 1(works) - Swap material A with material B and back to material A after "x" time.
Animation 2(does not work) - Swap material A with material B, but instantly back to material A . I don't see material B at runtime.
public IEnumerator SwapMaterial()
{
_gameObj.GetComponent<Renderer>().material = _gameObjB.GetComponent<Renderer>().sharedMaterial;
yield return _delayHalfSec;
_gameObj.GetComponent<Renderer>().material = _gameObjA.GetComponent<Renderer>().sharedMaterial;
}
Also I notice that there's 2 materials on my skeletonanimator, but when I look at the atlas there's only 1 material.

Any help would be appreciated. :sweat:
Adjuntos
Atlas.png
Inspector.png
Avatar de Usuario
IndieDoroid
Mensajes: 27

Pharan

You probably want the CustomMaterialOverride API.

Replace your code with this:
skeletonAnimator.CustomMaterialOverride[originalMaterial] = newMaterial; // to enable the replacement.
// delay
skeletonAnimator.CustomMaterialOverride.Remove(originalMaterial); // to disable that replacement.
CustomMaterialOverride is a Dictionary<Material, Material>.

You can find more info in the docs: https://github.com/pharan/spine-unity-docs/blob/master/Rendering.md#custommaterialoverride-and-customslotmaterial
Avatar de Usuario
Pharan

Pharan
Mensajes: 5220

IndieDoroid

EDIT: I ended up using an array of materials .. so I managed to swap out both materials. But it's just strange that there's 2, thats all. :) Thanks again

Hi Pharan,

That did the trick! Thanks allot! 8)

One other thing, I still have the other problem of 2 materials.

The atlas only shows one, but there's 2 on the actual game object. You can see that the "0" slot material went back to normal, but the 2nd didn't.

Do you have any idea on whats creating the 2nd material? I hid it to test it out and it hid parts of the Spine mesh. (Arms and hair)

Thanks
Adjuntos
Screenshot - 6_9_2018 , 3_41_52 AM.png
Avatar de Usuario
IndieDoroid
Mensajes: 27

Pharan

Is it possible that you're composing a skin at runtime or added externally-sourced attachments? That seems like the most obvious source of an extraneous material. Also, if you know of a plugin in your project that prepends materials with "SFX", or has that specific material name, that could give you a clue too.
Avatar de Usuario
Pharan

Pharan
Mensajes: 5220


Volver a Unity