如何使用“On Value Change"在 Unity3D 输入字段 UI 组件中 [英] How to use the "On Value Change" in Unity3D Input Field UI component

查看:151
本文介绍了如何使用“On Value Change"在 Unity3D 输入字段 UI 组件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关如何使用 Unity3d 的教程,但我遇到了死胡同.

I'm following a tutorial on how to work with Unity3d and I've hit a dead end.

我相信在较新版本的 Unity 中发生了一些变化,因为本教程似乎按照我的方式工作得很好.

I believe something changed in a newer version of Unity since the tutorial seems to work nice the way I'm doing it.

我有一个输入字段 UI 组件,我想在每次更改它时调用 C# 函数.

I have an Input Field UI component that I want to call a C# function every time I change it.

根据教程,我只需要使用输入字段(脚本)的On Value Change"属性并告诉它调用一些将 string 作为参数的函数.

According to the tutorial I just have to use the "On Value Change" property of the Input Field (script) and tell it to call some function that takes a string as an argument.

public string playerName;
public void setName (string name)
{
    playerName = name;
    Debug.Log("Set playerName: "+name, gameObject);
    Debug.Log("Get playerName: "+playerName, gameObject);

}

然而,这什么也没做,我的 playerName 属性总是空的,我在 name 中没有收到任何东西.

Yet, this does nothing, my playerName property it's always empty and I don't receive anything in name.

我该怎么做?我已经看到了在 Start() 函数中设置侦听器的答案,然后在此处使用 UnityEvent :从输入字段获取文本

How do I go about doing this? I've seen an answer setting up a listener in the Start() function, and then using an UnityEvent in here: Get text from Input field

但是有没有另一种方法可以使用 Unity3d 图形编辑器来做到这一点,而不需要编写这么多代码?

But is there another way to do this using the Unity3d graphical editor that doesn't involve writing so much code?

推荐答案

是的,您可以通过检查器添加事件处理程序.选择 InputField 游戏对象并向下滚动到检查器中 InputField 部分的底部.单击 + 添加新的事件处理程序,然后选择要调用的接收游戏对象和方法.

Yes you can add event handlers via the inspector. Select the InputField game object and scroll down to the bottom of the InputField section in the inspector. Click + to add a new event handler then select the receiving game object and method you want to call.

使用函数的动态字符串版本将输入字符串作为参数传递.静态参数回调让您可以在检查器中设置函数参数,这在响应 InputField 更改时可能不是您想要的.

Use the Dynamic string version of the function to pass the input string as a parameter. The Static Parameters callbacks let you set the function parameter in the inspector, which likely isn't what you want when responding to InputField changes.

这篇关于如何使用“On Value Change"在 Unity3D 输入字段 UI 组件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆