备份CatanBuilding瘦身独立工程
This commit is contained in:
46
Assets/Scripts/UIExtend/FloatExtensions.cs
Normal file
46
Assets/Scripts/UIExtend/FloatExtensions.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public static class FloatExtensions
|
||||
{
|
||||
public static string ToPercentageString(this float value)
|
||||
{
|
||||
int num = Mathf.RoundToInt(value * 100);
|
||||
return string.Format("{0}%", num);
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 安全的字符串格式化扩展方法。如果格式化失败,返回原始格式字符串并输出错误日志。
|
||||
/// </summary>
|
||||
/// <param name="format">包含格式项的字符串。</param>
|
||||
/// <param name="args">要格式化的对象数组。</param>
|
||||
/// <returns>格式化后的字符串;如果格式化失败,返回原始格式字符串。</returns>
|
||||
public static string SafeFormat(this string format, params object[] args)
|
||||
{
|
||||
if (format == null)
|
||||
{
|
||||
Debug.LogError("Error: Format string is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return string.Format(format, args);
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
Debug.LogError($"String formatting failed: {ex.Message}\n" +
|
||||
$"Format string: {format}\n" +
|
||||
$"Argument count: {args?.Length ?? 0}");
|
||||
return format;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"Unknown error occurred during string formatting: {ex.Message}");
|
||||
return format;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user