我目前正在开发一个应用程序,用户可以在其中查看所有产品,可以查看单个产品并可以下载特定产品。
这是我的代码:
IEnumerator ButtonClicked(int num, string name, string url, string color, string size, string price, string description)
{
prodName.text = name.ToString ();
prodColor.text = color.ToString ();
prodSize.text = size.ToString ();
prodPrice.text = price.ToString ();
prodDesc.text = description.ToString ();
string Url = url;
WWW www = new WWW (Url);
yield return www;
Texture2D texture = www.texture;
Image img = prodImg.GetComponent<Image> ();
img.sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), Vector2.zero);
string name2 = name.ToString ();
Debug.Log (name2.ToString ());
downloadButton.onClick.AddListener (() => StartCoroutine (onDownloadClick (name2.ToString ())));
}
IEnumerator onDownloadClick(string name) {
using (IDbConnection dbConnection = new SqliteConnection (conn)) {
dbConnection.Open ();
using (IDbCommand dbCmd = dbConnection.CreateCommand ()) {
string sqlQuery = String.Format ("INSERT INTO PRODUCTS(Name) VALUES(\"{0}\")", name);
dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar ();
dbConnection.Close ();
}
}
yield return new WaitForSeconds (.1f);
Debug.Log (name.ToString ());
}
在ButtonClicked函数上,它仅返回一个值,但是在显示onDownloadClick函数,当前值和所有先前值时。似乎是什么问题?谢谢!
最佳答案
Button.onClick
是一个ButtonClickedEvent
,它将保存“侦听器”,然后在被告知时(即,单击拥有的UnityEngine.UI.Button
时)调用它们。如果您从不退订,则将建立“监听器”列表。您只是通过AddListener
添加到列表中:
downloadButton.onClick.AddListener (() => StartCoroutine (onDownloadClick (name2.ToString ())));
在添加新的侦听器之前,请尝试清除
ButtonClickedEvent
对象,如下所示:downloadButton.onClick.RemoveAllListeners();
downloadButton.onClick.AddListener (() => StartCoroutine (onDownloadClick (name2.ToString ())));
关于c# - 函数正在传递变量的先前值,而不是Unity3D中的当前值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42617378/