TweenText 支持千位分隔符

作者:雨辰 发布于:2017-8-22 19:59 Tuesday 分类:Unity3D

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
/// <summary>
/// Tween the widget's depth.
/// </summary>

[RequireComponent(typeof(UIWidget))]
[AddComponentMenu("NGUI/Tween/Tween Text")]
public class TweenText : UITweener
{
    public int from = 100;
    public int to = 100;
    //是否带千位分隔符号
    public bool isShowSeparator = false;
    public string separatorStr = ",";
    UILabel mWidget;

    public UILabel cachedWidget { get { if (mWidget == null) mWidget = GetComponent<UILabel>(); return mWidget; } }

    [System.Obsolete("Use 'value' instead")]
    public int num { get { return this.value; } set { this.value = value; } }

    /// <summary>
    /// Tween's current value.
    /// </summary>

    public int value 
    { 
        get 
        {
            if (isShowSeparator)
            {
                return ParseThousandthString(cachedWidget.text); 
            }
            else
            {
                return Convert.ToInt32(cachedWidget.text); 
            }
        } 
        set 
        {
            if (isShowSeparator)
            {
                cachedWidget.text = FormatValue(value);
            }
            else 
            {
                cachedWidget.text = value.ToString();
            }
        }
    }

    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished)
    {
        value = Mathf.RoundToInt(from * (1f - factor) + to * factor);
    }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenText Begin(UILabel widget, float duration, int num)
    {
        TweenText comp = UITweener.Begin<TweenText>(widget.gameObject, duration);
        //TweenDepth comp = UITweener.Begin<TweenDepth>(go, duration);
        comp.from = Convert.ToInt32(widget.text);
        comp.to = num;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }

    /// <summary>
    /// 把数字转为要解析的字符串
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private string FormatValue(decimal value)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        string sValue = value.ToString();
        if (isShowSeparator)
        {
            List<string> list = new List<string>();

            string temp = sValue;

            bool includeFloat = sValue.LastIndexOf(".") != -1;

            if (includeFloat)
            {
                //temp = temp.Substring(0, sP);
                temp = ((int)value).ToString();
            }
            int templength = temp.Length;
            if (temp.Length > 3)
            {
                while (templength > 3)
                {
                    list.Add(temp.Substring(templength - 3, 3));
                    templength -= 3;
                }

                //最前面的添加进来
                list.Add(temp.Substring(0, temp.Length - list.Count * 3));

                for (int i = list.Count - 1; i > 0; i--)
                {
                    sb.Append(list[i] + separatorStr);
                }
                sb.Append(list[0]);

                if (includeFloat)
                {
                    sb.Append(sValue.Substring(sValue.LastIndexOf(".")));
                }
            }
            else
            {
                return sValue.ToString();
            }
        }
        return sb.ToString();
    }

    /// <summary>
    /// 将千分位字符串转换成数字
    /// 说明:将诸如"–111,222,333的千分位"转换成-111222333数字
    /// 若转换失败则返回-1
    /// </summary>
    /// <param name="thousandthStr">需要转换的千分位</param>
    /// <returns>数字</returns>
    public int ParseThousandthString(string thousandthStr)
    {
        int _value = -1;
        if (!string.IsNullOrEmpty(thousandthStr))
        {
            try
            {
                _value = int.Parse(thousandthStr,  NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign);
            }
            catch (Exception ex)
            {
                _value = -1;
                Debug.LogError(string.Format("将千分位字符串{0}转换成数字异常,原因:{0}", thousandthStr, ex.Message));
            }
        }
        return _value;
    }
}

标签: Unity3D-优化性能 Unity3D-Editer Unity3D

发表评论:

雨辰 joyimp|@2011-2017 京ICP备16030765号