Первоначально мне удалось создать что-то такое, что будет содержать вопрос (в массиве) и ответы (в массиве), и каждому из них будут назначаться случайные текстовые значения, причем одно из них будет правильным. Учитывая природу этого, в частности масштаб, я понял, что словарь будет более эффективным. Поэтому я попытался манипулировать своим кодом и включить его в словарь. Однако, сделав это, кажется, что код не работает, в частности, потому что «индекс массива выходит за пределы диапазона» (я использовал массив int для нахождения ключей), и поэтому ключи не упоминаются и, таким образом, данные не выходят. Я задавался вопросом, сможет ли кто-нибудь помочь мне с этой проблемой.
Для этого я сделал 1 скрипт вопроса и один словарь. Идея заключалась в том, что я вернул бы значение из словаря, которое затем было бы вставлено в текст вопроса.
(рассматриваемый словарь - это вопросы один ... другой - для кнопок, но я предполагаю, что после получения ответа на вопросы, кнопки должны быть довольно похожими, если не одинаковыми)
словарь
public class buttonDictionary : MonoBehaviour
{
public Dictionary<int, string> buttonA = new Dictionary<int, string>();
public Dictionary<int, string> questions = new Dictionary<int, string>();
public static int correctinput;
public static int index;
public static int key;
public static string answer1;
public int[] keys;
public static string answer;
public int wrongIndex;
private string wrongAnswer;
public static buttonDictionary arrays;
public static string question;
public static int correctButton;
// Use this for initialization
void Start()
{
// Adds all of the answers to this scene into the dictionary
buttonA.Add(0, "I");
buttonA.Add(1, "Like");
buttonA.Add(2, "Dogs");
buttonA.Add(3, "If");
buttonA.Add(4, "They");
buttonA.Add(5, "Like");
buttonA.Add(6, "Me");
buttonA.Add(7, "You");
buttonA.Add(8, "QDW");
buttonA.Add(9, "QDWQ");
// Adds all of the questions to the dictionary
questions.Add(0, "d");
questions.Add(1, "f");
questions.Add(2, "f");
questions.Add(3, "g");
questions.Add(4, "ff");
questions.Add(5, "gg");
questions.Add(6, "ff");
questions.Add(7, "gg");
questions.Add(8, "hh");
questions.Add(9, "ff");
// Update is called once per frame
}
void Awake()
{
arrays = this;
// Copies all keys in the dictionary into an array and generates a random number, then generates a key
int[] keys = new int[buttonA.Count];
buttonA.Keys.CopyTo(keys, 0);
int index = Random.Range(0, keys.Length);
GetAnswer1();
GetQuestion1();
}
void Update()
{
if (Input.GetKey("escape"))
Application.Quit();
}
// gets an index that refers to the key
public int He()
{
int index = Random.Range(0, keys.Length);
return index;
}
// Gets the question
public string GetQuestion1()
{
var key = keys[index];
question = questions[key];
return question;
}
поле с вопросом
public class Question : MonoBehaviour
{
// Use this for initialization
void Start()
{
SetText();
}
// Update is called once per frame
void Update()
{
}
void SetText()
{
GameObject textBox = gameObject;
textBox.GetComponent<Text>().text = buttonDictionary.question;
}
}