Simple C# script for creating simple smooth value change effect.

Content not available.
Please allow cookies by clicking Accept on the banner

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

 // © 2017 TheFlyingKeyboard and released under MIT License 
 // theflyingkeyboard.net 

public class LerpText : MonoBehaviour {
    [SerializeField] private float lerpSpeed = 0.25f;

    private Text myText;

    private float maxValue = 0.0f;
    private float previousValue = 0.0f;
    private float t = 0.0f;
    

    private void Start()
    {
        myText = GetComponent<Text>();
    }

    private void Update()
    {
        if(t < 1.0f)
        {
            t += lerpSpeed * Time.deltaTime;
            myText.text = Mathf.Lerp(previousValue, maxValue, t).ToString("0.");
            //myText.text = Mathf.SmoothStep(previousValue, maxValue, t).ToString("0.");
        }
    }

    public void AddValue(float amount)
    {
        previousValue = maxValue;
        maxValue += amount;
        t = 0.0f;
    }

    public void SubtractValue(float amount)
    {
        previousValue = maxValue;
        maxValue -= amount;
        t = 0.0f;
    }
}

 

Unity UI C# Smooth Value Text Effect
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close