114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[System.Serializable]
|
|
public class ThemeList {
|
|
public Theme[] themes;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class Theme {
|
|
public string name;
|
|
public string description;
|
|
public string[] words;
|
|
}
|
|
|
|
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();
|
|
currentTheme = themeList.themes[0];
|
|
changeSprite("kiwi");
|
|
changeWord("kiwi");
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update() {
|
|
/*
|
|
int randomIndex = UnityEngine.Random.Range(0, words.Length - 1);
|
|
string randomWord = words[randomIndex];
|
|
|
|
changeSprite(randomWord);
|
|
|
|
Debug.Log(randomWord);
|
|
*/
|
|
}
|
|
|
|
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) {
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|