Я в целом начинающий программист на C #, пытаюсь создать в Unity игру на японском языке с падающим словом, где слово / буква, отображаемая на экране, будет написана на хирагане, но запрошенный ввод будет выполнен буквами ромадзи (алфавит).
В настоящее время я застрял в колее, пытаясь выяснить, как сгенерировать случайное число один раз для слов. Идет добавление (слово). Например, когда создается объект Word, генерируется случайное число. Затем это случайное число используется в классах, которые зависят от него, таких как getWord_Hiragana и getWord_Romaji. В большинстве игр, набираемых в Интернете, отображается только один объект (английский), поэтому я не смог найти то, что мне нужно.
// WordManager.cs
public class WordManager : MonoBehaviour {
public List<Word> words;
public WordSpawner wordSpawner;
public void AddWord ()
{
Word word = new Word (WordGenerator.GetWord_Romaji(), wordSpawner.SpawnWord());
words.Add (word);
}
}
// WordGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WordGenerator : MonoBehaviour {
public static string[] wordList_Hiragana = { "あ", "い", "う", "え", "お" };
public static string[] wordList_Katakana = { "ア", "イ", "ウ", "エ", "オ" };
public static string[] wordList_Romaji = { "a", "i", "u", "e", "o" };
public static int GetIndex ()
{
int index = Random.Range (0, wordList_Romaji.Length - 1); // Get Random number which has the same index for Hiragana, Katakana, and Romaji arrays
Debug.Log ("Index #" + index + ": " + wordList_Hiragana[index] + " " + wordList_Katakana[index] + " " + wordList_Romaji[index]); // Debug Log
return index; // Returns the result of the random as a guidance.
}
public static string GetWord_Hiragana () // A function to return the result of GetIndex as Hiragana word to be used on WordManager and in turn, displays that Hiragana.
{
int index = GetIndex ();
string getWord_Hiragana = wordList_Hiragana [index];
return getWord_Hiragana;
}
public static string GetWord_Romaji ()
{
int index = GetIndex ();
string getWord_Romaji = wordList_Romaji [index];
return getWord_Romaji;
}
}
// Word.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Word {
public string word;
private int typeIndex; // Checks for current letter
WordDisplay display;
public Word (string _word, WordDisplay _display) // Displays the word as Hiragana / Katakana
{
word = _word;
display = _display;
display.SetWord (word);
}
public char GetNextLetter ()
{
return word[typeIndex]; // Gets the next letter of the Romaji array
}
public void TypeLetter ()
{
typeIndex++;
}
public bool WordTyped ()
{
bool wordTyped = (typeIndex >= word.Length); // Checks if the whole word has been typed
if (wordTyped)
{
display.RemoveWord (); // Remove the whole object on screen
}
return wordTyped;
}
}
Ожидаемый результат для GetIndex, чтобы бросить случайное число один раз для объекта Word. Когда getWord_Romaji выполняется, он получает возвращаемое значение GetIndex. То же самое происходит, когда выполняется getWord_Hiragana. Прямо сейчас GetIndex выполняется дважды и генерирует случайное число дважды в каждом объекте Word, в результате чего слово, появившееся в Debug, отличается от того, которое появляется на экране игры. Как мне сделать эту работу?
Если приведенного выше кода недостаточно для решения проблемы, проект размещается здесь .