Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解决了的!如何添加onClick监听器和带有变量的方法调用来动态预制按钮?_C#_Unity3d_Button_Dynamic - Fatal编程技术网

C# 解决了的!如何添加onClick监听器和带有变量的方法调用来动态预制按钮?

C# 解决了的!如何添加onClick监听器和带有变量的方法调用来动态预制按钮?,c#,unity3d,button,dynamic,C#,Unity3d,Button,Dynamic,我有一个预置元素,其中有一个按钮和几个文本和图像元素。我可以像这样填充文本和图像元素: foreach(prefactname.Transform中的转换项){ 如果(item.name==“TextName”){ item.getComponentChildren().text=“任意”; } } 但当尝试以类似的方式添加onclick列表器时,它不起作用。没有错误 foreach(prefactname.Transform中的转换项){ 如果(item.name==“ButtonName”

我有一个预置元素,其中有一个按钮和几个文本和图像元素。我可以像这样填充文本和图像元素:

foreach(prefactname.Transform中的转换项){
如果(item.name==“TextName”){
item.getComponentChildren().text=“任意”;
}
}
但当尝试以类似的方式添加onclick列表器时,它不起作用。没有错误

foreach(prefactname.Transform中的转换项){
如果(item.name==“ButtonName”){
item.getComponentChildren()
.text=“title”;
item.getComponentChildren()
.onClick.AddListener(()=>myMethod(“info”);
}
}
上面的例子动态地将按钮文本重命名为“title”,所以它确实找到了按钮。但它不会将方法附加到onclick侦听器。我也尝试了下面的版本,取得了类似的成功

foreach(prefactname.Transform中的转换项){
如果(item.name==“ButtonName”){
item.getComponentChildren().text=“title”;
Button putikka=item.getComponentChildren();
putikka.onClick.AddListener(()=>myMethod(“info”);
}
}
应该如何做到这一点才能发挥作用

谢谢

编辑:

这会更改TextName中的文本:

    foreach (Transform item in prefabName.transform) {
        if (item.name == "TextName") {
            item.GetComponentInChildren<Text>().text = "anything";
       }
    }
foreach(prefactname.Transform中的转换项){
如果(item.name==“TextName”){
item.getComponentChildren().text=“任意”;
}
}
这确实改变了标题。按钮“ButtonName”的子项,即按钮的子项文本

if (item.name == "ButtonName") {
        item.GetComponentInChildren<Text>()
            .text = "title";
    }
}
if(item.name==“ButtonName”){
item.getComponentChildren()
.text=“title”;
}
}
所以在预制中我有

那么,这个广告应该是onclick监听器和info方法调用按钮,还是错过了按钮导致它指向的文本是子对象?我如何使它成为按钮的目标,而不是文本

if (item.name == "ButtonName") {
       item.GetComponentInChildren<Button>().onClick.AddListener(delegate { info(mesage); });
    }
}
if(item.name==“ButtonName”){
item.getComponentChildren().onClick.AddListener(委托{info(mesage);});
}
}
更新:

所以现在它就像:

Button putikka = item.GetComponent<Button>();
putikka.GetComponentInChildren<Text>().text = "anything";
putikka.GetComponent<Button>().image.color = Color.red;
putikka.GetComponent<Button>().interactable = true;
putikka.GetComponent<Button>().onClick.AddListener(delegate () { myMethod("info"); });
Button putikka=item.GetComponent();
putikka.getComponentChildren().text=“任意”;
putikka.GetComponent().image.color=color.red;
putikka.GetComponent().interactiable=true;
putikka.GetComponent().onClick.AddListener(委托(){myMethod(“info”);});

我得到的是一个红色的按钮,上面有文本“anything”,点击时会点击,但onclick中没有任何内容。。。我到底做错了什么?

您需要确保您的按钮组件实际上位于预设项的子对象中。因此,
item.getComponentChildren()实际查找按钮组件

如果要更改的对象位于对象本身而不是其子对象上,请使用
GetComponent()而不是
getComponentChildren()

如果现在工作正常,您还需要确保以正确的方式分配EventListener

示例:

string message=“这是一条动态消息”;
//将info方法指定为OnClick EventListener并给出参数消息
btn.GetComponent().onClick.AddListener(委托{info(mesage);});
已解决

当问题发生的时候,我确信我是个白痴,只是无法指出白痴发生的地方。但是,有几天我撞到了我的头,然后就明白了


我将所有内容直接添加到Prefact中,然后在实例化时将其复制到内容中,并在适当的位置添加细节。因此,这种风格的作品太过优秀的Addlisteners。因此,我开始在实例化预置时为它们和按钮指定唯一的名称。然后,只需再次找到按钮并给它一个AddListener。它是有效的。我学到了我的一课,关于编写代码和关于白痴的知识。:)

这可以工作:item.getComponentChildren().text=“title”@Jarno如果也这样做的话,您可以通过这种方式传递变量数据。请参阅我所做的编辑。我的意思是,你需要确保你的预制件在它的一个子里面有一个按钮组件,否则GetComponentChildren就不能工作了。是的,预制件的子里面有按钮组件。此按钮具有子级文本,我可以更改该文本组件文本。因此,我遗漏了一些无法分配onclick的内容。可能它实际上没有找到按钮,但只找到按钮的子项文本?但为什么它会在预置中找到与按钮处于同一级别的所有文本和图像字段呢?Buttons文本位于button下方,可以使用。我已更新了我的主要问题,因此可能会更清楚。如果要获取ButtonName上的对象,需要使用
GetComponent
而不是
GetComponentChildren
,但这不起作用。如果我的目标是TextName,我需要这样做(这是可行的):item.getComponentChildren().text=“title”;为什么ButtonName是“唯一”的GetComponent?(我又试了一次,还是不行。)你的按钮组件不是在ButtonName游戏对象中吗?如果是,则需要使用GetComponentNo获取它,因为它是在预置中并动态添加的。TextName也是一样。是的,我知道我的意思是,当你实例化预置时,是按钮名称GameObject上的按钮组件还是文本GameObject?