Example #1
0
    public void LoadAsset(string url = "", bool loadEvenIfAlreadyLoaded = false)
    {
        if (string.IsNullOrEmpty(url) || (!loadEvenIfAlreadyLoaded && alreadyLoadedAsset))
        {
            return;
        }

        UpdateBackgroundColor(backgroundColor);

        // Check the src follows the needed format e.g.: 'ethereum://0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/558536'
        var regexMatches = Regex.Matches(url, "(?<protocol>[^:]+)://(?<registry>[^/]+)(?:/(?<asset>.+))?");

        if (regexMatches.Count == 0)
        {
            Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape. The accepted format is 'ethereum://ContractAddress/TokenID'");

            OnLoadingAssetFail?.Invoke();

            return;
        }

        Match match = regexMatches[0];

        if (match.Groups["protocol"] == null || match.Groups["registry"] == null || match.Groups["asset"] == null)
        {
            Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape.");

            OnLoadingAssetFail?.Invoke();

            return;
        }

        darURLProtocol = match.Groups["protocol"].ToString();
        if (darURLProtocol != "ethereum")
        {
            Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape. The only protocol currently supported is 'ethereum'");

            OnLoadingAssetFail?.Invoke();

            return;
        }

        darURLRegistry = match.Groups["registry"].ToString();
        darURLAsset    = match.Groups["asset"].ToString();

        if (VERBOSE)
        {
            Debug.Log("protocol: " + darURLProtocol);
            Debug.Log("registry: " + darURLRegistry);
            Debug.Log("asset: " + darURLAsset);
        }

        alreadyLoadedAsset = false;

        StartCoroutine(FetchNFTImage());
    }
    IEnumerator FetchNFTImage()
    {
        if (spinner != null)
        {
            spinner.SetActive(true);
        }

        string thumbnailImageURL = null;
        string previewImageURL   = null;
        string originalImageURL  = null;

        yield return(NFTHelper.FetchNFTInfo(darURLRegistry, darURLAsset,
                                            (nftInfo) =>
        {
            thumbnailImageURL = nftInfo.thumbnailUrl;
            previewImageURL = nftInfo.previewImageUrl;
            originalImageURL = nftInfo.originalImageUrl;
        },
                                            (error) =>
        {
            Debug.LogError($"Didn't find any asset image for '{darURLRegistry}/{darURLAsset}' for the NFTShape.\n{error}");
            OnLoadingAssetFail?.Invoke();
        }));

        yield return(new DCL.WaitUntil(() => (CommonScriptableObjects.playerUnityPosition - transform.position).sqrMagnitude < 900f));

        // We download the "preview" 256px image
        bool foundDCLImage = false;

        if (!string.IsNullOrEmpty(previewImageURL))
        {
            lastURLUsed = previewImageURL;

            yield return(WrappedTextureUtils.Fetch(previewImageURL, (downloadedTex, texturePromise) =>
            {
                foundDCLImage = true;
                this.texturePromise = texturePromise;
                SetFrameImage(downloadedTex, resizeFrameMesh: true);
            }, Asset_Gif.MaxSize.DONT_RESIZE));
        }

        //We fall back to the nft original image which can have a really big size
        if (!foundDCLImage && !string.IsNullOrEmpty(originalImageURL))
        {
            lastURLUsed = originalImageURL;

            yield return(WrappedTextureUtils.Fetch(originalImageURL, (downloadedTex, texturePromise) =>
            {
                foundDCLImage = true;
                this.texturePromise = texturePromise;
                SetFrameImage(downloadedTex, resizeFrameMesh: true);
            }, Asset_Gif.MaxSize._256));
        }

        FinishLoading(foundDCLImage);
    }
Example #3
0
    IEnumerator FetchNFTImage()
    {
        spinner?.SetActive(true);

        string thumbnailImageURL = null;
        string previewImageURL   = null;
        string originalImageURL  = null;

        yield return(NFTHelper.FetchNFTInfo(darURLRegistry, darURLAsset,
                                            (nftInfo) =>
        {
            thumbnailImageURL = nftInfo.thumbnailUrl;
            previewImageURL = nftInfo.previewImageUrl;
            originalImageURL = nftInfo.originalImageUrl;
        },
                                            (error) =>
        {
            Debug.LogError($"Didn't find any asset image for '{darURLRegistry}/{darURLAsset}' for the NFTShape.\n{error}");
            OnLoadingAssetFail?.Invoke();
        }));


        // We the "preview" 256px image
        bool foundDCLImage = false;

        if (!string.IsNullOrEmpty(previewImageURL))
        {
            yield return(Utils.FetchWrappedTextureAsset(previewImageURL, (downloadedAsset) =>
            {
                foundDCLImage = true;
                SetFrameImage(downloadedAsset, resizeFrameMesh: true);
            }));
        }

        // We fall back to the nft original image which can have a really big size
        if (!foundDCLImage && !string.IsNullOrEmpty(originalImageURL))
        {
            yield return(Utils.FetchWrappedTextureAsset(originalImageURL, (downloadedAsset) =>
            {
                foundDCLImage = true;
                SetFrameImage(downloadedAsset, resizeFrameMesh: true);
            }, DCL.WrappedTextureMaxSize._256));
        }

        if (foundDCLImage)
        {
            spinner?.SetActive(false);
            OnLoadingAssetSuccess?.Invoke();
        }
        else
        {
            OnLoadingAssetFail?.Invoke();
        }
    }
    void FinishLoading(bool loadedSuccessfully)
    {
        if (loadedSuccessfully)
        {
            if (spinner != null)
            {
                spinner.SetActive(false);
            }

            OnLoadingAssetSuccess?.Invoke();
        }
        else
        {
            OnLoadingAssetFail?.Invoke();
        }
    }
    IEnumerator FetchNFTImage()
    {
        if (spinner != null)
        {
            spinner.SetActive(true);
        }

        NFTInfo nftInfo = new NFTInfo();

        bool isError = false;

        yield return(NFTHelper.FetchNFTInfo(darURLRegistry, darURLAsset,
                                            (info) =>
        {
            nftInfo = info;
        },
                                            (error) =>
        {
            Debug.LogError($"Couldn't fetch NFT: '{darURLRegistry}/{darURLAsset}' {error}");
            OnLoadingAssetFail?.Invoke();
            isError = true;
        }));

        if (isError)
        {
            yield break;
        }

        yield return(new DCL.WaitUntil(() => (CommonScriptableObjects.playerUnityPosition - transform.position).sqrMagnitude < (config.loadingMinDistance * config.loadingMinDistance)));

        // We download the "preview" 256px image
        bool foundDCLImage = false;

        if (!string.IsNullOrEmpty(nftInfo.previewImageUrl))
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.previewImageUrl, (promise) =>
            {
                foundDCLImage = true;
                this.assetPromise = promise;
                SetFrameImage(promise.asset, resizeFrameMesh: true);
                SetupGifPlayer(promise.asset);

                var hqImageHandlerConfig = new NFTShapeHQImageConfig()
                {
                    controller = this,
                    nftConfig = config,
                    nftInfo = nftInfo,
                    asset = NFTAssetFactory.CreateAsset(promise.asset, config, UpdateTexture, gifPlayer)
                };
                hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig);
            }));
        }

        //We fall back to the nft original image which can have a really big size
        if (!foundDCLImage && !string.IsNullOrEmpty(nftInfo.originalImageUrl))
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.originalImageUrl,
                                                   (promise) =>
            {
                foundDCLImage = true;
                this.assetPromise = promise;
                SetFrameImage(promise.asset, resizeFrameMesh: true);
                SetupGifPlayer(promise.asset);
            }, () => isError = true));
        }

        if (isError)
        {
            Debug.LogError($"Couldn't fetch NFT image for: '{darURLRegistry}/{darURLAsset}' {nftInfo.originalImageUrl}");
            OnLoadingAssetFail?.Invoke();
            yield break;
        }

        FinishLoading(foundDCLImage);
    }
Example #6
0
    IEnumerator FetchNFTImage()
    {
        string jsonURL = $"{NFTDataFetchingSettings.DAR_API_URL}/{darURLRegistry}/asset/{darURLAsset}";

        UnityWebRequest www = UnityWebRequest.Get(jsonURL);

        yield return(www.SendWebRequest());

        if (!www.WebRequestSucceded())
        {
            OnLoadingAssetFail?.Invoke();

            Debug.LogError($"{www.responseCode} - {www.url}", gameObject);

            yield break;
        }

        NFTAssetData currentAssetData = JsonUtility.FromJson <NFTAssetData>(www.downloadHandler.text);

        if (currentAssetData.files == null)
        {
            Debug.LogError($"Didn't find any asset image for '{jsonURL}' for the NFTShape.");

            yield break;
        }

        if (VERBOSE)
        {
            Debug.Log("NFT fetched JSON: " + www.downloadHandler.text);

            Debug.Log("NFT Assets Found: ");
            for (int i = 0; i < currentAssetData.files.Length; i++)
            {
                Debug.Log("file url: " + currentAssetData.files[i]?.url);
            }
        }

        string thumbnailImageURL = null;
        string dclImageURL       = null;
        string previewImageURL   = null;

        for (int i = 0; i < currentAssetData.files.Length; i++)
        {
            if (currentAssetData.files[i].role == "thumbnail")
            {
                thumbnailImageURL = currentAssetData.files[i].url;
            }
            else if (currentAssetData.files[i].role == "preview")
            {
                previewImageURL = currentAssetData.files[i].url;
            }
            else if (currentAssetData.files[i].role == "dcl-picture-frame-image")
            {
                dclImageURL = currentAssetData.files[i].url;
            }
        }

        // We fetch and show the thumbnail image first
        if (!string.IsNullOrEmpty(thumbnailImageURL))
        {
            yield return(Utils.FetchWrappedTextureAsset(thumbnailImageURL, (downloadedAsset) =>
            {
                SetFrameImage(downloadedAsset);
            }));
        }

        // We fetch the final image
        bool foundDCLImage = false;

        if (!string.IsNullOrEmpty(dclImageURL))
        {
            yield return(Utils.FetchWrappedTextureAsset(dclImageURL, (downloadedAsset) =>
            {
                // Dispose thumbnail
                if (nftAsset != null)
                {
                    nftAsset.Dispose();
                }
                foundDCLImage = true;
                SetFrameImage(downloadedAsset, resizeFrameMesh: true);
            }));
        }

        // We fall back to a common "preview" image if no "dcl image" was found
        if (!foundDCLImage && !string.IsNullOrEmpty(previewImageURL))
        {
            yield return(Utils.FetchWrappedTextureAsset(previewImageURL, (downloadedAsset) =>
            {
                // Dispose thumbnail
                if (nftAsset != null)
                {
                    nftAsset.Dispose();
                }
                SetFrameImage(downloadedAsset, resizeFrameMesh: true);
            }));
        }

        OnLoadingAssetSuccess?.Invoke();
    }