You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法在Inspector中为输入框绑定ReadStringInput函数?

问题原因

Unity UI输入框的On End Edit事件,回调函数的签名要求是仅接收一个string类型参数(对应输入框的文本内容)。你的ReadStringInput函数定义了两个string参数,和事件的回调签名不匹配,所以编辑器不会把它列入可选的绑定函数列表。

解决方法

把要猜测的目标单词改成脚本的成员变量,让ChooseWord函数把选中的单词存在这个变量里,然后修改ReadStringInput的签名,只保留输入框文本的参数,这样就能正常绑定了。

修改后的完整代码示例

// 新增成员变量存储目标单词
private string targetWord;
private string input;
// 假设你已经定义了这些字母变量
private string firstLetterWord, secondLetterWord, thirdLetterWord, fourthLetterWord, fifthLetterWord;
private string firstLetterInput, secondLetterInput, thirdLetterInput, fourthLetterInput, fifthLetterInput;
// 假设dictionary是你定义的单词数组
private string[] dictionary;

public void ChooseWord()
{
    Random random = new Random();
    targetWord = dictionary[random.Next(dictionary.Length)];
    Debug.Log("目标单词:" + targetWord);
}

public void ReadStringInput(string s)
{
    input = s;

    // 从成员变量targetWord中提取字母
    firstLetterWord = targetWord.Substring(0, 1);
    secondLetterWord = targetWord.Substring(1, 1);
    thirdLetterWord = targetWord.Substring(2, 1);
    fourthLetterWord = targetWord.Substring(3, 1);
    fifthLetterWord = targetWord.Substring(4, 1);

    firstLetterInput = input.Substring(0, 1);
    secondLetterInput = input.Substring(1, 1);
    thirdLetterInput = input.Substring(2, 1);
    fourthLetterInput = input.Substring(3, 1);
    fifthLetterInput = input.Substring(4, 1);

    Debug.Log(firstLetterInput + " " + firstLetterWord);

    if (firstLetterWord == firstLetterInput)
    {
        Debug.Log("第一个字母正确");
    }
}

绑定步骤

修改完成后,回到Unity编辑器:

  • 选中挂载该脚本的对象
  • 在输入框的On End Edit事件面板中,点击+号,拖入脚本所在对象
  • 在下拉列表中就能找到ReadStringInput函数,选择它即可

内容的提问来源于stack exchange,提问作者Vota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:33:26