GameTool.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. try
  19. {
  20. string rawValue = current[key].ToString();
  21. rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
  22. if (count == path.Length - 1)
  23. {
  24. return rawValue;
  25. }
  26. Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
  27. current = value;
  28. count++;
  29. }
  30. catch (Exception ex)
  31. {
  32. // 如果在访问Json的某个key时发生错误,打印错误信息并返回null
  33. // 这有助于调试和了解哪个key导致了问题
  34. // 注意:在生产环境中,可能需要更细致的错误处理策略
  35. Debug.LogWarning($"Error accessing key '{key}' in path '{string.Join(".", path)}': {ex.Message}");
  36. }
  37. }
  38. return null;
  39. }
  40. // 输入X=100时候,输出一定是1
  41. // 输入X=0的时候,随机输出 0.5-0.7之间的数值
  42. // X越大,输出的随机数越靠近1.
  43. public static bool Random100Check(float x, float thresHold = 0.6f)
  44. {
  45. //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
  46. //// 原始公式 x/(x+99)=threshold
  47. //if (thresHold <= 0 || thresHold >= 1)
  48. // throw new ArgumentException("threshold must be in (0, 1)");
  49. //if (inputValue<=0 || inputValue > 100)
  50. //{
  51. // throw new ArgumentException("inputValue must be in (0,100)");
  52. //}
  53. //float factor = (99*thresHold)/(1-thresHold);
  54. //float result = (factor + inputValue) / (factor + 100f);
  55. //return result > thresHold;
  56. // 初始化随机数生成器
  57. var random = new System.Random();
  58. // 确保X在0-100范围内
  59. x = Mathf.Clamp(x, 0, 100);
  60. if (x == 100)
  61. {
  62. return true;
  63. }
  64. // 计算随机范围
  65. double minValue = 0.4 + (0.6 * x / 100.0); // 从0.4随X增加线性增长
  66. double maxValue = 0.8 + (0.2 * x / 100.0); // 从0.8随X增加线性增长
  67. // 确保最大值不超过1
  68. maxValue = Math.Min(maxValue, 1.0);
  69. // 在[minValue, maxValue]范围内生成随机数
  70. double result = minValue + (random.NextDouble() * (maxValue - minValue));
  71. return result > thresHold;
  72. }
  73. // 检测新的狗名是否符合重名
  74. public static bool NewDogNameAllowed(string newDogName)
  75. {
  76. bool dogNameAllowed = true;
  77. foreach (var dog in UserProperty.dogs)
  78. {
  79. if (newDogName == dog.dog_name)
  80. {
  81. dogNameAllowed = false;
  82. }
  83. }
  84. return dogNameAllowed;
  85. }
  86. // 恢复游戏时间运行
  87. public static void ResumeGameTime()
  88. {
  89. Time.timeScale = 1;
  90. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  91. }
  92. // 暂停游戏时间运行
  93. public static void PauseGameTime()
  94. {
  95. Time.timeScale = 0;
  96. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  97. }
  98. public static bool IntBetween(int min, int max, int value)
  99. {
  100. if (value >= min && value < max)
  101. {
  102. return true;
  103. }
  104. else
  105. {
  106. return false;
  107. }
  108. }
  109. public static bool IsValidEmail(string email)
  110. {
  111. try
  112. {
  113. var addr = new MailAddress(email);
  114. return addr.Address == email;
  115. }
  116. catch
  117. {
  118. return false;
  119. }
  120. }
  121. public static void ReloadCurrentScene()
  122. {
  123. // 获取当前场景的名称
  124. string currentSceneName = SceneManager.GetActiveScene().name;
  125. // 使用LoadScene方法重新加载当前场景
  126. SceneManager.LoadScene(currentSceneName);
  127. }
  128. }