Files
unity-application/Assets/Scripts/SpellingBeeThemeSelectionController.cs
2023-03-02 15:24:34 +01:00

89 lines
3.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SpellingBeeThemeSelectionController : MonoBehaviour {
public Button buttonPrefab;
public Transform parentTransform;
private int buttonHeight = 140;
private int buttonWidth = 600;
// Start is called before the first frame update
void Start()
{
int canvasWidth = 1920;
int canvasHeight = 1080;
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>();
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");
// Set the position of the button game object
// Create the text game object
GameObject textObject = new GameObject("Text - " + theme.name);
textObject.transform.SetParent(newButton.transform);
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;
// Set the text content and font size
textComponent.color = new Color(50, 50, 50);
textComponent.text = theme.name;
textComponent.rectTransform.sizeDelta = new Vector2(buttonWidth, buttonHeight);
// Set the parent of the text game object to the button game object
textComponent.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
textComponent.fontSize = 36;
buttonComponent.onClick.AddListener(() => OnButtonClick(theme.name));
// Set the size and position of the text game object
/*
RectTransform textTransform = textObject.GetComponent<RectTransform>();
textTransform.sizeDelta = new Vector2(200, 50);
textTransform.anchoredPosition = Vector2.zero;
// Set the parent of the button game object to the canvas
newButton.transform.SetParent(canvas.transform, false);
*/
}
}
void OnButtonClick(string clickedTheme)
{
Debug.Log("Button " + clickedTheme + " clicked!");
PlayerPrefs.SetString("themeName", clickedTheme);
ChangeSceneOnClick.loadScene("SpellingBee");
}
// Update is called once per frame
void Update()
{
}
}