C# 使用脚本的Unity材质渲染工作不正确
这是按钮组件的层次结构 钮扣C# 使用脚本的Unity材质渲染工作不正确,c#,unity3d,C#,Unity3d,这是按钮组件的层次结构 钮扣 形象 正文 用于渲染的脚本如下所示: for(int ii = 0; ii < idlist.Count; ii++){ GameObject addTypeButton = (GameObject)Instantiate(prefabButton); addTypeButton.transform.SetParent(ParentPanel, false);
- 形象
- 正文
for(int ii = 0; ii < idlist.Count; ii++){
GameObject addTypeButton = (GameObject)Instantiate(prefabButton);
addTypeButton.transform.SetParent(ParentPanel, false);
var mybutton = addTypeButton.GetComponent<MyButton>();
//set text
mybutton.text.text = (string)sometext[ii];
//get image
WWW www = new WWW((string)someImageUrl[ii]);
yield return www;
//set image
var b64_bytes = System.Convert.FromBase64String(www.text);
Texture2D tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
tex.EncodeToPNG();
tex.LoadImage(b64_bytes);
//yield return new WaitForSeconds (5) ;
mybutton.image.material.mainTexture = tex;
//testing
File.WriteAllBytes(Application.dataPath + "/../Saved["+ii +"].png", b64_bytes);
}
for(int ii=0;ii
测试命令在序列中生成正确的图像。
但是,在Unity中,图像在下一个对象上渲染。
我到底哪里出错了?不要使用加载图像。这就是为什么。因此,您的System.Convert.FromBase64String(www.text)应删除代码>。您也不需要Texture2D.EncodeToPNG()代码>函数,因为您下载的图像已经是png或jpeg格式。只需直接保存下载的图像字节
最后,如果您希望保存的内容在每个平台上都能正常工作,则必须使用Application.persistentDataPath
而不是Application.dataPath
。您还应该使用Path.combined
来连接路径,而不是手动连接
for (int ii = 0; ii < idlist.Count; ii++)
{
GameObject addTypeButton = (GameObject)Instantiate(prefabButton);
addTypeButton.transform.SetParent(ParentPanel, false);
var mybutton = addTypeButton.GetComponent<MyButton>();
//set text
mybutton.text.text = (string)sometext[ii];
//get image
WWW www = new WWW((string)someImageUrl[ii]);
yield return www;
//set image
byte[] downloadedImage = www.bytes;
Texture2D tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
tex.LoadImage(downloadedImage);
//yield return new WaitForSeconds (5) ;
mybutton.image.material.mainTexture = tex;
//testing
string savePath = Application.persistentDataPath;
savePath = Path.Combine(savePath, "images");
savePath = Path.Combine(savePath, ii.ToString());
savePath = Path.Combine(savePath, ".png");
File.WriteAllBytes(savePath, downloadedImage);
}
for(int ii=0;ii
谢谢您的建议。那么这个问题仍然存在。将在下一个对象而不是当前对象上渲染图像。(如图中的截图所示)什么是代码> IDList?考虑IDLIST。计数为元素的数量。这只是一个数据id数组。你的问题不完整。如果您需要帮助,您应该编辑您的问题并添加更多详细信息。包括您的MyButton
脚本。另外,添加创建idlist
的方式。发布someImageUrl
数组中使用的URL。没有这些,我不认为任何人可以复制你的问题。