简体   繁体   English

Unity,如何一次更新一个UI元素

[英]Unity, How to Update UI elements one at a time

I'm new to Unity and wants to make a Sudoku Solver. 我是Unity新手,想制作Sudoku Solver。 I've designed the input grid using input fields in the canvas. 我已经使用画布中的输入字段设计了输入网格。 These input fields can display the numbers I generate in my grid script which will held the solving logic. 这些输入字段可以显示我在网格脚本中生成的数字,这些数字将保存求解逻辑。 My problem is that I want to update the input fields inside the loop to display(update all input fields to new value if there are one) when a value changes but can't get it right. 我的问题是,当值更改但无法正确显示时,我想更新循环内的输入字段以显示(将所有输入字段更新为新值)。 Currently everything is displayed after the loop is done(in the next frame), but I need the values to update while the loop is running. 当前,一切都在循环完成后显示(在下一帧中),但是我需要在循环运行时更新这些值。 Any ideas? 有任何想法吗?

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
            }
        }      
    }
}

This is why Coroutines exist 这就是协程存在的原因

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);
        }
    }      
}

Firstly, you can use 9x9 array of InputFields, its more readable 首先,您可以使用9x9的InputFields数组,其可读性更高

public InputField[,] InputFields;

Then you can update all the array using for loop: 然后,您可以使用for循环更新所有数组:

for (int x = 0; x < 9; x++)
{
    for (int y = 0; y < 9; y++)
    {
        InputFields[x, y].text = arr[x, y];
    }
}      

OR you can detect user input in fields using on value change events to update only changed fields like this How to use the "On Value Change" in Unity3D Input Field UI component 或者,您可以使用值更改事件来检测字段中的用户输入,以仅更新这样的更改字段。 如何在Unity3D输入字段UI组件中使用“值更改”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM