change
This commit is contained in:
36
Assets/Scripts/Utils/GameUtil.cs
Normal file
36
Assets/Scripts/Utils/GameUtil.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class GameUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 格式化数字显示,大于等于10000显示N万,保留1位小数点,整数1万、2万不要小数点
|
||||
/// </summary>
|
||||
/// <param name="number">要格式化的数字</param>
|
||||
/// <returns>格式化后的字符串</returns>
|
||||
public static string FormatNumber(this int number)
|
||||
{
|
||||
if (number < 10000)
|
||||
{
|
||||
return number.ToString(); // 小于10000直接显示
|
||||
}
|
||||
|
||||
// 转换为万为单位
|
||||
double valueInTenThousand = number / 10000.0;
|
||||
|
||||
// 判断是否为整数万
|
||||
if (Math.Abs(valueInTenThousand - Math.Floor(valueInTenThousand)) < 0.00001) // 处理浮点数精度
|
||||
{
|
||||
// 整数万,不显示小数点
|
||||
return $"{(int)valueInTenThousand}万";
|
||||
}
|
||||
|
||||
// 非整数万,向下取整并保留1位小数
|
||||
double flooredValue = Math.Floor(valueInTenThousand * 10) / 10;
|
||||
|
||||
// 处理浮点数精度,确保只显示1位小数
|
||||
return $"{flooredValue:F1}万";
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utils/GameUtil.cs.meta
Normal file
3
Assets/Scripts/Utils/GameUtil.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8eef66abe7f429ebc76870fe1d0e10c
|
||||
timeCreated: 1770649984
|
||||
@@ -44,5 +44,64 @@ namespace NBF
|
||||
Debug.LogWarning($"androidId={androidId}");
|
||||
return androidId;
|
||||
}
|
||||
|
||||
public static string GetDeviceModel()
|
||||
{
|
||||
string deviceModel = string.Empty;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// 在编辑器中使用 SystemInfo
|
||||
deviceModel = SystemInfo.deviceModel;
|
||||
#elif UNITY_ANDROID
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 方法1:使用 SystemInfo.deviceModel(推荐,最简单)
|
||||
deviceModel = SystemInfo.deviceModel;
|
||||
|
||||
// 方法2:通过 Android API 获取更详细的信息(可选)
|
||||
/*
|
||||
using (AndroidJavaClass buildClass = new AndroidJavaClass("android.os.Build"))
|
||||
{
|
||||
string manufacturer = buildClass.GetStatic<string>("MANUFACTURER");
|
||||
string model = buildClass.GetStatic<string>("MODEL");
|
||||
string product = buildClass.GetStatic<string>("PRODUCT");
|
||||
string device = buildClass.GetStatic<string>("DEVICE");
|
||||
|
||||
// 可以根据需要组合不同的信息
|
||||
deviceModel = $"{manufacturer} {model}";
|
||||
// 或者更详细的:deviceModel = $"{manufacturer} {model} (Product: {product}, Device: {device})";
|
||||
}
|
||||
*/
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError("Error while fetching device model: " + e.Message);
|
||||
deviceModel = SystemInfo.deviceModel; // 失败时回退到 SystemInfo
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceModel = SystemInfo.deviceModel;
|
||||
}
|
||||
#elif UNITY_IOS
|
||||
// iOS平台
|
||||
deviceModel = SystemInfo.deviceModel;
|
||||
|
||||
// 如果需要获取更友好的设备名称(如 "iPhone 14 Pro" 而不是 "iPhone15,2")
|
||||
|
||||
if (Application.platform == RuntimePlatform.IPhonePlayer)
|
||||
{
|
||||
deviceModel = UnityEngine.iOS.Device.generation.ToString();
|
||||
}
|
||||
#else
|
||||
// 其他平台
|
||||
deviceModel = SystemInfo.deviceModel;
|
||||
#endif
|
||||
|
||||
Debug.LogWarning($"Device Model: {deviceModel}");
|
||||
return deviceModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user