我是Unity新手,想制作Sudoku Solver。我已经使用画布中的输入字段设计了输入网格。这些输入字段可以显示我在网格脚本中生成的数字,这些数字将保存求解逻辑。我的问题是,当值更改但无法正确显示时,我想更新循环内的输入字段以显示(将所有输入字段更新为新值)。当前,一切都在循环完成后显示(在下一帧中),但是我需要在循环运行时更新这些值。有什么想法吗?
using System.Collections;
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
public class SudokuGrid : MonoBehaviour
{
public InputField v00, v01,v02, v03, v04, v05, v06, v07, v08;
private int[,] arr = new int[9, 9];
public void Upd()
{
v00.text = arr[0, 0].ToString();
v01.text = arr[0, 1].ToString();
v02.text = arr[0, 2].ToString();
v03.text = arr[0, 3].ToString();
v04.text = arr[0, 4].ToString();
v05.text = arr[0, 5].ToString();
v06.text = arr[0, 6].ToString();
v07.text = arr[0, 7].ToString();
v08.text = arr[0, 8].ToString();
}
void Start()
{
}
void Update()
{
int c = 1;
for (var a = 0; a < 9; a++)
{
for (var b = 0; b < 9; b++)
{
arr[a, b] = c;
c++;
Upd();
//need to update all the inputfields here to display on screen
}
}
}
}
答案 0 :(得分:1)
void Start()
{
StartCoroutine(RevealNumber());
}
IEnumerator RevealNumber()
{
int c = 1;
for (var a = 0; a < 9; a++)
{
for (var b = 0; b < 9; b++)
{
arr[a, b] = c;
c++;
Upd();
//waits for 1 second.
yield return new WaitForSeconds(1);
}
}
}
答案 1 :(得分:0)
首先,您可以使用9x9的InputFields数组,其可读性更高
public InputField[,] InputFields;
然后您可以使用for循环更新所有数组:
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
InputFields[x, y].text = arr[x, y];
}
}
或 您可以使用值更改事件检测字段中的用户输入,以仅更新How to use the "On Value Change" in Unity3D Input Field UI component
这样的更改字段