2025-05-18 01:04:31 +08:00

381 lines
8.0 KiB
C#

// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2018-2-8
// Description :
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
// <copyright file= "GameProxy" company=""></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using UnityEngine;
namespace G
{
/// <summary>
///
/// </summary>
public static class GlobalUtils
{
/// <summary>
/// Copy 文本到 系统
/// </summary>
/// <param name="text"></param>
public static void CopyToClipboard(string text)
{
if (!string.IsNullOrEmpty(text))
GUIUtility.systemCopyBuffer = text;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string GetClipboard()
{
return GUIUtility.systemCopyBuffer;
}
/// <summary>
/// 从数组[id,weight]中随机
/// </summary>
/// <param name="sources"></param>
/// <param name="start"></param>
/// <returns>返回结果Id 失败返回 int.MinValue</returns>
public static int GetRandom(IList<int> sources, int start = 0)
{
if (sources != null && sources.Count > 1)
{
if (start < 0)
start = 0;
int maxW = 0;
for (int i = sources.Count - 1; i > start; i -= 2)
{
maxW += sources[i];
}
int curW = Random.Range(0, maxW);
for (int i = sources.Count - 1; i > start; i -= 2)
{
curW -= sources[i];
if (curW < 0)
{
return sources[i - 1];
}
}
}
return int.MinValue;
}
/// <summary>
///
/// </summary>
/// <param name="sources"></param>
/// <param name="result"></param>
/// <param name="start"></param>
public static void GetRandom(int[] sources, ref int[] result, int start = 0)
{
if (result != null && sources != null && sources.Length > 1)
{
if (start < 0)
start = 0;
int maxW = 0;
for (int i = sources.Length - 1; i > start; i -= 2)
{
maxW += sources[i];
}
int count = result.Length;
RESULT:
int curW = Random.Range(0, maxW);
for (int i = sources.Length - 1; i > start; i -= 2)
{
curW -= sources[i];
if (curW < 0)
{
result[--count] = sources[i - 1];
break;
}
}
if (count > 0)
goto RESULT;
}
}
/// <summary>
///
/// </summary>
/// <param name="maxWeight"></param>
/// <returns></returns>
public static int GetWeight(int maxWeight)
{
return Random.Range(0, maxWeight);
}
public static Vector3 ConvertTo(int[] data)
{
Vector3 result = Vector3.zero;
if (data != null && data.Length > 0)
{
for (int i = 0; i < data.Length && i < 3; i++)
{
result[i] = data[i];
}
}
return result;
}
public static Vector2 GetVector2(int[] data)
{
if (data != null && data.Length >= 2)
{
return new Vector2(data[0], data[1]);
}
return Vector2.zero;
}
public static Vector2 GetVector2(float[] data)
{
if (data != null && data.Length >= 2)
{
return new Vector2(data[0], data[1]);
}
return Vector2.zero;
}
public static Rect GetRect(int[] data)
{
if (data != null && data.Length >= 4)
{
return new Rect(data[0], data[1], data[2], data[3]);
}
return default;
}
public static string NumberToChinese(int number)
{
switch (number)
{
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
case 10:
return "十";
}
return "";
}
public static int CombineNumber8888(int bit1, int bit2, int bit3, int bit4)
{
return (bit1 << 24) | (bit2 << 16 & 0xFF0000) | (bit3 << 8 & 0xFF00) | (bit4 & 0xFF);
}
public static void SplitNumber8888(int number, out int bit1, out int bit2, out int bit3, out int bit4)
{
bit1 = (number >> 24) & 0xFF;
bit2 = (number >> 16) & 0xFF;
bit3 = (number >> 8) & 0xFF;
bit4 = number & 0xFF;
}
public static int CombineNumber824(int bit8, int bit24)
{
return (bit8 << 24) | (bit24 & 0x00FFFFFF);
}
public static void SplitNumber824(int number, out int bit8, out int bit24)
{
bit8 = (number >> 24) & 0xFF;
bit24 = number & 0x00FFFFFF;
}
public static int CombineNumber_16_16(int bit16, int bit16_2)
{
return (bit16 << 16) | (bit16_2 & 0x0000FFFF);
}
public static void SplitNumber_16_16(int number, out int bit16, out int bit16_2)
{
bit16 = (number >> 16) & 0xFFFF;
bit16_2 = number & 0x0000FFFF;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="index"></param>
/// <returns></returns>
public static int GetHalfByte(int source, int index)
{
int bit = index * 4;
return source >> bit & 0xF;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="index"></param>
/// <param name="hb"></param>
public static void SetHalfByte(int source, int index, int hb)
{
int bit = index * 4;
source = (source & ((0xF << bit) ^ -1)) & ((hb << bit) & 0xF);
}
/// <summary>
///
/// </summary>
public static float RndPercent
{
get { return Random.value; }
}
/// <summary>
///
/// </summary>
public static int RndHundred
{
get { return Random.Range(0, 100); }
}
/// <summary>
///
/// </summary>
public static int RndThousand
{
get { return Random.Range(0, 1000); }
}
/// <summary>
///
/// </summary>
public static int RndAngle
{
get { return Random.Range(0, 360); }
}
static readonly NumToChn _CNNumberString = new NumToChn();
public static string ConvertCNNumber(int number)
{
return _CNNumberString.NumberToChinese(number);
}
/// <summary>
///
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string GetNumberString(int number)
{
if (number > 1000000)
{
return $"{number / 10000}万";
}
return number.ToString();
}
/// <summary>
/// v1 < v2
/// </summary>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <returns></returns>
public static bool IsSmallVersion(string v1, string v2)
{
if (System.Version.TryParse(v1, out var ver1))
{
if (System.Version.TryParse(v2, out var ver2))
{
return ver1 < ver2;
}
return false;
}
return true;
}
}
class NumToChn
{
private readonly string[] _chnNumChar = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
private readonly string[] _chnUnitSection = new string[] { "", "万", "亿", "万亿" };
private readonly string[] _chnUnitChar = new string[] { "", "十", "百", "千" };
public string NumberToChinese(int num)
{
int unitPos = 0;
string strIns = "";
bool needZero = false;
string chnStr = "";
while (num > 0)
{
int section = num % 10000;
if (needZero)
{
chnStr = chnStr.Insert(0, _chnNumChar[0]);
}
strIns = SectionToChinese(section);
//是否需要节权位
strIns += (section != 0) ? _chnUnitSection[unitPos] : _chnUnitSection[0];
chnStr = chnStr.Insert(0, strIns);
//千位是0需要在下一个section补零
needZero = (section < 1000) && (section > 0);
num = num / 10000;
unitPos++;
}
return chnStr;
}
public string SectionToChinese(int section)
{
string strIns;
int unitPos = 0;
bool zero = true;
string chnStr = "";
while (section > 0)
{
int v = section % 10;
if (v == 0)
{
if ((section == 0) || !zero)
{
zero = true;
chnStr = chnStr.Insert(0, _chnNumChar[v]);
}
}
else
{
zero = false;
strIns = _chnNumChar[v];
strIns += _chnUnitChar[unitPos];
chnStr = chnStr.Insert(0, strIns);
}
unitPos++;
section = section / 10;
}
return chnStr;
}
}
}