G
hi, i wrote an custom animation utils for all UI elements that inspector can't seen in editor.
public static class UIShaderAnimatorUtils
{
public class AnimationRunner
{
private Coroutine animation;
private MonoBehaviour behaviour;
private string propertyName;
private float startvalue;
private float targetValue;
private float speed;
private float delay;
private Material material;
private Action completionAnimation;
internal AnimationRunner(MonoBehaviour behaviour, Material material, string propertyName)
{
this.material = material;
this.behaviour = behaviour;
this.propertyName = propertyName;
startvalue = material.GetFloat(propertyName);
}
internal void Start(float newValue, float speed, float delay = 0, Action completionAnimation = null)
{
this.delay = delay;
this.speed = speed;
this.completionAnimation = completionAnimation;
targetValue = newValue;
animation = behaviour?.StartCoroutine(AnimationProcess());
}
internal void Stop()
{
behaviour?.StopCoroutine(animation);
}
private IEnumerator AnimationProcess()
{
if (delay > 0f)
yield return new WaitForSeconds(delay);
bool isAdditive = startvalue < targetValue;
while (isAdditive ? (startvalue < targetValue) : (startvalue > targetValue))
{
if (isAdditive)
{
float newValue = startvalue + speed;
startvalue = newValue > targetValue ? targetValue : newValue;
}
else
{
float newValue = startvalue - speed;
startvalue = newValue < targetValue ? targetValue : newValue;
}
material.SetFloat(propertyName, startvalue);
yield return new WaitForSeconds(0.02f);
}
completionAnimation?.Invoke();
}
}
public static AnimationRunner BuildAnimation(MonoBehaviour behaviour, Material material, string propertyName)
{
return material == null
? null
: new AnimationRunner(behaviour, material, propertyName);
}
}
you can call that in this way:
UIShaderAnimatorUtils
.BuildAnimation(this, card.GetComponentInChildren<Image>().material, "_Burn_Value_1")
.Start(1f, 0.05f, 0, () => /*do something after animation complete */);
This my utils support only float params but can be extended for other purpuose :)
If unity inspector can see property field and not only variables would have been better. in this way it was enough to animate the property from the editor of unity which in turn changed the variable of the material, and instead we are here to reinvent the wheel