GameTool.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. public static class GameTool
  8. {
  9. //用于获取多层级Dictionary中的value
  10. public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
  11. {
  12. Dictionary<string, object> current = root;
  13. int count = 0;
  14. foreach (string key in path)
  15. {
  16. string rawValue = current[key].ToString();
  17. rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
  18. if (count == path.Length - 1)
  19. {
  20. return rawValue;
  21. }
  22. Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
  23. current = value;
  24. count++;
  25. }
  26. return null;
  27. }
  28. // 输入X=100时候,输出一定是1
  29. // 输入X=0的时候,随机输出 0.5-0.7之间的数值
  30. // X越大,输出的随机数越靠近1.
  31. public static bool Random100Check(float x, float thresHold = 0.6f)
  32. {
  33. //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
  34. //// 原始公式 x/(x+99)=threshold
  35. //if (thresHold <= 0 || thresHold >= 1)
  36. // throw new ArgumentException("threshold must be in (0, 1)");
  37. //if (inputValue<=0 || inputValue > 100)
  38. //{
  39. // throw new ArgumentException("inputValue must be in (0,100)");
  40. //}
  41. //float factor = (99*thresHold)/(1-thresHold);
  42. //float result = (factor + inputValue) / (factor + 100f);
  43. //return result > thresHold;
  44. // 初始化随机数生成器
  45. var random = new System.Random();
  46. // 确保X在0-100范围内
  47. x = Mathf.Clamp(x, 0, 100);
  48. if (x == 100)
  49. {
  50. return true;
  51. }
  52. // 计算随机范围
  53. double minValue = 0.5 + (0.5 * x / 100.0); // 从0.5随X增加线性增长
  54. double maxValue = 0.7 + (0.3 * x / 100.0); // 从0.7随X增加线性增长
  55. // 确保最大值不超过1
  56. maxValue = Math.Min(maxValue, 1.0);
  57. // 在[minValue, maxValue]范围内生成随机数
  58. double result = minValue + (random.NextDouble() * (maxValue - minValue));
  59. return result > thresHold;
  60. }
  61. }