Files
unity-application/Assets/SpellingBee/Scripts/SpellingBeeThemeSelectionController.cs
Dries Van Schuylenbergh ffa28e16dd Resolve WES-49 "Courses list"
2023-03-04 20:08:28 +00:00

68 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class SpellingBeeThemeSelectionController : MonoBehaviour
{
private int buttonHeight = 140;
private int buttonWidth = 600;
private int canvasWidth = 1920;
// Start is called before the first frame update
void Start()
{
ThemeList themeList = ThemeLoader.loadJson();
for (int i = 0; i < themeList.themes.Length; i++)
{
Theme theme = themeList.themes[i];
// First, you need to create a new button game object
GameObject newButton = new GameObject("Button - " + theme.name);
newButton.transform.SetParent(gameObject.transform);
// Then, add the button component to the game object
Button buttonComponent = newButton.AddComponent<Button>();
buttonComponent.onClick.AddListener(() => OnButtonClick(theme.name));
GameObject colorBox = new GameObject("Letter box");
colorBox.transform.SetParent(newButton.transform);
Image background = colorBox.AddComponent<Image>();
background.rectTransform.sizeDelta = new Vector2(buttonWidth, buttonHeight);
background.color = Color.black;
background.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UIMask.psd");
// Create the text game object
GameObject textObject = new GameObject("Text - " + theme.name);
textObject.transform.SetParent(newButton.transform);
// Set the position of the button game object
int xPos = (int)(i % 2 == 0 ? canvasWidth * 0.25f : canvasWidth * 0.75f);
int yPos = (1 + i / 2) * 180;
newButton.transform.position = new Vector2(xPos, yPos);
// Add the text component to the game object
Text textComponent = textObject.AddComponent<Text>();
textComponent.alignment = TextAnchor.MiddleCenter;
textComponent.color = new Color(50, 50, 50);
textComponent.text = theme.name;
textComponent.rectTransform.sizeDelta = new Vector2(buttonWidth, buttonHeight);
textComponent.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
textComponent.fontSize = 36;
}
}
void OnButtonClick(string clickedTheme)
{
PlayerPrefs.SetString("themeName", clickedTheme);
SceneManager.LoadScene("SpellingBee");
}
}