如何将图像加载到类的数组中?
我有以下代码在其中加载图像。 但是,每当我运行代码时,发现集合中的其他字段都是空的,并且在所有集合数据中只有一个图像,&生成以下错误。 indexoutofrangeException:索引不在数组的范围之外。 (包装器stelemref)system.Object.virt_stelemref_class_small_idepth(intptr,object)
下面是我的代码:
[System.Serializable]
public class CollectionDataClass
{
public string name;
public string id;
public string thumbnail;
public Texture2D collectionImage;
}
[System.Serializable]
public class DataClass
{
[SerializeField] public CollectionDataClass[] collectionDataClass;
}
[SerializeField] private DataClass[] DataClassArray;
public IEnumerator BookCatogary(string reqParameter)
{
BookCatogaryBodyClass BookCatogryClass = new BookCatogaryBodyClass(reqParameter);
string json = JsonConvert.SerializeObject(BookCatogryClass);
Debug.Log("==========================>" + json);
var req = new UnityWebRequest(bookCategaryAPI, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
req.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
if (req.isNetworkError)
{
Debug.Log("Error While Sending: " + req.error);
}
else
{
Debug.Log("Received: " + req.downloadHandler.text);
string temp = req.downloadHandler.text;
bookCatogaryObject = JsonConvert.DeserializeObject<BookCatogaryReceivedDataClass>(temp);
catogaryCount = bookCatogaryObject.Data.Rows.Count;
GetCatogariesName(catogaryCount);
GenerateCollectionData();
}
}
public void GenerateCollectionData()
{
DataClassArray = new DataClass[bookCatogaryObject.Data.Rows.Count];
for (int i = 0; i <= bookCatogaryObject.Data.Rows.Count; i++)
{
DataClassArray[i] = new DataClass();
DataClassArray[i].collectionDataClass = new CollectionDataClass[bookCatogaryObject.Data.Rows[i].String.Count];
for (int m = 0; m < bookCatogaryObject.Data.Rows[i].String.Count; m++)
{
DataClassArray[i].collectionDataClass[m] = new CollectionDataClass();
DataClassArray[i].collectionDataClass[m].name = bookCatogaryObject.Data.Rows[i].String[m].CollectionTitle;
DataClassArray[i].collectionDataClass[m].id = bookCatogaryObject.Data.Rows[i].String[m].Id;
DataClassArray[i].collectionDataClass[m].thumbnail = bookCatogaryObject.Data.Rows[i].String[m].CollectionThumbnail;
StartCoroutine(DownloadImage(mediaUrl: DataClassArray[i].collectionDataClass[m].thumbnail , bookCatogaryObject.Data.Rows[i].String.Count));
}
}
}
IEnumerator DownloadImage(string mediaUrl, int count)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(mediaUrl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);
else
{
//collectionImage = ((DownloadHandlerTexture)request.downloadHandler).texture;
//collectionImageContainer[count] = collectionImage;
for (int i = 0; i <= bookCatogaryObject.Data.Rows.Count; i++)
{
DataClassArray[i] = new DataClass();
DataClassArray[i].collectionDataClass = new CollectionDataClass[bookCatogaryObject.Data.Rows[i].String.Count];
for (int m = 0; m < bookCatogaryObject.Data.Rows[i].String.Count; m++)
{
DataClassArray[i].collectionDataClass[m] = new CollectionDataClass();
DataClassArray[i].collectionDataClass[m].collectionImage = ((DownloadHandlerTexture)request.downloadHandler).texture;
}
}
}
}
I have following code in which I wand to load images.
But whenever I Run the code other fields in collection are found empty and only one image is found in all collection data, & The following error is generated.
IndexOutOfRangeException: Index was outside the bounds of the array.
(wrapper stelemref) System.Object.virt_stelemref_class_small_idepth(intptr,object)
Below is my code :
[System.Serializable]
public class CollectionDataClass
{
public string name;
public string id;
public string thumbnail;
public Texture2D collectionImage;
}
[System.Serializable]
public class DataClass
{
[SerializeField] public CollectionDataClass[] collectionDataClass;
}
[SerializeField] private DataClass[] DataClassArray;
public IEnumerator BookCatogary(string reqParameter)
{
BookCatogaryBodyClass BookCatogryClass = new BookCatogaryBodyClass(reqParameter);
string json = JsonConvert.SerializeObject(BookCatogryClass);
Debug.Log("==========================>" + json);
var req = new UnityWebRequest(bookCategaryAPI, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
req.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
if (req.isNetworkError)
{
Debug.Log("Error While Sending: " + req.error);
}
else
{
Debug.Log("Received: " + req.downloadHandler.text);
string temp = req.downloadHandler.text;
bookCatogaryObject = JsonConvert.DeserializeObject<BookCatogaryReceivedDataClass>(temp);
catogaryCount = bookCatogaryObject.Data.Rows.Count;
GetCatogariesName(catogaryCount);
GenerateCollectionData();
}
}
public void GenerateCollectionData()
{
DataClassArray = new DataClass[bookCatogaryObject.Data.Rows.Count];
for (int i = 0; i <= bookCatogaryObject.Data.Rows.Count; i++)
{
DataClassArray[i] = new DataClass();
DataClassArray[i].collectionDataClass = new CollectionDataClass[bookCatogaryObject.Data.Rows[i].String.Count];
for (int m = 0; m < bookCatogaryObject.Data.Rows[i].String.Count; m++)
{
DataClassArray[i].collectionDataClass[m] = new CollectionDataClass();
DataClassArray[i].collectionDataClass[m].name = bookCatogaryObject.Data.Rows[i].String[m].CollectionTitle;
DataClassArray[i].collectionDataClass[m].id = bookCatogaryObject.Data.Rows[i].String[m].Id;
DataClassArray[i].collectionDataClass[m].thumbnail = bookCatogaryObject.Data.Rows[i].String[m].CollectionThumbnail;
StartCoroutine(DownloadImage(mediaUrl: DataClassArray[i].collectionDataClass[m].thumbnail , bookCatogaryObject.Data.Rows[i].String.Count));
}
}
}
IEnumerator DownloadImage(string mediaUrl, int count)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(mediaUrl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);
else
{
//collectionImage = ((DownloadHandlerTexture)request.downloadHandler).texture;
//collectionImageContainer[count] = collectionImage;
for (int i = 0; i <= bookCatogaryObject.Data.Rows.Count; i++)
{
DataClassArray[i] = new DataClass();
DataClassArray[i].collectionDataClass = new CollectionDataClass[bookCatogaryObject.Data.Rows[i].String.Count];
for (int m = 0; m < bookCatogaryObject.Data.Rows[i].String.Count; m++)
{
DataClassArray[i].collectionDataClass[m] = new CollectionDataClass();
DataClassArray[i].collectionDataClass[m].collectionImage = ((DownloadHandlerTexture)request.downloadHandler).texture;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是您问题的答案。我只是想向您展示如何使用临时当地人和对象初始化器来简化代码。它还使代码更有效,因为它消除了重复的索引和成员访问。但更重要的是,它使代码更可读。
This is not an answer to your question. I just wanted to show you how you can simplify code by using temp locals and object initializers. It also makes the code more efficient because it eliminates repeated indexing and member accesses. But more importantly, it makes the code more readable.
因此,最后我找到了上述问题的答案,而不是使用coroutines,而是使用async任务将下载的纹理返回到generateCollectionData()方法。
以下是参考代码:
So finally I found the answer to above question, instead of using coroutines I used async Task to return downloaded texture to GenerateCollectionData() method.
Below is code for reference :