C# 统一界面按钮;带有颜色和名称列表的foreach循环
所以我试着制作一个带有按钮的UI列表来改变统一的颜色。在这个脚本中,我有两个列表,一个用于颜色,一个用于名称,用于我尝试生成的按钮。为了生成这些按钮,我使用foreach循环。然而,如果我把for-each循环放在另一个循环中,我得到的按钮比我想要的多得多。或者我得到了正确数量的按钮,但是按钮在列表中会有姓氏。 在剧本中,我把我试过的东西注释掉了C# 统一界面按钮;带有颜色和名称列表的foreach循环,c#,user-interface,unity3d,C#,User Interface,Unity3d,所以我试着制作一个带有按钮的UI列表来改变统一的颜色。在这个脚本中,我有两个列表,一个用于颜色,一个用于名称,用于我尝试生成的按钮。为了生成这些按钮,我使用foreach循环。然而,如果我把for-each循环放在另一个循环中,我得到的按钮比我想要的多得多。或者我得到了正确数量的按钮,但是按钮在列表中会有姓氏。 在剧本中,我把我试过的东西注释掉了 public GameObject ContentPanel; public GameObject ListItemPrefab; // List
public GameObject ContentPanel;
public GameObject ListItemPrefab;
// List
public List<Color> ColorOptions;
public List<string> ColorNames;
// Use this for initialization
void Start () {
foreach(Color color in ColorOptions){
//Too much buttons
//foreach (string name in ColorNames){
//Spawn button
GameObject newButton = Instantiate(ListItemPrefab) as GameObject;
// Get button component
ButtonController controller = newButton.GetComponent<ButtonController>();
// Give button the color from the list
newButton.GetComponent<Image>().color = color;
controller.Name.text = name;
newButton.transform.SetParent(ContentPanel.transform);
newButton.transform.localScale = Vector3.one;
//List with the names
//Buttons get the same name
/*foreach (string name in ColorNames){
controller.Name.text = name;
}*/
//}
}
}
公共游戏对象内容面板;
公共游戏对象列表项预制;
//名单
公共列表选项;
公开名单颜色名称;
//用于初始化
无效开始(){
foreach(颜色选项中的颜色){
//按钮太多
//foreach(ColorNames中的字符串名称){
//繁殖按钮
GameObject newButton=实例化(ListItemPrefab)为GameObject;
//获取按钮组件
ButtonController controller=newButton.GetComponent();
//从列表中为按钮指定颜色
newButton.GetComponent().color=color;
controller.Name.text=名称;
newButton.transform.SetParent(ContentPanel.transform);
newButton.transform.localScale=Vector3.one;
//名单
//按钮的名称相同
/*foreach(ColorNames中的字符串名称){
controller.Name.text=名称;
}*/
//}
}
}
buttoncontroller中是按钮文本的变量名。不管怎样,我的问题是:我如何在不乘以按钮的情况下从列表中获取姓名?我希望你们能帮我解决这个问题 以下代码将创建尽可能多的按钮,颜色选项将与相应的名称一起使用-
public GameObject ContentPanel;
public GameObject ListItemPrefab;
// List of color option and corresponding names
public List<Color> ColorOptions;
public List<string> ColorNames;
// Use this for initialization
void Start () {
for(int count=0;count<ColorOptions.Count;count++){
//Spawn button
GameObject newButton = Instantiate(ListItemPrefab) as GameObject;
// Get button component
ButtonController controller = newButton.GetComponent<ButtonController>();
// Give button the color from the list
newButton.GetComponent<Image>().color = ColorOptions[count];
// Give button the name from the list
controller.Name.text = ColorNames[count];
newButton.transform.SetParent(ContentPanel.transform);
newButton.transform.localScale = Vector3.one;
}
}
公共游戏对象内容面板;
公共游戏对象列表项预制;
//颜色选项和相应名称的列表
公共列表选项;
公开名单颜色名称;
//用于初始化
无效开始(){
for(int count=0;count)您可能需要使用“for”循环,谢谢@spatbord。捕捉得好。这是一个输入错误。