我找到了更有效的代码,可以从父游戏对象中找到每个子组件,并更改游戏对象的每个子组件文本。
gameobject的每个子组件文本都有不同的值,每个依赖于变量。
这是屏幕截图

这是我的代码:
public Dictionary <string, GameObject> product = new Dictionary<string, GameObject>();
public Dictionary <string, Text> property = new Dictionary<string, Text> ();
dex = productMargaret.FindIndex(j => j.productID == 15);
product["Mayonnaise"] = GameObject.Find("btnMayonnaise");
property["qty"] = gameObject.transform.GetChild (1).GetComponent<Text> (); //+ productMargaret[dex].qty.ToString;
property["qty"].text = productMargaret[dex].qty.ToString;
property["price"] = gameObject.transform.GetChild (2).GetComponent<Text> (); //+ productMargaret[dex].price.ToString;
property["price"].text = "@ " + productMargaret[dex].price.ToString;
property["have"] = gameObject.transform.GetChild (2).GetComponent<Text> (); //+ productMargaret[dex].have.ToString;
property["have"].text = "You Have : " + productMargaret[dex].have.ToString;这就是代码不工作的原因。
该怎么做呢?为了更有效的方式,比如使用循环。
谢谢
发布于 2016-07-13 13:34:04
假设您的按钮的名称类似于btnProductID_15、btnProductID_16、btnProductID_17,并附加了按钮组件。您可以简单地在父gameObject上执行以下操作:
public Dictionary <string, string> property = new Dictionary<string, Text> (); //the Key would be btnProductID_15...btnProductID_N and value will be the label of the button.
List<Button> btnList = GetComponentsInChildren<Button>();
foreach (Button item in btnList)
{
Text textField = GetComponentInChildren<Text>();
textField.text = property[item.name];
}当然,您需要填充"property“字典accordingly....And now am。
https://stackoverflow.com/questions/38343043
复制相似问题