我想在下面的json中获得BodyPresets中的一个随机对象。我之所以使用它,是因为它简单易懂,并且成功地为我获取了一个字符串。
{
"BodyPresets":[
{
"name":"bodypreset1",
"height": {"x":0.3, "y":0.5},
"upper_arms": {"x":0.3, "y":0.5},
"lower_arms": {"x":0.3, "y":0.5}
},
{
"name":"bodypreset2",
"height": {"x":0.5, "y":0.7},
"upper_arms": {"x":0.5, "y":0.7},
"lower_arms": {"x":0.5, "y":0.7}
},
{
"name":"bodypreset3",
"height": {"x":0.7, "y":0.9},
"upper_arms": {"x":0.7, "y":0.9},
"lower_arms": {"x":0.7, "y":0.9}
}
]
}
在下面的C#代码中,我尝试了,但只得到了一个CS0121错误:
using SimpleJSON;
void RandomiseCharBody()
{
var dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configFilename = "Randomizer.json";
var configPath = Path.Combine(dllPath, configFilename);
//// from https://answers.unity.com/questions/1473952/how-to-write-and-read-json-in-unity.html
//// https://github.com/Bunny83/SimpleJSON
string jsonString = File.ReadAllText(configPath);
JSONNode data = JSON.Parse(jsonString);
JSONNode BodyPresets = data["BodyPresets"];
//attempt at gettting a random preset from the list
var randompreset = BodyPresets[UnityEngine.Random.Range(0, BodyPresets.Count)];
JSONNode preset = BodyPresets[randompreset];
float min1 = preset["height"]["x"];
float min2 = preset["upper_arms"]["x"];
float min3 = preset["lower_arms"]["x"];
float max1 = preset["height"]["y"];
float max2 = preset["upper_arms"]["y"];
float max3 = preset["lower_arms"]["y"];
// applies the values
shapeValueBody[0] = UnityEngine.Random.Range(min1, max1);
shapeValueBody[1] = UnityEngine.Random.Range(min2, max2);
shapeValueBody[2] = UnityEngine.Random.Range(min3, max3);
}
但如何做到这一点呢?
发布于 2020-06-27 21:57:45
不需要这样做:
JSONNode preset = BodyPresets[randompreset];
因为
var randompreset = BodyPresets[UnityEngine.Random.Range(0, BodyPresets.Count)];
已经得到了一个随机对象。
感谢@OnionFan
https://stackoverflow.com/questions/62615461
复制相似问题