Hi,
I followed a youtube tutorial to create the wordle game. It works fine in the play mode in my Desktop(the tutorial is for the PC)
I added some code to the scripts, to activate android keyboard, and switched to android platform, but when I run the application on my android phone, the keyboard doesnt show up. Will the code below map the enetered letters of the android keyboard to the tiles, just like it works for PC build?
here’s my script:
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class Board : MonoBehaviour
{
private static readonly KeyCode[] SUPPORTED_KEYS = new KeyCode[]
{
KeyCode.A, KeyCode.B, KeyCode.C, KeyCode.D, KeyCode.E,
KeyCode.F, KeyCode.G, KeyCode.H, KeyCode.I,
KeyCode.J, KeyCode.K, KeyCode.L,
KeyCode.M, KeyCode.N, KeyCode.O, KeyCode.P,
KeyCode.Q, KeyCode.R, KeyCode.S, KeyCode.T,
KeyCode.U, KeyCode.V, KeyCode.W,
KeyCode.V, KeyCode.X, KeyCode.Y,
KeyCode.Z,
};
private Row[] rows;
private string[] solutions;
private string[] validWords;
public string word;
private int rowIndex;
private int columnIndex;
private TileFlipper tileFlipper;
public AnimationCurve flipCurve;
[Header("UI")]
public TextMeshProUGUI invalidWordText;
public Button newWordButton;
public Button tryAgainButton;
private TouchScreenKeyboard keyboard;
[Header("States")]
public Tile.State emptyState;
public Tile.State occupiedState;
public Tile.State correctState;
public Tile.State wrongSpotState;
public Tile.State incorrectState;
private void Awake()
{
rows = GetComponentsInChildren<Row>();
tileFlipper = GetComponent<TileFlipper>();
}
private void Start()
{
LoadData();
NewGame();
}
public void NewGame()
{
ClearBoard();
SetRandomWord();
enabled = true;
}
public void TryAgain()
{
ClearBoard();
enabled = true;
}
public void BtnHome()
{
SceneManager.LoadScene("Home");
}
private void LoadData()
{
TextAsset textFile = Resources.Load("official_wordle_all") as TextAsset;
validWords = textFile.text.Split('\n');
textFile = Resources.Load("official_wordle_common") as TextAsset;
solutions = textFile.text.Split('\n');
}
private void SetRandomWord()
{
word = solutions[Random.Range(0, solutions.Length)];
word = word.ToLower().Trim();
}
private void Update()
{
Row currentRow = rows[rowIndex];
if (Input.GetKeyDown(KeyCode.Backspace))
{
columnIndex = Mathf.Max(columnIndex - 1, 0);
currentRow.tiles[columnIndex].SetLetter('\0');
currentRow.tiles[columnIndex].SetState(emptyState);
invalidWordText.gameObject.SetActive(false);
}
else if (columnIndex >= currentRow.tiles.Length)
{
if (Input.GetKeyDown(KeyCode.Return))
{
SubmitRow(currentRow);
}
}
else
{
for (int i = 0; i < SUPPORTED_KEYS.Length; i++)
{
if (Input.GetKeyDown(SUPPORTED_KEYS[i]))
{
currentRow.tiles[columnIndex].SetLetter((char)SUPPORTED_KEYS[i]);
currentRow.tiles[columnIndex].SetState(occupiedState);
columnIndex++;
break;
}
}
if (TouchScreenKeyboard.isSupported && (keyboard == null || !keyboard.active))
{
if (TouchScreenKeyboard.visible && TouchScreenKeyboard.area.Contains(Input.mousePosition))
{
keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, false);
}
}
if (keyboard != null && keyboard.status == TouchScreenKeyboard.Status.Done)
{
string input = keyboard.text;
if (!string.IsNullOrEmpty(input))
{
char letter = input[0];
currentRow.tiles[columnIndex].SetLetter(letter);
currentRow.tiles[columnIndex].SetState(occupiedState);
columnIndex++;
}
keyboard = null;
}
}
}
private void SubmitRow(Row row)
{
if (!IsValidWord(row.word))
{
invalidWordText.gameObject.SetActive(true);
return;
}
string remaining = word;
for (int i = 0; i < row.tiles.Length; i++)
{
Tile tile = row.tiles[i];
TileFlipper tileFlipper = tile.GetComponent<TileFlipper>();
if (tileFlipper != null)
{
tileFlipper.FlipTile();
tile.SetLetter(tile.letter);
}
if (tile.letter == word[i])
{
if (remaining.Contains(tile.letter.ToString()))
{
tile.SetState(correctState);
remaining = remaining.Remove(i, 1);
remaining = remaining.Insert(i, " ");
}
else
{
tile.SetState(incorrectState);
}
}
else if (!word.Contains(tile.letter.ToString()))
{
tile.SetState(incorrectState);
}
}
for (int i = 0; i < row.tiles.Length; i++)
{
Tile tile = row.tiles[i];
if (tile.state != correctState && tile.state != incorrectState)
{
if (tile.state != correctState && tile.state != incorrectState)
{
if (remaining.Contains(tile.letter.ToString()))
{
tile.SetState(wrongSpotState);
int index = remaining.IndexOf(tile.letter);
remaining = remaining.Remove(index, 1);
remaining = remaining.Insert(index, " ");
}
else
{
tile.SetState(incorrectState);
}
}
}
}
if (HasWon(row))
{
enabled = false;
}
rowIndex++;
columnIndex = 0;
if (rowIndex >= rows.Length)
{
enabled = false;
}
}
private IEnumerator FlipTile(Tile tile, Vector3 originalScale, float duration)
{
float t = 0f;
Vector3 startScale = tile.transform.localScale;
Vector3 targetScale = new Vector3(originalScale.x, -originalScale.y, originalScale.z);
while (t < duration)
{
t += Time.deltaTime;
float normalizedTime = Mathf.Clamp01(t / duration);
float flipProgress = flipCurve.Evaluate(normalizedTime);
tile.transform.localScale = Vector3.Lerp(startScale, targetScale, flipProgress);
yield return null;
}
tile.transform.localScale = targetScale;
}
private void ClearBoard()
{
for (int row = 0; row < rows.Length; row++)
{
for (int col = 0; col < rows[row].tiles.Length; col++)
{
rows[row].tiles[col].SetLetter('\0');
rows[row].tiles[col].SetState(emptyState);
}
rowIndex = 0;
columnIndex = 0;
}
}
private bool IsValidWord(string word)
{
for (int i = 0; i < validWords.Length; i++)
{
if (validWords[i] == word)
{
return true;
}
}
return false;
}
private bool HasWon(Row row)
{
for (int i = 0; i < row.tiles.Length; i++)
{
if (row.tiles[i].state != correctState)
{
return false;
}
}
return true;
}
private void OnEnable()
{
invalidWordText.gameObject.SetActive(false);
newWordButton.gameObject.SetActive(false);
tryAgainButton.gameObject.SetActive(false);
}
private void OnDisable()
{
if (HasWon(rows[rows.Length - 1]))
{
newWordButton.gameObject.SetActive(true);
tryAgainButton.gameObject.SetActive(false);
}
}
public void QuitGame()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}