Files
unity-application/Assets/Scripts/SpellingBeeController.cs
2023-03-01 17:19:32 +01:00

51 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class SpellingBeeController : MonoBehaviour
{
private string[] words;
public Image image;
// Start is called before the first frame update
void Start() {
TextAsset textFile = Resources.Load<TextAsset>("SpellingBee/words");
string fileContents = textFile.text;
words = fileContents.Split('\n');
Image imageComponent = image.GetComponent<Image>();
// Load the new sprite from the Resources folder
Debug.Log("SpellingBee/images/" + "monke");
Sprite sprite = Resources.Load<Sprite>("SpellingBee/images/monke");
image.sprite = sprite;
Debug.Log(sprite.pixelsPerUnit);
//changeSprite("monke");
}
// 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 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;
}
}