477 lines
13 KiB
C#
477 lines
13 KiB
C#
using System;
|
|
using FairyGUI;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using Application = UnityEngine.Application;
|
|
using Random = UnityEngine.Random;
|
|
using DG.Tweening;
|
|
|
|
namespace ZooMatch
|
|
{
|
|
public static class CommonHelper
|
|
{
|
|
public static Dictionary<int, int> activeTimes = new Dictionary<int, int>();
|
|
public static Dictionary<int, int> activeTimes_saveingpot = new Dictionary<int, int>();
|
|
public static int GetIndexByChanceList(int[] chanceList)
|
|
{
|
|
float total = 0;
|
|
for (int i = 0; i < chanceList.Length; i++)
|
|
{
|
|
total += chanceList[i];
|
|
}
|
|
|
|
float result = Random.Range(0, total);
|
|
float tmpTotal = 0;
|
|
for (int i = 0; i < chanceList.Length; i++)
|
|
{
|
|
float chance = chanceList[i];
|
|
tmpTotal += chance;
|
|
if (result < tmpTotal)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
|
|
|
|
return -1;
|
|
}
|
|
|
|
public static bool GetBoolByChance(float chance)
|
|
{
|
|
var value = UnityEngine.Random.Range(0, 1f);
|
|
if (value <= chance)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
public static void SetActive(this Component component, bool isActive)
|
|
{
|
|
component.gameObject.SetActive(isActive);
|
|
}
|
|
|
|
public static string GetAppSavePath()
|
|
{
|
|
return Application.persistentDataPath;
|
|
}
|
|
|
|
public static GTweener FadeIn(GObject obj, float duration = 0.3f, float delay = 0)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
obj.alpha = 0.5f;
|
|
return obj.TweenFade(1, duration).SetDelay(delay);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static GTweener FadeOut(GObject obj, float duration = 0.3f, float delay = 0)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
obj.alpha = 1;
|
|
return obj.TweenFade(0f, duration).SetDelay(delay);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static float clickTime;
|
|
|
|
public static bool IsClickNoQuick()
|
|
{
|
|
if (Time.time < clickTime)
|
|
{
|
|
clickTime = Time.time + 0.1f;
|
|
return false;
|
|
}
|
|
|
|
clickTime = Time.time + 0.3f;
|
|
return true;
|
|
}
|
|
|
|
public static void InitGButton(GObject button, Action action, bool isNetworkCheck = false,
|
|
bool isQuickClickCheck = true)
|
|
{
|
|
button?.onClick.Set(() =>
|
|
{
|
|
if (isQuickClickCheck && !IsClickNoQuick())
|
|
{
|
|
return;
|
|
}
|
|
|
|
action?.Invoke();
|
|
});
|
|
}
|
|
|
|
public static string TimeFormat(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 string FormatTime(int second,
|
|
CountDownType countDownType = CountDownType.Second, string dayFormat = "")
|
|
{
|
|
StringBuilder 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 void RandomSortList<T>(List<T> list)
|
|
{
|
|
System.Random r = new System.Random();
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
int index = r.Next(list.Count);
|
|
(list[i], list[index]) = (list[index], list[i]);
|
|
}
|
|
}
|
|
public static int RandomInt(this IList<int> array)
|
|
{
|
|
return Random.Range(array[0], array[1]);
|
|
}
|
|
public static int RandomRange(int[] range)
|
|
{
|
|
if (range == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (range.Length == 2)
|
|
{
|
|
return RandomRange(range[0], range[1]);
|
|
}
|
|
else
|
|
{
|
|
return range[0];
|
|
}
|
|
}
|
|
|
|
public static int RandomRange(int begin, int end)
|
|
{
|
|
return Random.Range(begin, end);
|
|
}
|
|
|
|
public static float RandomRange(float begin, float end)
|
|
{
|
|
return Random.Range(begin, end);
|
|
}
|
|
|
|
public static T RandomWeight<T>(Dictionary<T, int> weightDic)
|
|
{
|
|
int totalWeight = 0;
|
|
foreach (var weight in weightDic)
|
|
{
|
|
totalWeight += weight.Value;
|
|
}
|
|
|
|
int randomWeight = Random.Range(0, totalWeight);
|
|
|
|
int currWeight = 0;
|
|
foreach (var weight in weightDic)
|
|
{
|
|
currWeight += weight.Value;
|
|
if (randomWeight < currWeight)
|
|
{
|
|
return weight.Key;
|
|
}
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public static int RandomWeight(List<int> weightArray)
|
|
{
|
|
float totalWeight = 0;
|
|
for (var i = 0; i < weightArray.Count; i++) totalWeight += weightArray[i];
|
|
|
|
var randomWeight = Random.Range(0, totalWeight);
|
|
if (randomWeight >= totalWeight) return weightArray.Count - 1;
|
|
|
|
float currWeight = 0;
|
|
for (var i = 0; i < weightArray.Count; i++)
|
|
{
|
|
currWeight += weightArray[i];
|
|
if (randomWeight < currWeight) return i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public static int RandomRangeIncludeEnd(int[] range)
|
|
{
|
|
if (range == null) return 0;
|
|
if (range.Length == 2)
|
|
return RandomRange(range[0], range[1] + 1);
|
|
return range[0];
|
|
}
|
|
|
|
public static bool IsToday(long timestamp)
|
|
{
|
|
// 将时间戳转换为DateTime对象
|
|
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(timestamp).ToLocalTime().DateTime;
|
|
|
|
// 获取今天的日期(不包含时间部分)
|
|
DateTime today = DateTime.Today;
|
|
|
|
//Debug.Log($"dateTime: {dateTime.Date} \n today: {today} \n 是否是今天: {dateTime.Date == today}");
|
|
// 比较日期部分是否相等
|
|
return dateTime.Date == today;
|
|
}
|
|
|
|
public static void CheckAdTimes()
|
|
{
|
|
DateTime today = DateTime.Today;
|
|
|
|
var localTimes = PlayerPrefs.GetString("task_ad_times");
|
|
|
|
if (string.IsNullOrEmpty(localTimes))
|
|
{
|
|
PlayerPrefs.SetString("task_ad_times", today.ToString());
|
|
return;
|
|
}
|
|
|
|
if (localTimes != today.ToString())
|
|
{
|
|
DataMgr.VideoWatchCount.Value = 0;
|
|
SaveData.GetSaveObject().ad_task_record = new List<int>();
|
|
|
|
PlayerPrefs.SetString("task_ad_times", today.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
public static void ShowNumAnim(GTextField text_gold, decimal coin, int id = 102, float time = 1.0f)
|
|
{
|
|
|
|
|
|
var currency = id == 102 ? DataMgr.Ticket.Value : DataMgr.Coin.Value;
|
|
var startNum = currency - coin;
|
|
|
|
if (id == 102)
|
|
{
|
|
DOVirtual.Float((float)startNum, (float)currency, time,
|
|
value => { text_gold.text = $"{GameHelper.Get102Str((decimal)value)}"; }).OnComplete(() => {
|
|
// 动画完成时确保最终值被正确设置
|
|
text_gold.text = $"{GameHelper.Get102Str((decimal)currency)}";
|
|
});
|
|
} else {
|
|
DOVirtual.Float((float)startNum, (float)currency, time,
|
|
value => { text_gold.text = $"{GameHelper.Get101Str((decimal)value)}"; }).OnComplete(() => {
|
|
// 动画完成时确保最终值被正确设置
|
|
text_gold.text = $"{GameHelper.Get101Str((decimal)currency)}";
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} |