首页 > 解决方案 > 如何检查玩家是否在输入字段中写了一些东西?

问题描述

我需要玩家先写下他的名字,然后再点击统一的预制按钮上的播放。

public void GetNextScene()
{
    if (InputPlayerName != null)
    {
        _playerName = InputPlayerName.text;
        SceneManager.LoadScene(NextScene);
    }
    else
    {
        Debug.Log("Please, write your name first");
    }
}

标签: c#unity3d

解决方案


您可能希望在播放器更改输入字段中的文本时动态启用和禁用按钮。

您必须使用 onValueChanged 事件,如果您使用常规输入字段,则为事件,如果您使用 TextMesh Pro,则为该事件。

这里的例子:

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class Example : MonoBehaviour
{
    public InputField mainInputField;

    public void Start()
    {
        //Adds a listener to the main input field and invokes a method when the value changes.
        mainInputField.onValueChanged.AddListener(delegate {ValueChangeCheck(); });
    }

    // Invoked when the value of the text field changes.
    public void ValueChangeCheck()
    {
        Debug.Log("Value Changed");
    }
}

推荐阅读