Hi,
How can I display the android keyboard in my app when I launch the app in my phone? When I first open the app, the keyboard is displayed, but when I type, the text doesnt get mapped to the tiles, and when I press enter, th ekeyboard closes and there's no way to open it back. I have 4 scripts,the Board script contains the game logic. It builds successfully, but when I switch the platform to android and install the apk in my android device, Ineed to be able to get the touchscreen key inputs. Script below:
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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,
};//the array for detecting the different letters is a constant, syntax is to use capital letters to
declare
private Row[] rows;//the array of rows for the board
private string[] solutions;//to pick random solution which will compar against the guess words
private string[] validWords;//the guesses
public string word;//the random word
private int rowIndex;
private int columnIndex;//like 00,01,02,03 for the tiles
private TileFlipper tileFlipper;
//public float flipDuration = 0.2f;
public AnimationCurve flipCurve;
[Header("UI")]
public TextMeshProUGUI invalidWordText;
public Button newWordButton;
public Button tryAgainButton;
private TouchScreenKeyboard keyboard;
[Header("States")]//is used to organize things and it shows up as a label for the tile states in play
mode
public Tile.State emptyState;// tile is blank
public Tile.State occupiedState;
public Tile.State correctState;//tile will show up green
public Tile.State wrongSpotState;//tile will show up yellow
public Tile.State incorrectState;//tile will be greyed out
private void Awake()
{
rows = GetComponentsInChildren();
tileFlipper = GetComponent();
}
private void Start()
{
LoadData();
NewGame();
}
public void NewGame()
{
ClearBoard();
SetRandomWord();//to start the game again
enabled = true;//re-enable the script for the new game
}
public void TryAgain()
{
ClearBoard();
enabled = true;// re enable the script
}
public void QuitGame()
{
Application.Quit();
}
private void LoadData()
{
//load the words from the Resources folder
TextAsset textFile = Resources.Load("official_wordle_all") as TextAsset;//all is the valid words
validWords = textFile.text.Split('\n');//getting the entire string of text in the file, and seperated
by new line
textFile = Resources.Load("official_wordle_common") as TextAsset;//common is the solutions
solutions = textFile.text.Split('\n');//getting the entire string of text in the file, and seperated by
new line
}
private void SetRandomWord()
{
word = solutions[Random.Range(0, solutions.Length)];//to pick a random word from the solutions
array
word = word.ToLower().Trim();//to compare and see if its in lowercase and remove spaces
}
private void Update()
{
Row currentRow = rows[rowIndex];
if (Input.GetKeyDown(KeyCode.Backspace))
{
// Handle 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)
{
// Submit row
if (Input.GetKeyDown(KeyCode.Return))
{
SubmitRow(currentRow);
}
}
else
{
// Check for key press
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;
}
}
// Check for touch input
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)
{
//first check if the word is valid
if (!IsValidWord(row.word))//if the word in the row is not valid
{
invalidWordText.gameObject.SetActive(true);
return;
}
//indicate correct and incorrect
string remaining = word;//copy of the random word
for(int i = 0; i < row.tiles.Length; i++)
{
Tile tile = row.tiles[i];
//Apply the flip effect
TileFlipper tileFlipper = tile.GetComponent();
if(tileFlipper !=null)
{
tileFlipper.FlipTile();
}
if (tile.letter == word[i])//correct
{
if(remaining.Contains(tile.letter.ToString()))
{
tile.SetState(correctState);
remaining = remaining.Remove(i, 1);//remove the character in the random word
that is correct
remaining = remaining.Insert(i, " ");//replace the character in the copy string with
a space. it can be anything except a letter
}
else
{
tile.SetState(incorrectState);
}
}
else if(!word.Contains(tile.letter.ToString()))
{
tile.SetState(incorrectState);
}
}
//seperate for loop for wrong spot
for(int i=0;i= rows.Length)
{
enabled = false;//disable the script
}
}
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);
// Lerp between start and target scale
tile.transform.localScale = Vector3.Lerp(startScale, targetScale, flipProgress);
yield return null;
}
// Ensure the tile ends up with the correct scale
tile.transform.localScale = targetScale;
}
private void ClearBoard()//slips through every row and tile and sets it back to no character
{
for(int row=0; row
↧