GameTool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net.Mail;
  5. using System.Text.RegularExpressions;
  6. using Unity.Mathematics;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. public static class GameTool
  10. {
  11. //用于获取多层级Dictionary中的value
  12. public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
  13. {
  14. Dictionary<string, object> current = root;
  15. int count = 0;
  16. foreach (string key in path)
  17. {
  18. string rawValue = current[key].ToString();
  19. rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
  20. if (count == path.Length - 1)
  21. {
  22. return rawValue;
  23. }
  24. Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
  25. current = value;
  26. count++;
  27. }
  28. return null;
  29. }
  30. // 输入X=100时候,输出一定是1
  31. // 输入X=0的时候,随机输出 0.5-0.7之间的数值
  32. // X越大,输出的随机数越靠近1.
  33. public static bool Random100Check(float x, float thresHold = 0.6f)
  34. {
  35. //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
  36. //// 原始公式 x/(x+99)=threshold
  37. //if (thresHold <= 0 || thresHold >= 1)
  38. // throw new ArgumentException("threshold must be in (0, 1)");
  39. //if (inputValue<=0 || inputValue > 100)
  40. //{
  41. // throw new ArgumentException("inputValue must be in (0,100)");
  42. //}
  43. //float factor = (99*thresHold)/(1-thresHold);
  44. //float result = (factor + inputValue) / (factor + 100f);
  45. //return result > thresHold;
  46. // 初始化随机数生成器
  47. var random = new System.Random();
  48. // 确保X在0-100范围内
  49. x = Mathf.Clamp(x, 0, 100);
  50. if (x == 100)
  51. {
  52. return true;
  53. }
  54. // 计算随机范围
  55. double minValue = 0.4 + (0.6 * x / 100.0); // 从0.4随X增加线性增长
  56. double maxValue = 0.8 + (0.2 * x / 100.0); // 从0.8随X增加线性增长
  57. // 确保最大值不超过1
  58. maxValue = Math.Min(maxValue, 1.0);
  59. // 在[minValue, maxValue]范围内生成随机数
  60. double result = minValue + (random.NextDouble() * (maxValue - minValue));
  61. return result > thresHold;
  62. }
  63. // 检测新的狗名是否符合重名
  64. public static bool NewDogNameAllowed(string newDogName)
  65. {
  66. bool dogNameAllowed = true;
  67. foreach (var dog in UserProperty.dogs)
  68. {
  69. if (newDogName == dog.dog_name)
  70. {
  71. dogNameAllowed = false;
  72. }
  73. }
  74. return dogNameAllowed;
  75. }
  76. // 恢复游戏时间运行
  77. public static void ResumeGameTime()
  78. {
  79. Time.timeScale = 1;
  80. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  81. }
  82. // 暂停游戏时间运行
  83. public static void PauseGameTime()
  84. {
  85. Time.timeScale = 0;
  86. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  87. }
  88. public static bool IntBetween(int min, int max, int value)
  89. {
  90. if (value >= min && value < max)
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. public static bool IsValidEmail(string email)
  100. {
  101. try
  102. {
  103. var addr = new MailAddress(email);
  104. return addr.Address == email;
  105. }
  106. catch
  107. {
  108. return false;
  109. }
  110. }
  111. public static void ReloadCurrentScene()
  112. {
  113. // 获取当前场景的名称
  114. string currentSceneName = SceneManager.GetActiveScene().name;
  115. // 使用LoadScene方法重新加载当前场景
  116. SceneManager.LoadScene(currentSceneName);
  117. }
  118. }