C#使用simpleJSON获得随机对象
我想在下面的json中的BodyPresets中获得一个随机对象。 我之所以使用它,是因为它很容易理解,而且它成功地为我获取了一个字符串C#使用simpleJSON获得随机对象,c#,json,unity3d,simplejson,C#,Json,Unity3d,Simplejson,我想在下面的json中的BodyPresets中获得一个随机对象。 我之所以使用它,是因为它很容易理解,而且它成功地为我获取了一个字符串 { "BodyPresets":[ { "name":"bodypreset1", "height": {"x":0.3, "y":0.5}, "upper_arms&q
{
"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);
}
但是如何做到这一点呢?这是不需要的:
JSONNode preset = BodyPresets[randompreset];
因为
var randompreset = BodyPresets[UnityEngine.Random.Range(0, BodyPresets.Count)];
已获取一个随机对象
多亏了@Ononinfan你能发布错误的全文吗?@Abion47 error CS0121以下方法或属性之间的调用不明确:“JSONNode.this[int]”和“JSONNode.this[string]”RandomizerIn你在哪一行得到这个错误?@Ononinfan at JSONNode preset=BodyPresets[randompreset];但是在这一行中-var randompreset=BodyPresets[UnityEngine.Random.Range(0,BodyPresets.Count)];-你已经得到了随机预设,不是吗?在这一行中,您试图使用对象-JSONNode preset=BodyPresets[randompreset];,获取预设;,奇怪的行为