从预定义的集

时间:2015-10-06 08:04:10

标签: c# unity3d

正如标题所说,有没有办法从预定义的集合中提取数学问题并将其输出给用户?

我正在制作一款游戏。我们为0-9,+和 - 符号生成了精灵。

无论如何都会产生数学问题并且他们的精灵附加到gameObject?然后将答案显示为选项?谢谢!

我最初可以想到的是为每个精灵的gameObject赋值,然后将其传递给一个字符串,然后代码将解决它。

2 个答案:

答案 0 :(得分:1)

一种简单的方法是将精灵放在Dictionary

Dictionary<char,GameObject> characters;
...
characters.Add('+', plusSignSprite);
...

然后你可以用字符串

实例化精灵
int i=0;
myString = "5-2=3"; // or take is from an array of strings

foreach(char c in myString.ToCharArray()){
    Instantiate(characters[c], new Vector3(i,0,0), Quaternion.identity);
    i+=characterWidth;
}

答案 1 :(得分:0)

I would try to avoid strings. Start by generating the components of the equation say 5, + and 4. From these work out the answer 9. Then generate the sprites and display. This will work out easier than using a string which you would need to parse. You can build more complex equations using a tree structure if needed.