Harald wroteUsing a MaterialPropertyBlock
in the editor is a valid approach.
In fact you only need to add [ExecuteInEditMode]
above the class declaration in the MaterialPropertyBlockExample.cs class, then you can have a look at the example scene Spine Examples/Other Examples/Per Instance Material Properties
to see color applied before entering play mode.
I have attached the modified script in case the description above is not clear enough:
MaterialPropertyBlockExample.cs
Hell yes, my team says thanks Harald 😃
Here is the script I made for real-time tinting in the Editor:
[Edit: This script has problems - see post below]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshRenderer))]
public class SpineMaterialBlockTint : MonoBehaviour {
public Color tintColor = new Color(1, 0, 0, 1); //Initial tint to Red
public string colorPropertyName = "_Color"; //Set this to the correct name based on which Shader you're using
MaterialPropertyBlock mpb;
float timeToNextColor = 0;
void Start() {
mpb = new MaterialPropertyBlock();
}
private void OnValidate() {
mpb.SetColor(colorPropertyName, tintColor);
GetComponent<MeshRenderer>().SetPropertyBlock(mpb);
}
}
It seems to be working great, but I'll mention that on one occasion it seemed to have lost the reference to mpb - I removed the component and re-added it and it started working again, so I'm not sure if it was just a fluke or not.
Script above had issues where the reference to "mpb" would get lost. It would happen when you run the game, or if you would focus on another window for a while and then go back to the Unity window.
So I added a null check for the MPB, and create another one if it is null. I added a Debug Log line "Created new MPB", and it looks like it only has to re-create it a single time when you run the game or come back to the Unity window after un-focusing it for a while. Not sure if there is a way around that or not, but it does indeed work now!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshRenderer))]
public class SpineMaterialBlockTint : MonoBehaviour {
public Color tintColor = new Color(1, 0, 0, 1); //Initial tint to Red
public string colorPropertyName = "_Color"; //May need to change based on Shader
MaterialPropertyBlock mpb;
void Start() {
mpb = new MaterialPropertyBlock();
UpdateColor();
}
private void OnValidate() {
UpdateColor();
}
void UpdateColor() {
if(mpb == null) {
Debug.Log("Creating new MPB");
mpb = new MaterialPropertyBlock();
}
mpb.SetColor(colorPropertyName, tintColor);
GetComponent<MeshRenderer>().SetPropertyBlock(mpb);
}
}