how to load images inside cell dynamically using www
vahidreza opened this issue · comments
hi i tried to change UpdateContent function inside FancyScrollviewCell.cs file as below:
public virtual IEnumerator UpdateContent(TData itemData) { return new object[0].GetEnumerator(); }
and changed UpdateContent function inside Example02ScrollViewCell.cs as below:
public override IEnumerator UpdateContent(Example02CellDto itemData) { message.text = itemData.Message; WWW www = new WWW(itemData.url); yield return www; if (context != null) { var isSelected = context.SelectedIndex == DataIndex;
image.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));; /*image.color = isSelected ? new Color32 (0, 255, 255, 100) : new Color32 (255, 255, 255, 77);*/ } yield return new object[0].GetEnumerator(); }
I have also changed my data model to be as below in Example02CellDto.cs:
namespace UnityEngine.UI.Extensions.Examples { public class Example02CellDto { public string Message; public string url; } }
the odd thing is the UpdateContent in Example02ScrollViewCell.cs does not get called.
what is really wrong with this approach and what is the right one?
cool, I have found the solution
I have rolled back my changes so FancyScrollviewCell.cs and Example02ScrollViewCell would look like the version before my changes, then I have added a couroutin function named loadImage as below:
`public IEnumerator loadImage(Example02CellDto itemData){
message.text = itemData.Message;
if (image.sprite == null) {
WWW www = new WWW (itemData.url);
yield return www;
if (context != null) {
var isSelected = context.SelectedIndex == DataIndex;
image.sprite = Sprite.Create (www.texture, new Rect (0, 0, www.texture.width, www.texture.height),
new Vector2 (0, 0));
;
/*image.color = isSelected
? new Color32 (0, 255, 255, 100)
: new Color32 (255, 255, 255, 77);*/
}
}
yield return new object[0].GetEnumerator();
}`
then I simply called the function inside the UpdateContent function in Example02ScrollViewCell.cs:
public override void UpdateContent(Example02CellDto itemData) { StartCoroutine (loadImage(itemData)); }
Thanks!
can we reopen it again for solving the lazy loading of images problem?
Since all images loads altogether the app becomes unresponsive and sometimes android kills the app.
is there a way you offer so I can lazy load my photos?
When loading a new image in Cell, have you discarded the old texture?
Destroy(oldTexture);
Actually I check if the texture of the cell is null, then I load the image, so I think when I assign the texture, the code won't run again.
Should I use Destroy function here again?
That was not necessary.
Could you tell me more in detail of problem?
The UpdateContent method of a cell is only called when necessary for display. It's already like lazy loading.
Hey man,
Merry chrismas
Can you tell me what specific method i can use to run a peice of code just when a scroll item is selected and is in the middle of the screen?
I guess im running loading images codes simultaneously for some items so the app hangs for sometime when i start the scene.
@vahidreza
Hi.
Fixed a bug that the "UpdateContent" method is called every frame when scrolling.
b5ee146