GameTool.cs 3.5 KB

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