Unity中对于数值游戏的大数显示

前言

以前玩过几款手游,到中后期数值爆炸,显示单位也从正常的k、m等进化到aa、bb、cc等代替更高单位,下面记录下做法。

代码

csharp 复制代码
    private static readonly string[] Units = new string[] { "", "k", "m", "b", "t", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy", "zz" /* ...继续添加更多单位... */ };

    /// <summary>
    /// 大数转换
    /// </summary>
    /// <param name="number">原始数</param>
    /// <param name="decimalPlaces">保留几位小数</param>
    /// <returns></returns>
    public static string FormatNumber(double number, int decimalPlaces = 3)
    {
        if (number == 0)
            return "0";

        int unitIndex = 0;

        while (number >= 1000)
        {
            number /= 1000;
            unitIndex++;
        }

        unitIndex = Math.Min(unitIndex, Units.Length - 1);

        // 使用指定的小数位数格式化数字,并添加适当的单位
        string format = "{0:0." + new string('#', decimalPlaces) + "}{1}";
        return String.Format(format, number, Units[unitIndex]);
    }
相关推荐
yi碗汤园5 小时前
MQTT消息队列遥测传输协议
网络·网络协议·unity
林川~019 小时前
Unity 常用 API 与核心类全解:从生命周期到协程,附性能避坑清单(Unity 6 适配)
unity·游戏引擎·常用api
sensen_kiss10 小时前
CPT306 Coursework 1 个人游戏作业——坦克大战
unity·游戏程序·游戏策划
蒸鱼Yuzheng10 小时前
Unity 与 Unreal 测试工具怎么回答:Profiler、日志、自动化与证据链
测试工具·unity·性能分析·游戏测试·unrealengine
野区捕龙为宠12 小时前
Unity WebGL平台 通过 URL 加载图片(UGUI RawImage / 模型材质)
unity·团结引擎
一孤程1 天前
游戏测试专题第七篇:游戏自动化测试实战
游戏·测试
一孤程1 天前
游戏测试专题第八篇:游戏安全测试与反外挂
游戏·测试
阿松爱学习1 天前
【Unity开发】FileStream 详解及用法指南
unity·c#·unity开发
想做后端的前端1 天前
Unity-UIGI优化Rebatch与Rebuild
unity
玖玥拾2 天前
Unity Shader 基础与图形学(一)
unity·图形渲染·图形学·shader