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

查看:157
本文介绍了如何使用“价值变化"在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.

根据本教程,我只需要使用输入字段(脚本)的按值更改"属性,并告诉它调用一些将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.

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

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