Post by sniper199999 on Mar 17, 2022 14:56:52 GMT
I want to combine 2 demo scenes for my project (View Driven Cell Sizes and Remote Loading). It it seems like in Set Data function, data.cellSize is getting called for every Cell, before the cellImage.sprite is set. The outcome for this is same CellView size for all cells. I suspect it is something to do with StartCoroutine().
(Also all the Image sizes are different)
My Code:
namespace EnhancedScrollerDemos1.RemoteResourcesDemo1
{
public class CellView : EnhancedScrollerCellView
{
public Image cellImage;
public Sprite defaultSprite;
public RectTransform textRectTransform;
private Coroutine _loadImageCoroutine;
public void SetData(Data data,bool calculateLayout )
{
_loadImageCoroutine = StartCoroutine(LoadRemoteImage(data));
if (calculateLayout)
{
Canvas.ForceUpdateCanvases();
Debug.Log("here");
data.cellSize = textRectTransform.rect.height + textBuffer.top + textBuffer.bottom;
}
}
public IEnumerator LoadRemoteImage(Data data)
string path = data.imageUrl;
Texture2D texture = null;
#if UNITY_2020_1_OR_NEWER
var webRequest = UnityWebRequestTexture.GetTexture(path);
yield return webRequest.SendWebRequest();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Failed to download image [" + path + "]: " + webRequest.error);
}
else
{
texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
}
#endif
if (texture != null)
{
cellImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
else
{
ClearImage();
}
}
public void ClearImage()
{
cellImage.sprite = defaultSprite;
}
public void WillRecycle()
{
if (_loadImageCoroutine != null)
{
StopCoroutine(_loadImageCoroutine);
}
}
}
}
I tried to calculate Layout in setting cellImage.sprite in IEnumerator LoadRemoteImage, but that doesnt work as well. I am quite new to C# scripting, and I know it would be something really stupid that I am missing, can anyone help me with this. Thanks