XShell Nginx JS 莱斯分布 二代征信 api dom rxjs yii2 base64 vue中文网 vue图表 vue绑定点击事件 后台界面 前端项目实战 微信小游戏开发视频 jquery循环遍历 查看rabbitmq版本 oracle取第一条数据 bitlocker加密好慢 jq入口函数 oracle重命名表名 python安装mysql python3教程 python编程教程 java函数 java基础数据类型 java求和 javaif语句 java集合遍历 python 教程 win10长期服务版 beatedit ad下载 saminside 网络克隆 js日期格式化 刷新页面 mysql导出数据 骰子gif
当前位置: 首页 > 学习教程  > 编程语言

Unity 老虎机数字滚动

2020/9/19 15:52:14 文章标签:

找不到原作者博客了 , 转载自该大神 代码作者: shuchangliu ,谢谢这位大佬的无私奉献,为您鼓掌

组织结构图:

 

/* ============================================================================== 
* 功能描述:数字动态变化Text 
* 创 建 者: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("按最高位起始顺序设置每位数字Text(显示组)")]
    private List<Text> _numbers;
    [SerializeField]
    [Tooltip("按最高位起始顺序设置每位数字Text(替换组)")]
    private List<Text> _unactiveNumbers;
    /// <summary>  
    /// 动画时长  
    /// </summary>  
    [SerializeField]
    private float _duration = 1.5f;
    /// <summary>  
    /// 数字每次滚动时长  
    /// </summary>  
    [SerializeField]
    private float _rollingDuration = 0.05f;
    /// <summary>  
    /// 数字每次变动数值  
    /// </summary>  
    private int _speed;
    /// <summary>  
    /// 滚动延迟(每进一位增加一倍延迟,让滚动看起来更随机自然)  
    /// </summary>  
    [SerializeField]
    private float _delay = 0.008f;
    /// <summary>  
    /// Text文字宽高  
    /// </summary>  
    private Vector2 _numberSize;
    /// <summary>  
    /// 当前数字  
    /// </summary>  
    private int _curNumber;
    /// <summary>  
    /// 起始数字  
    /// </summary>  
    private int _fromNumber;
    /// <summary>  
    /// 最终数字  
    /// </summary>  
    private int _toNumber;
    /// <summary>  
    /// 各位数字的缓动实例  
    /// </summary>  
    private List<Tweener> _tweener = new List<Tweener>();
    /// <summary>  
    /// 是否处于数字滚动中  
    /// </summary>  
    private bool _isJumping;
    /// <summary>  
    /// 滚动完毕回调  
    /// </summary>  
    public Action OnComplete;

    private void Awake()
    {
        if (_numbers.Count == 0 || _unactiveNumbers.Count == 0)
        {
            Debug.LogError("[JumpingNumberTextComponent] 还未设置Text组件!");
            return;
        }
        _numberSize = _numbers[0].rectTransform.sizeDelta;
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            TestChange();
        }
    }

    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 && _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 / _rollingDuration)));
        _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)//增加  
            {
                _curNumber = Math.Min(_curNumber + _speed, _toNumber);
            }
            else if (_speed < 0) //减少  
            {
                _curNumber = Math.Max(_curNumber + _speed, _toNumber);
            }
            SetNumber(_curNumber, true);

            if (_curNumber == _toNumber)
            {
                StopCoroutine("DoJumpNumber");
                _isJumping = false;
                if (OnComplete != null) OnComplete();
                yield return null;
            }
            yield return new WaitForSeconds(_rollingDuration);
        }
    }

    /// <summary>  
    /// 设置战力数字  
    /// </summary>  
    /// <param name="v"></param>  
    /// <param name="isTween"></param>  
    public void SetNumber(int v, bool isTween)
    {
        char[] c = v.ToString().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.anchoredPosition.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("测试数字变化")]
    public void TestChange()
    {
        int x = UnityEngine.Random.Range(1, 100000);
        print(x);
        Change(0, x);
    }

}

 


本文链接: http://www.dtmao.cc/news_show_200331.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?