в поле ввода ничего не написано => кнопка не может быть нажата - PullRequest
0 голосов
/ 15 апреля 2019

знает ли кто-нибудь скрипт, который, если ничего не было написано в поле ввода => кнопка не может быть нажата, и он покажет сообщение на экране

C # unity 2d

Ответы [ 2 ]

0 голосов
/ 15 апреля 2019

Вы можете использовать OnValueChanged , который вызывается после каждого изменения (добавления или удаления) символа.

На поле ввода добавьте

[RequireComponent(typeof(InputField))]
public class InputValidator : MonoBehaviour
{
    // Here reference the according Button
    // Via the Inspector or script
    public Button targetButton;

    // As little bonus if you want here reference an info box why the input is invalid
    public GameObject infoBox;

    private InputField inputField;

    private void Awake()
    {
        inputField = GetComponent<InputField>();

        // Add callback
        inputField.onValueChanged.AddListener(ValidateInput);
    }

    // Additionally validate the input value everytime 
    // The InputField (this component to be exact) gets enabled in the scene
    private void OnEnable()
    {
        ValidateInput(inputField.text);
    }

    private void ValidateInput(string input)
    {
        // Here you could implement some replace or further validation logic
        // if e.g. only certain characters shall be allowed

        // Enable the button only if some valid input is available
        targetButton.interactable = !string.IsNullOrWhiteSpace(input);

        // just a bonus if you want to show an info box why input is invalid
        if (infoBox) infoBox.SetActive(string.IsNullOrWhiteSpace(input));
    }
}

enter image description here

0 голосов
/ 15 апреля 2019

Вы можете использовать метод InputField.onValueChanged из: https://docs.unity3d.com/ScriptReference/UI.InputField-onValueChanged.html

Затем вы проверяете, является ли InputField.Text пустым, затем отключаете кнопку, иначе включите ее.

Чтобы отключить кнопкуустановите button.interactable = true/false

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...