Make digital changes cool up, digital scrolling text widget [Unity]

Source: Internet
Author: User
Let the numbers roll up

Last week, my plan also asked for a kind of demand, when the player rating changes, the screen appears the character scoring floating UI, play the scoring digital scrolling animation. This kind of digital scrolling demand is very common, I will follow the general idea, Startvalue and endvalue every little time to do interpolation changes and display, so as to achieve the effect of digital scrolling, which is the majority of apps and games to take the implementation, the effect is as follows:

A few lines of code to plan to see the results, planning said not the effect, with the XX game do not like the same as the Lao Tiger machine numbers, there is a real digital scrolling effect, well, this rolling non-scrolling, the expected effect should be the following, looks more really cool:
Code Implementation

This effect is not difficult to achieve, but to pay attention to some details, so that the animation looks more realistic: still every little time to do interpolation changes, calculate the interpolation, no longer simply modify the text content, but prepared with another text to display the new value, 2 text from the bottom up to do easing, resulting in like Lao Tiger Machine, The new value animates the old value, and note that each digit is displayed separately using the text component. The forces in our game are fixed in 6-digit numbers, so there are 12 text components set up in total. Stagger the scrolling time points for each digit, making the scrolling effect look more natural. Set a delay variable, starting from the single digit, each of the following bits is incremented by a delay and then scrolled.

/* ============================================================================== * Function Description: Digital dynamic Change Text * Create
By: Shuchangliu * ==============================================================================*/using System;
Using System.Collections;
Using System.Collections.Generic;
Using System.Linq; Using DG.
tweening;
Using Unityengine;

Using Unityengine.ui;
    public class Jumpingnumbertextcomponent:monobehaviour {[Serializefield] [Tooltip ("Set per digit text (display group) by highest bit start order)] 
    Private list<text> _numbers;
    [Serializefield]
    [Tooltip (set per digit Text (substitution group) by highest bit start order)] private list<text> _unactivenumbers;
    <summary>///Animation duration///</summary> [Serializefield] private float _duration = 1.5f; <summary>////number of times per scroll///</summary> [Serializefield] private float _rollingduration = 0.
    05f;
    <summary>///digital change per value///</summary> private int _speed; <summary>// Rolling delay (one multiplier per incoming one increases the delay, allowing scrolling to look more random natural)///</summary> [Serializefield] private float _delay = 0.008f;
    <summary>///Text text width///</summary> private Vector2 _numbersize;
    <summary>///Current digital///</summary> private int _curnumber;
    <summary>//start digit///</summary> private int _fromnumber;
    <summary>///FINAL digital///</summary> private int _tonumber; <summary>////For examples of easing instances///</summary> private list<tweener> _tweener = new List<twee
    Ner> ();
    <summary>//Whether in digital scrolling///</summary> private bool _isjumping;

    <summary>///Rollover callback///</summary> public Action oncomplete; private void Awake () {if (_numbers. Count = = 0 | | _unactivenumbers.count = = 0) {MediaUnity.Debugger.LogError ("[jumpingnumbertextcomponent] has not yet set the text component!")
 );           Return
    } _numbersize = _numbers[0].recttransform.sizedelta;
        } public float Duration {get {return _duration;}
        set {_duration = value;
    }} private float _different;
    Public float Different {get {return _different;} public void change (int from, int to) {bool Isrepeatcall = _isjumping && _fromnumber = = from &A
        mp;& _tonumber = = to;

        if (Isrepeatcall) return; BOOL Iscontinuouschange = (_tonumber = = from) && ((To-from > 0 && _different > 0) | |
        (To-from < 0 && _different < 0));
            if (_isjumping && iscontinuouschange) {} else {_fromnumber = from;
        _curnumber = _fromnumber;

        } _tonumber = to;
        _different = _tonumber-_fromnumber; _speed = (int) math.ceiling (_different/(_duration * (1/_ro(llingduration))); _speed = _speed = = 0?

        (_different > 0 1:-1): _speed;
        Setnumber (_curnumber, false);
        _isjumping = true;
        Stopcoroutine ("Dojumpnumber");  
    Startcoroutine ("Dojumpnumber");
        } public int Number {get {return _tonumber;}
            set {if (_tonumber = = value) return;
        Change (_curnumber, _tonumber);
            }} IEnumerator Dojumpnumber () {while (true) {if (_speed > 0)//Increase
            {_curnumber = math.min (_curnumber + _speed, _tonumber); } else if (_speed < 0)//decrease {_curnumber = Math.max (_curnumber + _speed, _tonum
            ber);

            } setnumber (_curnumber, true);
                if (_curnumber = = _tonumber) {stopcoroutine ("Dojumpnumber");
                _isjumping = false; if (oncomplete! = null) OnCoMplete ();
            yield return null;
        } yield return new Waitforseconds (_rollingduration);
    }}///<summary>///Set force figures///</summary>//<param name= "V" ></param> <param name= "Istween" ></param> public void Setnumber (int v, bool istween) {char[] c = v.t Ostring ().
        ToCharArray ();
        Array.reverse (c);

        string s = new string (c); if (!istween) {for (int i = 0; i < _numbers. Count;
                i++) {if (I < S.count ()) _numbers[i].text = S[i] + "";
            else _numbers[i].text = "0"; }} else {while (_tweener. Count > 0) {_tweener[0].
                Complete (); _tweener.
            RemoveAt (0); } for (int i = 0; i < _numbers. Count; i++) {if(I < S.count ())
                {_unactivenumbers[i].text = S[i] + "";
                } else {_unactivenumbers[i].text = "0"; } _unactivenumbers[i].recttransform.anchoredposition = new Vector2 (_unactivenumbers[i].recttransform.anch
                Oredposition.x, (_speed > 0 -1:1) * _numbersize.y);

                _numbers[i].recttransform.anchoredposition = new Vector2 (_unactivenumbers[i].recttransform.anchoredposition.x, 0); if (_unactivenumbers[i].text! = _numbers[i].text) {dotween (_numbers[i], (_ Speed > 0?
                    1:-1) * _numbersize.y, _delay * i);

                    Dotween (_unactivenumbers[i], 0, _delay * i);
                    Text tmp = _numbers[i];
                    _numbers[i] = _unactivenumbers[i];
                _unactivenumbers[i] = tmp; }}}} public void Dotween (TeXT text, float endvalue, float delay) {Tweener T = dotween.to (() = Text.rectTransform.anchoredPosition, (
        X) = = {Text.rectTransform.anchoredPosition = x; }, New Vector2 (text.recttransform.anchoredposition.x, Endvalue), _rollingduration-delay).
        Setdelay (delay); _tweener.
    ADD (t); } [ContextMenu ("Test number Changes")] public void Testchange () {Change (UnityEngine.Random.Range (1, 1), Unityeng Ine.
    Random.range (1, 100000)); }

}
Tagged:  game development,  Unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.