- Edited
Applying to change one spine material color of prefab
Hello,
I'm trying to change spine material of prefab with the script like as below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MONSTER1_AI;
namespace Spine.Unity.Examples
{
public class SpineTint : MonoBehaviour
{
public string colorPropertyName = "_Black";
MaterialPropertyBlock mpb;
MeshRenderer monsterColorNow;
void Start()
{
mpb = new MaterialPropertyBlock();
}
void Update()
{
monsterColorNow = this.gameObject.transform.root.GetChild(0).GetComponent<MeshRenderer>();
Debug.Log(this.gameObject.transform.root.name + " " + monsterColorNow);
if(monster1_ai.whitePaint == true) // if monster took damage
{
Color newColor = Color.white; // make the material tint with white
mpb.SetColor(colorPropertyName, newColor);
monsterColorNow.SetPropertyBlock(mpb);
}
else // if monster not took damage
{
Color newColor = Color.black; // off the material tint
mpb.SetColor(colorPropertyName, newColor);
monsterColorNow.SetPropertyBlock(mpb);
}
}
}
}
And it succeed once, but it seems to affects for entire material, so color of whole monsters using same prefab is changing now.
How can I solve this? Any advices will be appreciated!!
The property block should affect only a single MeshRenderer, it also works as expected in the Spine Examples/Other Examples/Per Instance Material Properties
example scene. Did you test whether the if
branch is really only entered once?
Thank you for reply Harald. I checked it again and solved. Thanks!
The problem on my issue was "FillPhase" from Skeleton Fill shader. Most of sample code shows me "FillAlpha" so I confused.
I think it will be better if you update the guidance / sample code with it.
Result
Glad you figured it out. The result looks good already!
windship wroteI think it will be better if you update the guidance / sample code with it.
Would happily update it, just could not find it (neither in the sample code of the spine-unity package, nor on the spine-unity documentation pages). Where did you find this?
Like as below:
https://gist.github.com/pharan/7a06e2a5239f3893d79b8852c249642f <
Pharan's sample code
spine-unity Runtime Documentation: Materials <
No description of "FillPhase" on MaterialPropertyBlocks
windship wrote<
Pharan's sample code
The pages by Pharan are old and obsolete, so please use them with caution. Please only use the official git repository or spine-unity documentation pages as reference. Unfortunately we do not have access to Pharan's private git repositories, so we cannot change these (Pharan left Esoteric Software about 1.5 years ago).
windship wrotespine-unity Runtime Documentation: Materials <
No description of "FillPhase" on MaterialPropertyBlocks
While it is not necessary in the mentioned example code, I have add the line so that it's perhaps helping others.
In general please always look at either the shader code or use the Inspector's debug view mode to show the actual names of shader parameters. Unfortuantely Unity's MaterialPropertyBlock API is referencing the shader names directly and not the nice verbose name displayed in the normal Inspector view.
Thank you for reply and your detailed answer. Deeply appreciated!
Well, I see about the situation. I think this is not simple because there's so many articles about "MaterialPropertyBlock" by Pharan in the forum. Even he left the Esotericsoftware, novices like me will try to search the solution and will find his code.
However, as you updated the Runtime Documentation, another novices will find and use the feature more easily now. Thank you so much!
Thanks for taking the time for your detailed feedback as well! Old postings can be a two-edged sword unfortuantely - sometimes they help, sometimes they lead astray.
So thanks for providing all the feedback, it helps a lot in improving the official documentation pages with every hint about missing topics (it also just reminded me of one additional paragraph that might help), so that in the end everything is well documented and makes searching hopefully unnecessary. :nerd: