Skip to content

Commit

Permalink
feat: New member function in Asset Downloader, OnCompleteReturnLinked…
Browse files Browse the repository at this point in the history
…Nft() action returning Nft class with Texture2D ( NFTImage) attached in Nft.Assets.Texture when passed an Nft with it + Stop() fn to stop any in progress downloads
  • Loading branch information
saszer committed Jul 13, 2022
1 parent 7cdec35 commit d93ce27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
43 changes: 38 additions & 5 deletions Runtime/AssetDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class GetImage : AssetDownloader
{
private UnityAction<string> OnErrorAction;
private UnityAction<UnityEngine.Texture2D> OnCompleteAction;
private UnityAction<Nft> OnCompleteReturnLinkedNftAction;
private static UnityAction<string> OnAllAssetsDownloadEnded;

/// <summary>
Expand All @@ -47,12 +48,24 @@ public static GetImage Initialize(bool destroyAtEnd = true)
/// <summary>
/// Action on succesfull Download
/// </summary>
/// <returns> Texture2D </returns>
/// <returns> Texture2D of Nft</returns>
public GetImage OnComplete(UnityAction<Texture2D> action)
{
this.OnCompleteAction = action;
return this;
}

/// <summary>
/// Action on succesfull Download
/// </summary>
/// <returns> Nft class with attched Texture2D</returns>
private Nft OnCompleteLinkNft;
public GetImage OnCompleteReturnLinkedNft(Nft Nft, UnityAction<Nft> action)
{
OnCompleteLinkNft = Nft;
this.OnCompleteReturnLinkedNftAction = action;
return this;
}

/// <summary>
/// Action on Error
Expand Down Expand Up @@ -95,12 +108,18 @@ public GetImage Download(string URL, Nft NFT = null, bool isIPFS = false)
StartCoroutine(DownloadTexture(URL, NFT));
return this;
}


public void Stop(bool destroy = true)
{
StopAllCoroutines();
End();
}

private UnityWebRequest request;
IEnumerator DownloadTexture(string URL, Nft NFT = null)
{
assetsDownloaders++;
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(URL))
using (request = UnityWebRequestTexture.GetTexture(URL))
{
yield return request.SendWebRequest();

Expand All @@ -111,6 +130,7 @@ IEnumerator DownloadTexture(string URL, Nft NFT = null)
Debug.Log($"Null data. Response code: {request.responseCode}. Result {request.downloadHandler.text}");
if(afterError!=null)
afterError.Invoke();
End();
}
else
{
Expand All @@ -122,7 +142,13 @@ IEnumerator DownloadTexture(string URL, Nft NFT = null)

if (NFT != null)
NFT.assets.image_texture = lastGetImage;


if ((OnCompleteReturnLinkedNftAction != null && OnCompleteLinkNft != null && lastGetImage != null))
{
OnCompleteLinkNft.assets.image_texture = lastGetImage;
OnCompleteReturnLinkedNftAction.Invoke(OnCompleteLinkNft);
}
if(OnCompleteAction!=null && lastGetImage!=null)
OnCompleteAction.Invoke(lastGetImage);

Expand All @@ -137,10 +163,17 @@ IEnumerator DownloadTexture(string URL, Nft NFT = null)
OnAllAssetsDownloadEnded("assetsDownloaders Ended");
}

if(_destroyAtEnd)
Destroy (this.gameObject);
}
}

public void End()
{
if(request != null)
request.Dispose();
StopAllCoroutines();
if(_destroyAtEnd)
Destroy (this.gameObject);
}

}
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Internal/models/NFTs_model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class Nft
public List<Royalty> royalties;
public List<string> signatures;
public int total;
public Assets assets;
public Assets assets = new Assets();
}

[Serializable]
Expand Down

0 comments on commit d93ce27

Please sign in to comment.