Finally manage to render a word

This commit is contained in:
lvrossem
2023-03-01 21:16:26 +01:00
parent ecd24dce04
commit 95b17fff22
2 changed files with 228 additions and 188 deletions

View File

@@ -1,35 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class ThemeList
{
public class ThemeList {
public Theme[] themes;
}
[System.Serializable]
public class Theme
{
public class Theme {
public string name;
public string description;
public string[] words;
}
public class SpellingBeeController : MonoBehaviour
{
public class SpellingBeeController : MonoBehaviour {
private string[] words;
private ThemeList themeList;
private Theme currentTheme;
private List<GameObject> letters;
public Image image;
// Start is called before the first frame update
void Start() {
loadJson();
theme = themeList[0];
changeSprite("kiwi");
currentTheme = themeList.themes[0];
changeSprite("kiwi");
changeWord("kiwi");
}
// Update is called once per frame
@@ -45,7 +46,46 @@ 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");
// Add a Text component to the new GameObject
Text textComponent = letterBox.AddComponent<Text>();
textComponent.text = Char.ToString(word[i]);
*/
GameObject colorBox = new GameObject("Image with Text");
colorBox.transform.SetParent(gameObject.transform);
// Add an Image component to the new GameObject
Image background = colorBox.AddComponent<Image>();
background.color = Color.red;
background.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");
// Create a new child GameObject to hold the Text component
GameObject letterObject = new GameObject("Text");
letterObject.transform.SetParent(colorBox.transform);
// Add a Text component to the new child GameObject
Text letterComponent = letterObject.AddComponent<Text>();
letterComponent.text = Char.ToString(Char.ToUpper(word[i]));
letterComponent.alignment = TextAnchor.MiddleCenter;
letterComponent.fontSize = 60;
// Set the font of the Text component
letterComponent.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
// Set the position of the parent GameObject
colorBox.transform.position = new Vector2(50 + i * 200, 350);
}
}
void changeSprite(string spriteName) {