Example #1
0
        private void StartFetchOperation(long productId, bool hidden = false)
        {
            // when the purchase info is not available for a package (either it's not fetched yet or just not available altogether)
            // we'll try to fetch the purchase info first and then call the `FetchInternal`.
            // In the case where a package not purchased, `purchaseInfo` will still be null,
            // but the generated `AssetStorePackage` in the end will contain an error.
            var fetchOperation = new AssetStoreListOperation(m_UnityConnect, m_AssetStoreRestAPI, m_AssetStoreCache);
            var queryArgs      = new PurchasesQueryArgs {
                productIds = new List <string> {
                    productId.ToString()
                }, status = hidden? "hidden" : string.Empty
            };

            fetchOperation.onOperationSuccess += op =>
            {
                var purchaseInfo = fetchOperation.result.list.FirstOrDefault();
                // If we can't find the purchase info the first time, it could be be that the asset is hidden
                // we'll do another check
                if (!hidden && purchaseInfo == null)
                {
                    StartFetchOperation(productId, true);
                    return;
                }

                if (purchaseInfo != null)
                {
                    m_AssetStoreCache.SetPurchaseInfo(purchaseInfo);
                }
                FetchInternal(productId, purchaseInfo);
            };
            fetchOperation.Start(queryArgs);
        }
Example #2
0
        public virtual void ListPurchases(PurchasesQueryArgs queryArgs)
        {
            CancelListPurchases();

            RefreshLocalInfos();

            m_ListOperation = m_ListOperation ?? new AssetStoreListOperation(m_UnityConnect, m_AssetStoreRestAPI, m_AssetStoreCache);
            m_ListOperation.onOperationSuccess += op =>
            {
                var result = m_ListOperation.result;
                if (result.list.Count > 0)
                {
                    var updatedPackages = new List <IPackage>();
                    foreach (var purchaseInfo in result.list)
                    {
                        var productIdString = purchaseInfo.productId.ToString();
                        var oldPurchaseInfo = m_AssetStoreCache.GetPurchaseInfo(productIdString);
                        m_AssetStoreCache.SetPurchaseInfo(purchaseInfo);

                        // create a placeholder before fetching data from the cloud for the first time
                        var productInfo = m_AssetStoreCache.GetProductInfo(productIdString);
                        if (productInfo == null)
                        {
                            updatedPackages.Add(new PlaceholderPackage(productIdString, purchaseInfo.displayName, PackageType.AssetStore, PackageTag.Downloadable, PackageProgress.Refreshing));
                        }
                        else if (oldPurchaseInfo != null)
                        {
                            // for now, `tags` is the only component in `purchase info` that can be updated over time, so we only check for changes there
                            var oldTags = oldPurchaseInfo.tags ?? Enumerable.Empty <string>();
                            var newTags = purchaseInfo.tags ?? Enumerable.Empty <string>();
                            if (!oldTags.SequenceEqual(newTags))
                            {
                                updatedPackages.Add(new AssetStorePackage(m_AssetStoreUtils, m_IOProxy, purchaseInfo, productInfo, m_AssetStoreCache.GetLocalInfo(productIdString)));
                            }
                        }
                    }

                    if (updatedPackages.Any())
                    {
                        onPackagesChanged?.Invoke(updatedPackages);
                    }
                }

                foreach (var cat in result.categories)
                {
                    m_AssetStoreCache.SetCategory(cat.name, cat.count);
                }

                onProductListFetched?.Invoke(result);
            };

            onListOperation?.Invoke(m_ListOperation);
            m_ListOperation.Start(queryArgs);
        }