Get all words to render correctly

This commit is contained in:
lvrossem
2023-03-01 22:09:55 +01:00
parent 95b17fff22
commit 51f8cc169c
2 changed files with 544 additions and 27 deletions

View File

@@ -29,8 +29,11 @@ public class SpellingBeeController : MonoBehaviour {
void Start() {
loadJson();
currentTheme = themeList.themes[0];
changeSprite("kiwi");
changeWord("kiwi");
int randomIndex = UnityEngine.Random.Range(0, words.Length - 1);
string randomWord = words[randomIndex];
changeSprite(randomWord);
changeWord(randomWord);
}
// Update is called once per frame
@@ -46,22 +49,21 @@ public class SpellingBeeController : MonoBehaviour {
}
void changeWord(string word) {
for (int i = 0; i < word.Length; i++) {
/*
Debug.Log("Yello");
GameObject letterBox = new GameObject();
Debug.Log("Yello");
// Add an Image component to the new GameObject
Image image = letterBox.AddComponent<Image>();
Debug.Log("Yello");
letterBox.color = Color.black;
Debug.Log("Yello");
int length = word.Length;
// Add a Text component to the new GameObject
Text textComponent = letterBox.AddComponent<Text>();
textComponent.text = Char.ToString(word[i]);
*/
int center = 960;
int canvasWidth = 1920;
int maxWidth = 1080;
int[] xvalues = new int[length];
int spacing = maxWidth / (length - 1);
for (int i = 0; i < length; i++) {
xvalues[i] = (canvasWidth - maxWidth) / 2 + i * spacing;
}
for (int i = 0; i < length; i++) {
GameObject colorBox = new GameObject("Image with Text");
colorBox.transform.SetParent(gameObject.transform);
@@ -84,7 +86,7 @@ public class SpellingBeeController : MonoBehaviour {
letterComponent.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
// Set the position of the parent GameObject
colorBox.transform.position = new Vector2(50 + i * 200, 350);
colorBox.transform.position = new Vector2(xvalues[i], 450);
}
}
@@ -92,9 +94,8 @@ public class SpellingBeeController : MonoBehaviour {
Image imageComponent = image.GetComponent<Image>();
// Load the new sprite from the Resources folder
Debug.Log("SpellingBee/images/" + spriteName);
Sprite sprite = Resources.Load<Sprite>("SpellingBee/images/" + spriteName);
Debug.Log(sprite.pixelsPerUnit);
// Set the new sprite as the Image component's source image
image.sprite = sprite;
}
@@ -102,12 +103,7 @@ public class SpellingBeeController : MonoBehaviour {
void loadJson() {
TextAsset themeJson = Resources.Load<TextAsset>("SpellingBee/words");
string jsonString = themeJson.text;
Debug.Log(jsonString);
themeList = JsonUtility.FromJson<ThemeList>(jsonString);
foreach (Theme theme in themeList.themes)
{
Debug.Log(theme.description);
}
words = themeList.themes[UnityEngine.Random.Range(0, themeList.themes.Length - 1)].words;
}
}