- Uređeno
Cant get CustomMaterialOverride to work
Hi, I'm trying to change the material of a Skeleton_Animation at runtime using CustomMaterialOverride. I'm using latest 4.0 Unity runtime and 4.0 spine software
I have checked the documentation already here: spine-unity Runtime Documentation: Changing Materials Per Instance
From my understanding, the following should just work? I have add Debug.Log(..)s to the code to ensure it is running, and it definitely is, and m_OriginalMaterial does exist, and so does newMaterial
However, even if I set the newMaterial to something like blue water or something, the appearance doesn't change in engine. Am i missing something or doing something wrong?
// Check
if (m_Skeleton_Animation != null)
{
// Check cached
if (m_OriginalMaterial == null)
{
// Store original
m_OriginalMaterial = m_Skeleton_Animation.GetComponent<MeshRenderer>().material;
}
// Override - both these methods don't work?
//m_Skeleton_Animation.CustomMaterialOverride[m_OriginalMaterial] = newMaterial;
m_Skeleton_Animation.CustomMaterialOverride.Add(m_OriginalMaterial, newMaterial);
}
EDIT - SOLVED
I managed to solve this, it was because I was getting .material instead of .sharedMaterial. From what I remember, using .material returns an instanced clone of the material, not the actual original one
It'd be nice if a note on how to actually obtain the original material was added to that documentation snippet, as it was difficult to figure out how to get it - and even then I did it wrong.
Thank you
I'm glad to hear that you were able to solve your issue!
And thank you for the suggestion to add that snippet to our documentation. I'll confer with Harri, who maintains our spine-unity documentation about adding that.
Sorry to hear you've been having troubles, thanks for reporting. Please note that your code using .sharedMaterial
might also return null when no Material is assigned (no attachments visible), or when called to early.
The recommended way to query the primary material is as follows:
originalMaterial = skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
I have added respective notes in the documentation here, thanks for your feedback:
spine-unity Runtime Documentation: Changing Materials Per Instance
Please let us know if the documentation is now more clear in this regard, we are always happy to improve it.
We have also added the lines to the example script here on the 4.0 branch:
MaterialReplacementExample.cs#L50
It will be merged to 4.1-beta soon, and will be released in the upcoming unitypackages.