Files
BingoGrassland/Assets/BingoBrain/Harmony/GlobalHarmony.cs
T
2026-04-20 13:49:36 +08:00

347 lines
9.1 KiB
C#

using System;
using FairyGUI;
using System.Text;
using UnityEngine;
using System.Collections.Generic;
using Application = UnityEngine.Application;
using Random = UnityEngine.Random;
namespace BingoBrain
{
public static class GlobalHarmony
{
public static bool IsChance(float chance)
{
var value = Random.Range(0, 1f);
return value <= chance;
}
public static void Active(this Component component, bool isActive)
{
component.gameObject.SetActive(isActive);
}
public static string GetAppSavePath()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
return Application.streamingAssetsPath;
#elif UNITY_IOS
return Application.persistentDataPath;
#endif
}
public static GTweener In(GObject obj, float duration = 0.3f, float delay = 0)
{
if (obj == null) return null;
obj.alpha = 0.5f;
return obj.TweenFade(1, duration).SetDelay(delay);
}
public static GTweener Out(GObject obj, float duration = 0.3f, float delay = 0)
{
if (obj == null) return null;
obj.alpha = 1;
return obj.TweenFade(0f, duration).SetDelay(delay);
}
private static float clickTime;
public static bool NoQuick()
{
if (Time.time < clickTime)
{
clickTime = Time.time + 0.1f;
return false;
}
clickTime = Time.time + 0.3f;
return true;
}
public static void CheckClick(GObject button, Action action, bool isNetworkCheck = false,
bool isQuickClickCheck = true)
{
button?.onClick.Set(() =>
{
if (isQuickClickCheck && !NoQuick())
{
return;
}
action?.Invoke();
});
}
public static string FormatDate(int second, CountDownType countDownType = CountDownType.Second,
string dayFormat = "")
{
var result = new StringBuilder();
if (second < 0)
{
second = 0;
}
if (countDownType == CountDownType.Second)
{
result.Append(second);
return result.ToString();
}
int tmpSecond;
if (second >= 60)
{
tmpSecond = second % 60;
}
else
{
tmpSecond = second;
}
result.Append(tmpSecond);
if (tmpSecond < 10)
{
result.Insert(0, 0);
}
int tmpMin = second / 60;
if (countDownType == CountDownType.Minute)
{
result.Insert(0, ":");
result.Insert(0, tmpMin);
return result.ToString();
}
int tmpHour = tmpMin / 60;
if (countDownType == CountDownType.Hour)
{
if (tmpMin >= 60)
{
tmpMin %= 60;
}
result.Insert(0, ":");
result.Insert(0, tmpMin);
if (tmpMin < 10)
{
result.Insert(0, 0);
}
result.Insert(0, ":");
result.Insert(0, tmpHour);
if (tmpHour < 10)
{
result.Insert(0, 0);
}
return result.ToString();
}
int tmpDay = tmpHour / 24;
if (countDownType == CountDownType.Day)
{
if (tmpMin >= 60)
{
tmpMin %= 60;
}
result.Insert(0, ":");
result.Insert(0, tmpMin);
if (tmpMin < 10)
{
result.Insert(0, 0);
}
if (tmpHour >= 24)
{
tmpHour %= 24;
}
result.Insert(0, ":");
result.Insert(0, tmpHour);
if (tmpHour < 10)
{
result.Insert(0, 0);
}
if (string.IsNullOrEmpty(dayFormat))
{
dayFormat = "Days";
}
result.Insert(0, $" {dayFormat} ");
result.Insert(0, tmpDay);
}
return result.ToString();
}
public static T GetRandomList<T>(IList<T> list)
{
if (list.Count == 0)
{
return default(T);
}
int index = UnityEngine.Random.Range(0, list.Count);
return list[index];
}
public static List<int> tmpIndexList = new List<int>();
public static List<T> GetRandomList<T>(List<T> list, int num, bool returnMax = true)
{
if (list.Count < num)
{
if (returnMax)
{
num = list.Count;
}
else
{
return null;
}
}
tmpIndexList.Clear();
List<T> tempList = new List<T>();
for (int i = 0; i < num; i++)
{
int index = UnityEngine.Random.Range(0, list.Count);
while (tmpIndexList.Contains(index))
{
index = UnityEngine.Random.Range(0, list.Count);
}
tmpIndexList.Add(index);
}
foreach (var index in tmpIndexList)
{
tempList.Add(list[index]);
}
return tempList;
}
public static int RandomWeight(int[] weights)
{
int val = UnityEngine.Random.Range(1, 101);
int temp = 0;
for (int i = 0; i < weights.Length; i++)
{
int tar = weights[i];
temp += tar;
if (val <= temp)
{
return i;
}
}
return -1;
}
public static int GetChanceList(List<float> chanceList)
{
float total = 0;
for (int i = 0; i < chanceList.Count; i++)
{
total += chanceList[i];
}
float result = Random.Range(0, total);
float tmpTotal = 0;
for (int i = 0; i < chanceList.Count; i++)
{
float chance = chanceList[i];
tmpTotal += chance;
if (result < tmpTotal)
{
return i;
}
}
if (chanceList.Count > 0)
{
return chanceList.Count - 1;
}
return -1;
}
public static string FormatJson(string sourceJson)
{
sourceJson += " ";
var index = 0;
var newJson = new StringBuilder();
for (int i = 0; i < sourceJson.Length - 1; i++)
{
if (sourceJson[i] == '{' || sourceJson[i] == '[')
{
index++;
newJson.Append(sourceJson[i]);
newJson.Append("\n");
for (var a = 0; a < index; a++)
{
newJson.Append("\t");
}
}
else if ((sourceJson[i] == '}' || sourceJson[i] == ']'))
{
index--;
newJson.Append("\n");
for (var a = 0; a < index; a++)
{
newJson.Append("\t");
}
newJson.Append(sourceJson[i]);
newJson.Append(sourceJson[i + 1] == ',' ? "," : "");
newJson.Append("\n");
if (sourceJson[i + 1] == ',') i++;
for (int a = 0; a < index; a++)
{
newJson.Append("\t");
}
}
else if (sourceJson[i] != '}' && sourceJson[i] != ']' && sourceJson[i + 1] == ',')
{
newJson.Append(sourceJson[i]);
newJson.Append(sourceJson[i + 1]);
newJson.Append("\n");
i++;
for (var a = 0; a < index; a++)
{
newJson.Append("\t");
}
}
else
{
newJson.Append(sourceJson[i]);
}
}
return newJson.ToString();
}
public static void ForEachSafe<TK, TV>(this Dictionary<TK, TV> dict, Action<TK, TV> action)
{
var keys = new List<TK>(dict.Keys);
for (var i = 0; i < dict.Count; i++)
{
var temp = dict[keys[i]];
action?.Invoke(keys[i],temp);
}
}
}
}