1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using Unity.Mathematics;
- using UnityEngine;
- public static class GameTool
- {
- //用于获取多层级Dictionary中的value
- public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
- {
- Dictionary<string, object> current = root;
- int count = 0;
- foreach (string key in path)
- {
- string rawValue = current[key].ToString();
- rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
- if (count == path.Length - 1)
- {
- return rawValue;
- }
- Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
- current = value;
- count++;
- }
- return null;
- }
- // 输入X=100时候,输出一定是1
- // 输入X=0的时候,随机输出 0.5-0.7之间的数值
- // X越大,输出的随机数越靠近1.
- public static bool Random100Check(float x, float thresHold = 0.6f)
- {
- //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
- //// 原始公式 x/(x+99)=threshold
- //if (thresHold <= 0 || thresHold >= 1)
- // throw new ArgumentException("threshold must be in (0, 1)");
- //if (inputValue<=0 || inputValue > 100)
- //{
- // throw new ArgumentException("inputValue must be in (0,100)");
- //}
- //float factor = (99*thresHold)/(1-thresHold);
- //float result = (factor + inputValue) / (factor + 100f);
- //return result > thresHold;
- // 初始化随机数生成器
- var random = new System.Random();
- // 确保X在0-100范围内
- x = Mathf.Clamp(x, 0, 100);
- if (x == 100)
- {
- return true;
- }
- // 计算随机范围
- double minValue = 0.5 + (0.5 * x / 100.0); // 从0.5随X增加线性增长
- double maxValue = 0.7 + (0.3 * x / 100.0); // 从0.7随X增加线性增长
- // 确保最大值不超过1
- maxValue = Math.Min(maxValue, 1.0);
- // 在[minValue, maxValue]范围内生成随机数
- double result = minValue + (random.NextDouble() * (maxValue - minValue));
- return result > thresHold;
- }
- }
|