Пример #1
0
        /// <summary>
        /// Overload for purchasing virtual product based on its product identifier.
        /// </summary>
        public static void PurchaseProduct(IAPObject obj)
        {
            string productId = obj.id;

            //product is set to already owned, do nothing
            if (DBManager.isPurchased(productId))
            {
                OnPurchaseFailed("Product already purchased.");
                return;
            }

            //check whether the player has enough funds
            bool didSucceed = DBManager.VerifyVirtualPurchase(obj);

            if (isDebug)
            {
                Debug.Log("Purchasing virtual product " + productId + ", result: " + didSucceed);
            }
            //on success, non-consumables are saved to the database. This automatically
            //saves the new substracted fund value, then and fire the succeeded event
            if (didSucceed)
            {
                if (obj.type != ProductType.Consumable)
                {
                    DBManager.SetToPurchased(productId);
                }
                purchaseSucceededEvent(productId);
            }
            else
            {
                OnPurchaseFailed("Insufficient funds.");
            }
        }
Пример #2
0
        // set product to purchased after successful verification (or without)
        // Consumable IAPs must be consumed
        // For non consumable IAPs or subscriptions, alter database entry
        private void PurchaseVerified(string id)
        {
            if (!IAPObjects.ContainsKey(id))
            {
                return;
            }
            IAPObject obj = IAPObjects[id];

            if (obj.type == IAPType.consumable)
            {
                OpenIAB.consumeProduct(inventory.GetPurchase(obj.GetIdentifier()));                 //local id
                return;
            }

            //don't continue if the product is already purchased,
            //for example if we just want to verify an existing product again
            if (DBManager.isPurchased(id))
            {
                return;
            }

            if (obj.type == IAPType.nonConsumable || obj.type == IAPType.subscription)
            {
                DBManager.SetToPurchased(id);
            }

            purchaseSucceededEvent(id);
        }
Пример #3
0
        /// <summary>
        /// this method checks user's funds for a virtual purchase
        /// and if they met the requirements, substracts funds
        /// </summary>
        public static bool VerifyVirtualPurchase(IAPObject obj)
        {
            //get a list of all currencies to check against
            Dictionary <string, int> curs = GetAllCurrencies();

            //loop over currencies and check each amount
            //if the player does not have enough funds, return false
            for (int i = 0; i < obj.virtualPrice.Count; i++)
            {
                IAPCurrency cur = obj.virtualPrice[i];
                if (curs.ContainsKey(cur.name) && cur.amount > curs[cur.name])
                {
                    return(false);
                }
            }
            //the player has enough funds,
            //get location string
            string loc = instance.currency;

            //loop over each currency
            //and substract price for this item
            for (int i = 0; i < obj.virtualPrice.Count; i++)
            {
                IAPCurrency cur = obj.virtualPrice[i];
                if (curs.ContainsKey(cur.name))
                {
                    instance.gameData[loc][cur.name].AsInt -= cur.amount;
                }
            }
            //verification succeeded
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Initialize the instance using the specified IStoreCallback.
        /// </summary>
        public void Initialize(IStoreCallback callback)
        {
            this.callback = callback;

            IOSInAppPurchaseManager.OnStoreKitInitComplete += OnProductsRetrieved;
            IOSInAppPurchaseManager.OnTransactionComplete  += OnPurchaseSucceeded;

            #if !UNITY_EDITOR
            if (!IOSInAppPurchaseManager.Instance.IsInAppPurchasesEnabled)
            {
                OnSetupFailed("Apple App Store billing is disabled.");
                return;
            }
            #endif

            definitions = IAPManager.GetProductDefinitions();
            IOSNativeSettings.Instance.InAppProducts.Clear();
            for (int i = 0; i < definitions.Length; i++)
            {
                IOSInAppPurchaseManager.Instance.AddProductId(definitions[i].storeSpecificId);

                #if UNITY_EDITOR
                IAPObject obj = IAPManager.GetIAPObject(definitions[i].id);
                IOSNativeSettings.Instance.InAppProducts[i].DisplayName    = obj.title;
                IOSNativeSettings.Instance.InAppProducts[i].Description    = obj.description;
                IOSNativeSettings.Instance.InAppProducts[i].LocalizedPrice = obj.realPrice;
                #endif
            }

            IOSInAppPurchaseManager.Instance.LoadStore();
        }
Пример #5
0
        /// <summary>
        /// Initialize the instance using the specified IStoreCallback.
        /// </summary>
        public virtual void Initialize(IStoreCallback callback)
        {
            this.callback = callback;

            AndroidInAppPurchaseManager.ActionBillingSetupFinished    += OnInitialized;
            AndroidInAppPurchaseManager.ActionRetrieveProducsFinished += OnProductsRetrieved;
            AndroidInAppPurchaseManager.ActionProductPurchased        += OnPurchaseSucceeded;
            AndroidInAppPurchaseManager.ActionProductConsumed         += OnTransactionFinished;

            if (!AndroidNativeSettings.Instance.IsBase64KeyWasReplaced)
            {
                Debug.LogWarning("Google Store Public Key missing in Android Native Settings. "
                                 + "Purchases for real money won't be supported on the device.");
            }

            definitions = IAPManager.GetProductDefinitions();
            AndroidNativeSettings.Instance.InAppProducts.Clear();
            for (int i = 0; i < definitions.Length; i++)
            {
                AndroidInAppPurchaseManager.Client.AddProduct(definitions[i].storeSpecificId);

                #if UNITY_EDITOR
                IAPObject obj = IAPManager.GetIAPObject(definitions[i].id);
                AndroidNativeSettings.Instance.InAppProducts[i].Title          = obj.title;
                AndroidNativeSettings.Instance.InAppProducts[i].Description    = obj.description;
                AndroidNativeSettings.Instance.InAppProducts[i].LocalizedPrice = obj.realPrice;
                #endif
            }

            AndroidInAppPurchaseManager.Client.Connect();
        }
Пример #6
0
        /// <summary>
        /// Purchase overload for virtual products, as they differ in their workflow on PlayFab.
        /// Also the virtual currency funds are checked locally before forwarding the request to PlayFab.
        /// </summary>
        public void Purchase(IAPObject obj)
        {
            int curIndex = 0;

            for (int i = 0; i < obj.virtualPrice.Count; i++)
            {
                if (obj.virtualPrice[i].amount != 0)
                {
                    curIndex = i;
                    break;
                }
            }

            //local check before doing the request
            if (DBManager.GetFunds(obj.virtualPrice[curIndex].name) < obj.virtualPrice[curIndex].amount)
            {
                IAPManager.OnPurchaseFailed("Insufficient Funds.");
                return;
            }

            PurchaseItemRequest request = new PurchaseItemRequest()
            {
                ItemId          = obj.id,
                VirtualCurrency = obj.virtualPrice[curIndex].name.Substring(0, 2).ToUpper(),
                Price           = obj.virtualPrice[curIndex].amount
            };

            PlayFabClientAPI.PurchaseItem(request, OnPurchaseSucceeded, OnVirtualPurchaseFailed);
        }
Пример #7
0
        // set product to purchased after successful verification (or without)
        // For non consumable IAPs or subscriptions, alter database entry
        public void PurchaseVerified(string id)
        {
            if (!IAPObjects.ContainsKey(id))
            {
                id = GetIAPIdentifier(id);
            }
            if (!IAPObjects.ContainsKey(id))
            {
                return;
            }
            IAPObject obj = IAPObjects[id];

            //don't continue if the product is already purchased,
            //for example if we just want to verify an existing product again
            if (DBManager.isPurchased(id))
            {
                return;
            }
            if (obj.type != ProductType.Consumable)
            {
                DBManager.SetToPurchased(id);
            }

            purchaseSucceededEvent(id);
        }
Пример #8
0
        /// <summary>
        /// Purchase product based on its product id.
        /// If the productId matches "restore", we restore transactions instead.
        /// Our delegates then fire the appropriate succeeded/fail/restore event.
        /// </summary>
        public static void PurchaseProduct(string productId)
        {
            if (productId == "restore")
            {
                RestoreTransactions();
            }
            else
            {
                IAPObject obj = GetIAPObject(productId);
                if (obj == null)
                {
                    if (isDebug)
                    {
                        Debug.LogError("Product " + productId + " not found in IAP Settings.");
                    }
                    return;
                }

                //distinguish between virtual and real products
                if (obj.isVirtual)
                {
                    PurchaseProduct(obj);
                }
                else
                {
                    controller.InitiatePurchase(controller.products.WithID(productId));
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Set this item to 'purchased' state (true), or unpurchased state (false) for fake purchases.
        /// </summary>
        public void Purchased(bool state)
        {
            //in case we restored an old purchase on a
            //locked item, we have to unlock it first
            Unlock();

            //back to unpurchased state, deselect
            if (!state)
            {
                Deselect();
            }
            //activate the select button
            else if (selectButton)
            {
                selectButton.SetActive(state);
            }

            //initialize variables for a product with upgrades
            IAPObject obj         = IAPManager.GetIAPObject(productId);
            bool      hasUpgrade  = false;
            string    nextUpgrade = obj.req.nextId;

            //in case this good has upgrades, here we find the next upgrade
            //and replace displayed item data in the store with its upgrade details
            if (!string.IsNullOrEmpty(nextUpgrade))
            {
                hasUpgrade = true;
                Init(IAPManager.GetIAPObject(nextUpgrade));
            }

            //take upgrade state into account
            if (state && hasUpgrade)
            {
                state = !hasUpgrade;
            }

            //activate the sold gameobject
            if (sold)
            {
                sold.SetActive(state);
            }

            //hide both buy trigger and buy button, for ignoring further purchase clicks.
            //but don't do that for subscriptions, so that the user could easily renew it
            if ((int)obj.type > 1)
            {
                return;
            }

            if (buyTrigger)
            {
                buyTrigger.SetActive(!state);
                buyButton.SetActive(false);
            }
            else
            {
                buyButton.SetActive(!state);
            }
        }
Пример #10
0
 // Construct IAP product data with their App Store identifiers
 private void RequestProductData(ConfigurationBuilder builder)
 {
     for (int i = 0; i < realIDs.Length; i++)
     {
         IAPObject obj = GetIAPObject(realIDs[i]);
         builder.AddProduct(obj.id, obj.type, obj.GetIDs());
     }
 }
Пример #11
0
        //method for remote catalog config
        //converts a (downloaded) config string for virtual products into JSON nodes and overwrites
        //existing IAP objects with new properties, after doing a null reference check for empty nodes.
        private void ApplyCatalogItem(string id, CatalogItem item)
        {
            IAPObject obj = IAPManager.GetIAPObject(id);

            if (!obj.fetch)
            {
                return;
            }

            switch (obj.editorType)
            {
            case IAPType.Currency:
                foreach (string curKey in item.Bundle.BundledVirtualCurrencies.Keys)
                {
                    IAPCurrency cur = obj.virtualPrice.Find(x => x.name.StartsWith(curKey, System.StringComparison.OrdinalIgnoreCase));
                    if (cur != null)
                    {
                        cur.amount = (int)item.Bundle.BundledVirtualCurrencies[curKey];
                    }
                }
                break;

            case IAPType.Virtual:
                foreach (string curKey in item.VirtualCurrencyPrices.Keys)
                {
                    IAPCurrency cur = obj.virtualPrice.Find(x => x.name.StartsWith(curKey, System.StringComparison.OrdinalIgnoreCase));
                    if (cur != null)
                    {
                        cur.amount = (int)item.VirtualCurrencyPrices[curKey];
                    }
                }
                break;
            }

            if (!string.IsNullOrEmpty(item.CustomData))
            {
                JSONNode data = JSON.Parse(item.CustomData);
                if (!string.IsNullOrEmpty(data["requirement"].ToString()))
                {
                    data = data["requirement"];
                    if (!string.IsNullOrEmpty(data["entry"]))
                    {
                        obj.req.entry = data["entry"];
                    }
                    if (!string.IsNullOrEmpty(data["labelText"]))
                    {
                        obj.req.labelText = data["labelText"];
                    }
                    if (!string.IsNullOrEmpty(data["target"]))
                    {
                        obj.req.target = data["target"].AsInt;
                    }
                }
            }
        }
Пример #12
0
        /// <summary>
        /// Sets a product to purchased after successful verification (or without).
        /// This alters the database entry for non-consumable or products with usage as well.
        /// </summary>
        public void PurchaseVerified(string id)
        {
            if (!IAPObjects.ContainsKey(id))
            {
                id = GetIAPIdentifier(id);
            }
            if (!IAPObjects.ContainsKey(id))
            {
                return;
            }
            IAPObject obj = IAPObjects[id];

            switch (obj.editorType)
            {
            case IAPType.Currency:
                foreach (IAPCurrency cur in obj.virtualPrice)
                {
                    DBManager.IncreaseFunds(cur.name, cur.amount);
                }
                break;

            default:
                //for consumables, add the defined usage count to tracking in player data
                //on non-consumables, don't continue if the product is already purchased,
                //for example if we just want to verify an existing product again
                switch (obj.type)
                {
                case ProductType.Consumable:
                    if (obj.usageCount > 0)
                    {
                        DBManager.IncreasePlayerData(id, obj.usageCount);

                        //update server player data
                                #if PLAYFAB && !PLAYFAB_VALIDATION
                        PlayfabManager.SetPlayerData();
                                #endif
                    }
                    break;

                default:
                    if (DBManager.GetPurchase(id) > 0)
                    {
                        return;
                    }

                    DBManager.IncreasePurchase(id, 1);
                    break;
                }
                break;
            }

            purchaseSucceededEvent(id);
        }
Пример #13
0
 /// <summary>
 /// Method for overwriting shop item's properties with online IAP data
 /// from the App Store servers (those in the developer console).
 /// When we receive the online item list of products from the IAPManager,
 /// we loop over our products and check if 'fetch' was checked in the
 /// IAP Settings editor, then simply reinitialize the items with new data.
 /// </summary>
 public static void OverwriteWithFetch(Product[] products)
 {
     for (int i = 0; i < products.Length; i++)
     {
         string    id   = products[i].definition.id;
         IAPObject item = IAPManager.GetIAPObject(id);
         if (item != null && item.fetch && instance.IAPItems.ContainsKey(id))
         {
             instance.IAPItems[id].Init(products[i]);
         }
     }
 }
Пример #14
0
 /// <summary>
 /// method for overwriting shop item's properties with online IAP data
 /// from Google's or Apple's servers (those in the developer console).
 /// When we receive the online item list of IAPProducts from IAPManager,
 /// we loop over our products and check if 'fetch' was checked in the
 /// IAP Settings editor, then simply reinitialize the items with new info
 /// </summary>
 public static void OverwriteWithFetch(List <IAPArticle> products)
 {
     for (int i = 0; i < products.Count; i++)
     {
         string    id   = IAPManager.GetIAPIdentifier(products[i].id);
         IAPObject item = IAPManager.GetIAPObject(id);
         if (item != null && item.fetch && instance.IAPItems.ContainsKey(id))
         {
             instance.IAPItems[id].Init(products[i]);
         }
     }
 }
Пример #15
0
        /// <summary>
        /// returns a list of all upgrade ids associated to a product
        /// </summary>
        public static List <string> GetIAPUpgrades(string productId)
        {
            List <string> list = new List <string>();
            IAPObject     obj  = GetIAPObject(productId);

            while (!string.IsNullOrEmpty(obj.req.nextId))
            {
                list.Add(obj.req.nextId);
                obj = GetIAPObject(obj.req.nextId);
            }

            return(list);
        }
Пример #16
0
        /// <summary>
        /// Refreshes the visual representation of a specific shop item.
        /// This is called automatically because of subscribing to the DBManager update event.
        /// It also means saving performance due to not refreshing all items every time.
        /// </summary>
        public void Refresh(string id)
        {
            //this method is based on data from the database,
            //so if we don't have a DBManager instance don't continue
            if (!DBManager.GetInstance())
            {
                return;
            }

            IAPObject obj  = IAPManager.GetIAPObject(id);
            IAPItem   item = instance.IAPItems.ContainsKey(id) ? instance.IAPItems[id] : null;

            if (obj == null || item == null || item.productId != id)
            {
                return;
            }

            bool isSelected  = DBManager.GetSelected(id);
            bool isPurchased = DBManager.GetPurchase(id) > 0;

            //double check that selected items are actually owned
            //if not, correct the entry by setting it to deselected
            if (isSelected && !isPurchased)
            {
                DBManager.SetDeselected(id);
                isSelected = false;
            }

            if (isPurchased)
            {
                item.Purchased(true);

                //in case the item has been selected before, but also auto-select one item per group
                //more items per group can be pre-selected manually e.g. on app launch
                if (isSelected || (item.selectButton && !item.deselectButton &&
                                   DBManager.GetSelectedGroup(IAPManager.GetIAPObjectGroupName(id)).Count == 0))
                {
                    item.IsSelected(true);
                }
            }
            else if (!string.IsNullOrEmpty(obj.req.entry) && DBManager.isRequirementMet(obj.req))
            {
                //check if a requirement is set up for this item,
                //then unlock if the requirement has been met
                if (IAPManager.isDebug)
                {
                    Debug.Log("requirement met for: " + obj.id);
                }
                item.Unlock();
            }
        }
Пример #17
0
        /// <summary>
        /// Unlocks items if the requirement for them has been met. You can
        /// call this method at runtime whenever the player made some
        /// progress, to ensure your shop items reflect the current state.
        /// </summary>
        public static void UnlockItems()
        {
            //this method is based on data from the database,
            //so if we don't have a DBManager instance don't continue
            if (!DBManager.GetInstance())
            {
                return;
            }

            //get list of all shop groups from IAPManager
            List <IAPGroup> list = IAPManager.GetInstance().IAPs;

            //loop over groups
            for (int i = 0; i < list.Count; i++)
            {
                //cache current group
                IAPGroup group = list[i];

                //loop over items
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache IAP object
                    IAPObject obj = group.items[j];
                    if (obj.req == null)
                    {
                        continue;
                    }

                    //cache reference to IAP item instance
                    IAPItem item = GetIAPItem(obj.id);

                    //check if the item reference is empty or set to purchased already
                    if (item == null || DBManager.isPurchased(obj.id))
                    {
                        continue;
                    }

                    //check if a requirement is set up for this item,
                    //then unlock if the requirement has been met
                    if (!string.IsNullOrEmpty(obj.req.entry) &&
                        DBManager.isRequirementMet(obj.req))
                    {
                        if (IAPManager.isDebug)
                        {
                            Debug.Log("requirement met for: " + obj.id);
                        }
                        item.Unlock();
                    }
                }
            }
        }
Пример #18
0
        /// <summary>
        /// Returns the next unpurchased upgrade id of a product.
        /// </summary>
        public static string GetNextUpgrade(string productId)
        {
            string    id  = GetCurrentUpgrade(productId);
            IAPObject obj = GetIAPObject(id);

            if (!DBManager.isPurchased(id) || obj == null || string.IsNullOrEmpty(obj.req.nextId))
            {
                return(id);
            }
            else
            {
                return(obj.req.nextId);
            }
        }
Пример #19
0
        // initialize IAP ids:
        // populate IAP dictionary and arrays with product ids
        private void InitIds()
        {
            //create temporary list for all IAPGroups,
            //as well as a list only for real money purchases
            List <IAPGroup> idsList = GetIAPs();
            List <string>   ids     = new List <string>();

            if (idsList.Count == 0)
            {
                Debug.LogError("Initializing IAPManager, but IAP List is empty."
                               + " Did you set up IAPs in the IAP Settings?");
            }

            //loop over all groups
            for (int i = 0; i < idsList.Count; i++)
            {
                //cache current group
                IAPGroup group = idsList[i];
                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item
                    IAPObject obj = group.items[j];
                    if (String.IsNullOrEmpty(obj.id))
                    {
                        Debug.LogError("Found IAP Object in IAP Settings without an ID."
                                       + " This will cause errors during runtime.");
                    }

                    //add this IAPObject to the dictionary of id <> IAPObject
                    IAPObjects.Add(obj.id, obj);
                    //if it's an IAP for real money, also add it to the id list
                    if (obj.type == IAPType.consumable || obj.type == IAPType.nonConsumable ||
                        obj.type == IAPType.subscription)
                    {
                        ids.Add(obj.GetIdentifier());
                    }
                }
            }

            //don't add the restore button to the list of online purchases
            if (ids.Contains("restore"))
            {
                ids.Remove("restore");
            }
            //convert and store list of real money IAP ids as string array,
            //this array is being used for initializing Google/Apple's billing system
            this.ids = ids.ToArray();
        }
Пример #20
0
        /// <summary>
        /// Method for overwriting shop item's properties with localized IAP data from the App Store servers. When we receive
        /// the online item list of products from the IAPManager, we loop over our products and check if 'fetch' was checked
        /// in the IAP Settings editor, then simply reinitialize the items by using the new data.
        /// </summary>
        public static void OverwriteWithFetch(Product[] products)
        {
            for (int i = 0; i < products.Length; i++)
            {
                string    id   = products[i].definition.id;
                IAPObject item = IAPManager.GetIAPObject(id);

                if (item == null || !item.fetch || !instance.IAPItems.ContainsKey(id) || item.editorType == IAPType.Virtual)
                {
                    continue;
                }

                instance.IAPItems[id].Init(products[i]);
            }
        }
Пример #21
0
        // online verification request (Android version)
        // here we build the POST request to our external server,
        // that will forward the request to Google's servers
        private void MakeRequest(GooglePurchase prod)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("store", "Android");
            dic.Add("pid", prod.productId);
            dic.Add("tid", prod.orderId);
            dic.Add("rec", prod.purchaseToken);
            IAPObject obj = GetIAPObject(prod.productId);

            if (obj != null && obj.type == IAPType.subscription)
            {
                dic.Add("type", "subs");
            }
            StartCoroutine(WaitForRequest(dic));
        }
Пример #22
0
        // initialize IAP ids:
        // populate IAP dictionary and arrays with product ids
        private void InitIds()
        {
            //create a list only for real money purchases
            List <string> ids = new List <string>();

            if (IAPs.Count == 0)
            {
                Debug.LogError("Initializing IAPManager, but IAP List is empty."
                               + " Did you set up IAPs in the IAP Settings?");
            }

            //loop over all groups
            for (int i = 0; i < IAPs.Count; i++)
            {
                //cache current group
                IAPGroup group = IAPs[i];
                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item
                    IAPObject obj = group.items[j];

                    if (String.IsNullOrEmpty(obj.id) || IAPObjects.ContainsKey(obj.id))
                    {
                        Debug.LogError("Found IAP Object in IAP Settings without an identifier "
                                       + " or " + obj.id + " does exist already. Skipping product.");
                        continue;
                    }

                    //add this IAPObject to the dictionary of id <> IAPObject
                    IAPObjects.Add(obj.id, obj);
                    //if it's an IAP for real money, add it to the id list
                    if (!obj.isVirtual)
                    {
                        ids.Add(obj.id);
                    }
                }
            }

            //don't add the restore button to the list of online purchases
            if (ids.Contains("restore"))
            {
                ids.Remove("restore");
            }
            //convert and store list of real money IAP ids as string array
            realIDs = ids.ToArray();
        }
Пример #23
0
        /// <summary>
        /// Returns the group name of a specific product id.
        /// Used by DBManager
        /// <summary>
        public static string GetIAPObjectGroupName(string id)
        {
            if (instance.IAPObjects.ContainsKey(id))
            {
                IAPObject obj = GetIAPObject(id);
                //loop over groups to find the product id,
                //then return the name of the group
                for (int i = 0; i < instance.IAPs.Count; i++)
                {
                    if (instance.IAPs[i].items.Contains(obj))
                    {
                        return(instance.IAPs[i].name);
                    }
                }
            }

            //if the corresponding group has not been found
            return(null);
        }
Пример #24
0
        /// <summary>
        /// Overload for purchasing virtual product based on its product identifier.
        /// </summary>
        public static void PurchaseProduct(IAPObject obj)
        {
            string productId = obj.id;

            //product is set to already owned, this should not happen
            if (obj.type != ProductType.Consumable && DBManager.GetPurchase(productId) > 0)
            {
                OnPurchaseFailed("Product already owned.");
                return;
            }

            #if PLAYFAB && !PLAYFAB_VALIDATION
            new PlayfabStore().Purchase(obj);
            return;
            #endif

            //check whether the player has enough funds
            bool didSucceed = DBManager.VerifyVirtualPurchase(obj);
            if (isDebug)
            {
                Debug.Log("Purchasing virtual product " + productId + ", result: " + didSucceed);
            }
            //on success, non-consumables are saved to the database. This automatically
            //saves the new substracted fund value, then and fire the succeeded event
            if (didSucceed)
            {
                if (obj.type == ProductType.Consumable)
                {
                    DBManager.IncreasePlayerData(productId, obj.usageCount);
                    purchaseSucceededEvent(productId);
                }
                else
                {
                    DBManager.SetPurchase(obj.id);
                }
                purchaseSucceededEvent(productId);
            }
            else
            {
                OnPurchaseFailed("Insufficient funds.");
            }
        }
Пример #25
0
 /// <summary>
 /// returns the group name of a specific product id.
 /// Used by DBManager, depends on platform
 /// <summary>
 public static string GetIAPObjectGroupName(string id)
 {
     if (instance.IAPObjects.ContainsKey(id))
     {
         //cache object and create temporary group list
         IAPObject       obj    = instance.IAPObjects[id];
         List <IAPGroup> groups = GetIAPs();
         //loop over groups to find the product id,
         //then return the name of the group
         for (int i = 0; i < groups.Count; i++)
         {
             if (groups[i].items.Contains(obj))
             {
                 return(groups[i].name);
             }
         }
     }
     //if the corresponding group has not been found
     return(null);
 }
Пример #26
0
        /// <summary>
        /// Returns the purchase amount of a product.
        /// </summary>
        public static int GetPurchase(string id)
        {
            //if the product exists, return purchase amount
            if (instance.gameData[contentKey][id] != null)
            {
                return(instance.gameData[contentKey][id].AsInt);
            }
            else
            {
                //check if the product is available for free
                IAPObject obj = IAPManager.GetIAPObject(id);
                if (obj.editorType == IAPType.Virtual && !obj.virtualPrice.Any(x => x.amount > 0))
                {
                    return(1);
                }
            }

            //otherwise return zero as default
            return(0);
        }
Пример #27
0
        /*
         * public void CloudMoolahButton()
         * {
         *  extensions.GetExtension<IMoolahExtension>().FastRegister(DBManager.GetDeviceId(), (string moolahName) =>
         *  {
         *      //register succeeded
         *      extensions.GetExtension<IMoolahExtension>().Login(moolahName, DBManager.GetDeviceId(), (LoginResultState loginResult, string error) =>
         *      {
         *          //login succeeded?
         *          Debug.Log("Moolah Login State: " + loginResult + ", " + error);
         *      });
         *  }, (FastRegisterError registerResult, string error) =>
         *  {
         *      //register failed
         *      Debug.Log("Moolah Register Error: " + registerResult + ", " + error);
         *  });
         * }
         */


        /// <summary>
        /// Purchase product based on its product id. If the productId matches "restore", we restore transactions instead.
        /// Our delegates then fire the appropriate succeeded/fail/restore event.
        /// </summary>
        public static void PurchaseProduct(string productId)
        {
            #if UNITY_PURCHASING
            if (productId == "restore")
            {
                RestoreTransactions();
                return;
            }
            #endif

            IAPObject obj = GetIAPObject(productId);
            if (obj == null)
            {
                if (isDebug)
                {
                    Debug.LogError("Product " + productId + " not found in IAP Settings.");
                }
                return;
            }

            //distinguish between virtual and real products
            if (obj.editorType == IAPType.Virtual)
            {
                PurchaseProduct(obj);
                return;
            }

            #if UNITY_PURCHASING
            if (controller == null)
            {
                if (ShopManager.GetInstance())
                {
                    ShopManager.ShowMessage("Billing is not available.");
                }
                Debug.LogError("Unity IAP is not initialized correctly! Please check your billing settings.");
                return;
            }

            controller.InitiatePurchase(controller.products.WithID(productId));
            #endif
        }
Пример #28
0
        //converting Unity IAP product definitions into the VoxelBuster format
        private BillingProduct ConvertProduct(ProductDefinition product)
        {
            IAPObject obj = IAPManager.GetIAPObject(product.id);

            if (obj == null)
            {
                return(null);
            }

            List <VoxelBusters.NativePlugins.PlatformID> platformIds = new List <VoxelBusters.NativePlugins.PlatformID>();

            platformIds.Add(VoxelBusters.NativePlugins.PlatformID.Editor(product.id));

                        #if UNITY_ANDROID
            platformIds.Add(VoxelBusters.NativePlugins.PlatformID.Android(product.storeSpecificId));
                        #elif UNITY_IOS || UNITY_TVOS
            platformIds.Add(VoxelBusters.NativePlugins.PlatformID.IOS(product.storeSpecificId));
                        #endif

            return(BillingProduct.Create(obj.title, product.type == ProductType.Consumable ? true : false, platformIds.ToArray()));
        }
Пример #29
0
        // Once we've received the productList, we create a
        // cross-platform version of it and overwrite
        // the existing shop item values with this online data
        // Optional: verify old purchases online.
        private void ProductDataReceived(Inventory inv)
        {
            //store fetched inventory for later access
            inventory = inv;

            //check for non-consumed consumables
            List <Purchase> prods = inv.GetAllPurchases();

            for (int i = 0; i < prods.Count; i++)
            {
                IAPObject obj = GetIAPObject(prods[i].Sku);
                if (obj != null && obj.type == IAPType.consumable)
                {
                    OpenIAB.consumeProduct(prods[i]);
                }
            }

                        #if UNITY_ANDROID
            if ((verificationType == VerificationType.onStart || verificationType == VerificationType.both) &&
                !string.IsNullOrEmpty(serverUrl))
            {
                VerifyReceipts();
            }
                        #endif

            //build live cache
            productCache = new List <IAPArticle>();
            for (int i = 0; i < ids.Length; i++)
            {
                if (inv.GetSkuDetails(ids[i]) != null)
                {
                    productCache.Add(new IAPArticle(inv.GetSkuDetails(ids[i])));
                }
            }

            if (ShopManager.GetInstance())
            {
                ShopManager.OverwriteWithFetch(productCache);
            }
        }
Пример #30
0
        // online verification request
        // here we build the POST request to our external server,
        // that will forward the request to the verification server
        private void MakeRequest(Purchase prod)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();

                        #if UNITY_ANDROID
            dic.Add("store", "Android");
            dic.Add("pid", prod.Sku);
            dic.Add("tid", prod.OrderId);
            dic.Add("rec", prod.Token);
            IAPObject obj = GetIAPObject(prod.Sku);
            if (obj != null && obj.type == IAPType.subscription)
            {
                dic.Add("type", "subs");
            }
                        #elif UNITY_IPHONE
            dic.Add("store", "IOS");
            dic.Add("pid", prod.Sku);
            dic.Add("rec", prod.OriginalJson);
                        #endif

            StartCoroutine(WaitForRequest(dic));
        }
Пример #31
0
        //draw specific settings for the selected type
        void DrawTypeSettings(IAPObject obj)
        {
            switch (obj.type)
            {
                case IAPType.VirtualCurrencyPack:
                    int currencyIndex = 0;
                    for (int k = 0; k < currencyNames.Length; k++)
                    {
                        if (obj.specific == currencyNames[k])
                            currencyIndex = k;
                    }
                    currencyIndex = EditorGUILayout.Popup(currencyIndex, currencyNames, GUILayout.Width(100));
                    obj.specific = currencyNames[currencyIndex];
                    obj.amount = EditorGUILayout.IntField(obj.amount, GUILayout.Width(60));
                    break;
                case IAPType.SingleUsePackVG:
                    if (string.IsNullOrEmpty(obj.specific)) obj.specific = "[GOOD ID]";
                    obj.specific = EditorGUILayout.TextField(obj.specific, GUILayout.Width(100));
                    obj.amount = EditorGUILayout.IntField(obj.amount, GUILayout.Width(60)); ;
                    break;
                case IAPType.UpgradeVG:
                    if (string.IsNullOrEmpty(obj.specific)) obj.specific = "[ID];[PREV];[NEXT]";
                    string[] props = obj.specific.Split(';');
                    for (int k = 0; k < props.Length; k++)
                        props[k] = EditorGUILayout.TextField(props[k], GUILayout.Width(52));
                    obj.specific = props[0] + ";" + props[1] + ";" + props[2];
                    break;
                case IAPType.EquippableVG:
                    string[] equippingModel = new string[] { EquippableVG.EquippingModel.LOCAL.ToString(),
                                                                     EquippableVG.EquippingModel.CATEGORY.ToString(),
                                                                     EquippableVG.EquippingModel.GLOBAL.ToString() };
                    int equippingIndex = 0;
                    for (int k = 0; k < equippingModel.Length; k++)
                    {
                        if (obj.specific == equippingModel[k])
                            equippingIndex = k;
                    }

                    equippingIndex = EditorGUILayout.Popup(equippingIndex, equippingModel, GUILayout.Width(164));
                    obj.specific = equippingModel[equippingIndex];
                    break;
                default:
                    EditorGUILayout.LabelField("--------------------------------", GUILayout.Width(164));
                    break;
            }
        }
Пример #32
0
        //draws the in game content editor
        void DrawIGC(List<IAPGroup> list)
        {
            EditorGUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.yellow;

            //draw currencies up to a maximum of 9
            //(there is no limitation, but 9 currencies do fit in the window nicely,
            //and there really shouldnt be a reason to have 9+ different currencies)
            if (script.currency.Count < 9)
            {
                //button for adding a new currency
                if (GUILayout.Button("Add Currency"))
                {
                    //create new currency, then loop over items
                    //and add a new currency slot for each of them 
                    IAPCurrency currency = new IAPCurrency();
                    script.currency.Add(currency);
                    return;
                }
            }
            else
            {
                //for more than 9 currencies,
                //we show a transparent button with no functionality
                GUI.backgroundColor = new Color(1, 0.9f, 0, 0.4f);
                if (GUILayout.Button("Add Currency"))
                { }
            }

            GUI.backgroundColor = Color.yellow;
            //draw yellow button for adding a new IAP group
            if (GUILayout.Button("Add new Category"))
            {
                //create new group, give it a generic name based on
                //the current system time and add it to the list of groups
                IAPGroup newGroup = new IAPGroup();
                string timestamp = GenerateUnixTime();
                newGroup.name = "Grp " + timestamp;
                newGroup.id = timestamp;
                list.Add(newGroup);
                return;
            }

            GUI.backgroundColor = Color.white;
            EditorGUILayout.EndHorizontal();

            //begin a scrolling view inside tab, pass in current Vector2 scroll position 
            scrollPosIGC = EditorGUILayout.BeginScrollView(scrollPosIGC, GUILayout.Height(462));
            GUILayout.Space(10);

            //only draw a box behind currencies if there are any
            if (script.currency.Count > 0)
            {
                EditorGUILayout.LabelField("Currencies:", EditorStyles.boldLabel);
                GUILayout.Space(10);
                GUI.Box(new Rect(10, 35, script.currency.Count * 110, 65), "");
            }

            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            //loop through currencies
            for (int i = 0; i < script.currency.Count; i++)
            {
                IAPCurrency current = script.currency[i];
                EditorGUILayout.BeginVertical();
                //draw currency properties,
                //such as name and amount
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Name", GUILayout.Width(44));
                current.name = EditorGUILayout.TextField(current.name, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Default", GUILayout.Width(44));
                script.currency[i].amount = EditorGUILayout.IntField(script.currency[i].amount, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();

                //button for deleting a currency
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(52);
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(54)))
                {
                    //ask again before deleting the currency,
                    //as deleting it could cause angry customers!
                    //it's probably better not to remove currencies in production versions
                    if (EditorUtility.DisplayDialog("Delete Currency?",
                        "Existing users might lose their funds associated with this currency when updating.",
                        "Continue", "Abort"))
                    {
                        //then remove the currency
                        script.currency.RemoveAt(i);
                        break;
                    }
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();
            }

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(30);

            //loop over IAP groups
            for (int i = 0; i < list.Count; i++)
            {
                //cache group
                IAPGroup group = list[i];
                Container shopGroup = null;
                if (shop)
                {
                    shopGroup = shop.GetContainer(group.id);
                    if (shopGroup == null)
                    {
                        shopGroup = new Container();
                        shopGroup.id = group.id;
                        shop.containers.Add(shopGroup);
                    }
                }

                EditorGUILayout.BeginHorizontal();
                GUI.backgroundColor = Color.yellow;
                //button for adding a new IAPObject (product) to this group
                if (GUILayout.Button("New Object", GUILayout.Width(120)))
                {
                    IAPObject obj = new IAPObject();
                    group.items.Add(obj);
                    break;
                }
                GUI.backgroundColor = Color.white;

                //draw group properties
                EditorGUILayout.LabelField("Category:", GUILayout.Width(60));
                group.name = EditorGUILayout.TextField(group.name, GUILayout.Width(90));
                GUILayout.FlexibleSpace();

                if (!shop)
                    EditorGUILayout.LabelField("No ShopManager prefab found in this scene!", GUILayout.Width(300));
                else
                {
                    EditorGUILayout.LabelField("Prefab:", GUILayout.Width(45));
                    shopGroup.prefab = (GameObject)EditorGUILayout.ObjectField(shopGroup.prefab, typeof(GameObject), false, GUILayout.Width(100));
                    GUILayout.Space(10);
                    EditorGUILayout.LabelField("Parent:", GUILayout.Width(45));
                    shopGroup.parent = (Transform)EditorGUILayout.ObjectField(shopGroup.parent, typeof(Transform), true, GUILayout.Width(100));
                }

                GUILayout.FlexibleSpace();

                //same as in DrawIAP(),
                //move group up & down buttons
                int groupUpWidth = 22;
                int groupDownWidth = 22;
                if (i == 0) groupDownWidth = 48;
                if (i == list.Count - 1) groupUpWidth = 48;

                if (i > 0 && GUILayout.Button("▲", GUILayout.Width(groupUpWidth)))
                {
                    list[i] = list[i - 1];
                    list[i - 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }
                if (i < list.Count - 1 && GUILayout.Button("▼", GUILayout.Width(groupDownWidth)))
                {
                    list[i] = list[i + 1];
                    list[i + 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }

                //button for removing a group including items
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(20)))
                {
                    if(shop) shop.containers.Remove(shopGroup);
                    list.RemoveAt(i);
                    break;
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));

                //draw header information for each item property
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("ID:", GUILayout.Width(120));
                EditorGUILayout.LabelField("Icon:", GUILayout.Width(65));
                EditorGUILayout.LabelField("Type:", GUILayout.Width(165));
                EditorGUILayout.LabelField("Title:", GUILayout.Width(122));
                if (group.items.Count == 1) GUILayout.Space(50);
                EditorGUILayout.LabelField("Description:", GUILayout.Width(150));
                EditorGUILayout.LabelField("Specific Settings:", GUILayout.Width(135));
                EditorGUILayout.LabelField("Price:", GUILayout.Width(100));
                EditorGUILayout.EndHorizontal();

                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item reference
                    IAPObject obj = group.items[j];
                    EditorGUILayout.BeginHorizontal();
                    IAPType selectedType = obj.type;

                    //draw IAPObject (item/product) properties
                    obj.id = EditorGUILayout.TextField(obj.id, GUILayout.Width(120));
                    obj.icon = EditorGUILayout.ObjectField(obj.icon, typeof(Sprite), false, GUILayout.Width(65)) as Sprite;
                    obj.type = (IAPType)EditorGUILayout.EnumPopup(obj.type, GUILayout.Width(110));
                    obj.title = EditorGUILayout.TextField(obj.title);
                    obj.description = EditorGUILayout.TextField(obj.description);

                    if (obj.type != selectedType)
                    {
                        obj.specific = "";
                        obj.amount = 0;
                    }

                    DrawTypeSettings(obj);

                    //price field
                    int priceIndex = 0;
                    for (int k = 0; k < currencyNames.Length; k++)
                    {
                        if (obj.virtualPrice.name == currencyNames[k])
                            priceIndex = k;
                    }
                    priceIndex = EditorGUILayout.Popup(priceIndex, currencyNames, GUILayout.Width(60));
                    obj.virtualPrice.name = currencyNames[priceIndex];
                    obj.virtualPrice.amount = EditorGUILayout.IntField(obj.virtualPrice.amount, GUILayout.Width(60));

                    //same as in DrawIAP(), requirement button
                    if (!string.IsNullOrEmpty(obj.req.entry))
                        GUI.backgroundColor = Color.yellow;
                    if (GUILayout.Button("R", GUILayout.Width(20)))
                    {
                        reqEditor = (RequirementEditor)EditorWindow.GetWindowWithRect(typeof(RequirementEditor), new Rect(0, 0, 300, 150), false, "Requirement");
                        reqEditor.obj = obj;
                    }

                    GUI.backgroundColor = Color.white;
                    //same as in DrawIAP(), move item up & down buttons
                    int buttonUpWidth = 22;
                    int buttonDownWidth = 22;
                    if (j == 0) buttonDownWidth = 48;
                    if (j == group.items.Count - 1) buttonUpWidth = 48;

                    if (j > 0 && GUILayout.Button("▲", GUILayout.Width(buttonUpWidth)))
                    {
                        group.items[j] = group.items[j - 1];
                        group.items[j - 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }
                    if (j < group.items.Count - 1 && GUILayout.Button("▼", GUILayout.Width(buttonDownWidth)))
                    {
                        group.items[j] = group.items[j + 1];
                        group.items[j + 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }

                    //button for removing an item of the group
                    GUI.backgroundColor = Color.gray;
                    if (GUILayout.Button("X"))
                    {
                        group.items.RemoveAt(j);
                        break;
                    }
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.EndHorizontal();
                }
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                GUILayout.Space(30);
            }

            //ends the scrollview defined above
            EditorGUILayout.EndScrollView();
        }
Пример #33
0
        //draws the in app purchase editor
        void DrawIAP(List<IAPGroup> list)
        {
            EditorGUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.yellow;

            //draw yellow button for adding a new IAP group
            if (GUILayout.Button("Add new Category"))
            {
                //create new group, give it a generic name based on
                //the current unix time and add it to the list of groups
                IAPGroup newGroup = new IAPGroup();
                string timestamp = GenerateUnixTime();
                newGroup.name = "Grp " + timestamp;
                newGroup.id = timestamp;
                list.Add(newGroup);
                return;
            }

            GUI.backgroundColor = Color.white;
            EditorGUILayout.EndHorizontal();

            //begin a scrolling view inside this tab, pass in current Vector2 scroll position 
            scrollPosIAP = EditorGUILayout.BeginScrollView(scrollPosIAP, GUILayout.Height(462));
            GUILayout.Space(20);

            //loop over IAP groups
            for (int i = 0; i < list.Count; i++)
            {
                //cache group
                IAPGroup group = list[i];
                //populate shop container variables if ShopManager is present
                Container shopGroup = null;
                if (shop)
                {
                    shopGroup = shop.GetContainer(group.id);
                    if (shopGroup == null)
                    {
                        shopGroup = new Container();
                        shopGroup.id = group.id;
                        shop.containers.Add(shopGroup);
                    }
                }

                EditorGUILayout.BeginHorizontal();
                GUI.backgroundColor = Color.yellow;
                //button for adding a new IAPObject (product) to this group
                if (GUILayout.Button("New Object", GUILayout.Width(120)))
                {
                    IAPObject newObj = new IAPObject();
                    //add platform dependent ids to the local list
                    int platforms = System.Enum.GetValues(typeof(IAPPlatform)).Length;
                    for (int j = 0; j < platforms; j++)
                        newObj.localId.Add(new IAPIdentifier());

                    group.items.Add(newObj);
                    break;
                }

                //draw group properties
                GUI.backgroundColor = Color.white;
                EditorGUILayout.LabelField("Category:", GUILayout.Width(60));
                group.name = EditorGUILayout.TextField(group.name, GUILayout.Width(90));
                GUILayout.FlexibleSpace();

                if (!shop)
                    EditorGUILayout.LabelField("No ShopManager prefab found in this scene!", GUILayout.Width(300));
                else
                {
                    EditorGUILayout.LabelField("Prefab:", GUILayout.Width(45));
                    shopGroup.prefab = (GameObject)EditorGUILayout.ObjectField(shopGroup.prefab, typeof(GameObject), false, GUILayout.Width(100));
                    GUILayout.Space(10);
                    EditorGUILayout.LabelField("Parent:", GUILayout.Width(45));
                    shopGroup.parent = (Transform)EditorGUILayout.ObjectField(shopGroup.parent, typeof(Transform), true, GUILayout.Width(100));
                }
                
                GUILayout.FlexibleSpace();

                //button width for up & down buttons. These should always be at the same width,
                //so if there's only one button (e.g. if there's only one group), the width must be extended
                int groupUpWidth = 22;
                int groupDownWidth = 22;
                if (i == 0) groupDownWidth = 48;
                if (i == list.Count - 1) groupUpWidth = 48;

                //draw up & down buttons for re-ordering groups
                //this will simply switch references in the list
                //hotControl and keyboardControl unsets current mouse focus
                if (i > 0 && GUILayout.Button("▲", GUILayout.Width(groupUpWidth)))
                {
                    list[i] = list[i - 1];
                    list[i - 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }
                if (i < list.Count - 1 && GUILayout.Button("▼", GUILayout.Width(groupDownWidth)))
                {
                    list[i] = list[i + 1];
                    list[i + 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }

                //button for removing a group including items
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(20)))
                {
                    if(shop) shop.containers.Remove(shopGroup);
                    list.RemoveAt(i);
                    break;
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));

                //draw header information for each item property
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(58);
                EditorGUILayout.LabelField("ID:", GUILayout.Width(100));
                EditorGUILayout.LabelField("Icon:", GUILayout.Width(65));
                EditorGUILayout.LabelField("Type:", GUILayout.Width(80));
                EditorGUILayout.LabelField("Fetch:", GUILayout.Width(100));
                EditorGUILayout.LabelField("Title:", GUILayout.Width(145));
                if (group.items.Count == 1) GUILayout.Space(50);
                EditorGUILayout.LabelField("Description:", GUILayout.Width(150));
                EditorGUILayout.LabelField("Specific Settings:", GUILayout.Width(135));
                EditorGUILayout.LabelField("Price:", GUILayout.Width(100));
                EditorGUILayout.EndHorizontal();

                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item reference
                    IAPObject obj = group.items[j];

                    EditorGUILayout.BeginHorizontal();

                    obj.platformFoldout = EditorGUILayout.Foldout(obj.platformFoldout, "");
                    IAPType selectedType = obj.type;

                    //draw IAPObject (item/product) properties
                    obj.id = EditorGUILayout.TextField(obj.id, GUILayout.Width(100));
                    obj.icon = EditorGUILayout.ObjectField(obj.icon, typeof(Sprite), false, GUILayout.Width(65)) as Sprite;
                    obj.type = (IAPType)EditorGUILayout.EnumPopup(obj.type, GUILayout.Width(90));
                    obj.fetch = EditorGUILayout.Toggle(obj.fetch, GUILayout.Width(20));
                    obj.title = EditorGUILayout.TextField(obj.title);
                    obj.description = EditorGUILayout.TextField(obj.description);

                    if (obj.type != selectedType)
                    {
                        obj.specific = "";
                        obj.amount = 0;
                    }

                    DrawTypeSettings(obj);

                    obj.realPrice = EditorGUILayout.TextField(obj.realPrice, GUILayout.Width(60));

                    //button for adding a requirement to this item
                    if (!string.IsNullOrEmpty(obj.req.entry))
                        GUI.backgroundColor = Color.yellow;
                    if (GUILayout.Button("R", GUILayout.Width(20)))
                    {
                        reqEditor = (RequirementEditor)EditorWindow.GetWindowWithRect(typeof(RequirementEditor), new Rect(0, 0, 300, 150), false, "Requirement");
                        reqEditor.obj = obj;
                    }

                    GUI.backgroundColor = Color.white;
                    //do the same here as with the group up & down buttons
                    //(see above)
                    int buttonUpWidth = 22;
                    int buttonDownWidth = 22;
                    if (j == 0) buttonDownWidth = 48;
                    if (j == group.items.Count - 1) buttonUpWidth = 48;

                    //draw up & down buttons for re-ordering items in a group
                    //this will simply switch references in the list
                    if (j > 0 && GUILayout.Button("▲", GUILayout.Width(buttonUpWidth)))
                    {
                        group.items[j] = group.items[j - 1];
                        group.items[j - 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }
                    if (j < group.items.Count - 1 && GUILayout.Button("▼", GUILayout.Width(buttonDownWidth)))
                    {
                        group.items[j] = group.items[j + 1];
                        group.items[j + 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }

                    //button for removing an item of the group
                    GUI.backgroundColor = Color.gray;
                    if (GUILayout.Button("X"))
                    {
                        group.items.RemoveAt(j);
                        break;
                    }
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.EndHorizontal();

                    //draw platform override foldout
                    if (obj.platformFoldout)
                    {
                        EditorGUILayout.LabelField("Platform ID Overrides");
                        for (int k = 0; k < obj.localId.Count; k++)
                        {
                            EditorGUILayout.BeginHorizontal();
                            GUILayout.Space(40);
                            obj.localId[k].overridden = EditorGUILayout.BeginToggleGroup("", obj.localId[k].overridden);
                            EditorGUILayout.BeginHorizontal();
                            obj.localId[k].id = EditorGUILayout.TextField(obj.localId[k].id, GUILayout.Width(120));
                            EditorGUILayout.LabelField(((IAPPlatform)k).ToString());
                            EditorGUILayout.EndHorizontal();
                            EditorGUILayout.EndToggleGroup();
                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                GUILayout.Space(30);
            }

            //ends the scrollview defined above
            EditorGUILayout.EndScrollView();
        }
Пример #34
0
        //each CurrencyPack gets created seperately
        VirtualCurrencyPack CreateCurrencyPack(IAPObject obj, bool market)
        {
            PurchaseType purchaseType = null;
            if(market)
                purchaseType = new PurchaseWithMarket(obj.GetIdentifier(), 0.00);
            else
                purchaseType = new PurchaseWithVirtualItem(obj.virtualPrice.name, obj.virtualPrice.amount);

            return new VirtualCurrencyPack(obj.title, obj.description, obj.id, obj.amount,
                                           obj.specific, purchaseType);
        }
Пример #35
0
        //each VirtualGood gets created seperately
        VirtualGood CreateGood(IAPObject obj, bool market)
        {
            PurchaseType purchaseType = null;
            if (market)
                purchaseType = new PurchaseWithMarket(obj.GetIdentifier(), 0.00);
            else
                purchaseType = new PurchaseWithVirtualItem(obj.virtualPrice.name, obj.virtualPrice.amount);

            switch (obj.type)
            {
                case IAPType.SingleUseVG:
                    return new SingleUseVG(obj.title, obj.description, obj.id, purchaseType);
                case IAPType.SingleUsePackVG:
                    return new SingleUsePackVG(obj.specific, obj.amount, obj.title, obj.description, obj.id, purchaseType);
                case IAPType.LifetimeVG:
                    return new LifetimeVG(obj.title, obj.description, obj.id, purchaseType);
                case IAPType.EquippableVG:
                    EquippableVG.EquippingModel equipModel = EquippableVG.EquippingModel.LOCAL;
                    if (obj.specific == EquippableVG.EquippingModel.CATEGORY.ToString()) equipModel = EquippableVG.EquippingModel.CATEGORY;
                    else if (obj.specific == EquippableVG.EquippingModel.GLOBAL.ToString()) equipModel = EquippableVG.EquippingModel.GLOBAL;
                    return new EquippableVG(equipModel, obj.title, obj.description, obj.id, purchaseType);
                case IAPType.UpgradeVG:
                    string[] props = obj.specific.Split(';');
                    return new UpgradeVG(props[0], string.IsNullOrEmpty(props[2]) ? null : props[2], string.IsNullOrEmpty(props[1]) ? null : props[1],
                                         obj.title, obj.description, obj.id, purchaseType);
                default: return null;
            }
        }
Пример #36
0
 /// <summary>
 /// this method checks user's funds for a virtual purchase
 /// and if they met the requirements, substracts funds
 /// </summary>
 public static bool VerifyVirtualPurchase(IAPObject obj)
 {
     //get a list of all currencies to check against
     Dictionary<string, int> curs = GetAllCurrencies();
     //loop over currencies and check each amount
     //if the player does not have enough funds, return false
     for (int i = 0; i < obj.virtualPrice.Count; i++)
     {
         IAPCurrency cur = obj.virtualPrice[i];
         if (curs.ContainsKey(cur.name) && cur.amount > curs[cur.name])
             return false;
     }
     //the player has enough funds,
     //get location string
     string loc = instance.currency;
     //loop over each currency
     //and substract price for this item
     for (int i = 0; i < obj.virtualPrice.Count; i++)
     {
         IAPCurrency cur = obj.virtualPrice[i];
         if (curs.ContainsKey(cur.name))
             instance.gameData[loc][cur.name].AsInt -= cur.amount;
     }
     //verification succeeded
     return true;
 }
Пример #37
0
        /// <summary>
        /// initialize virtual or real item properties
        /// based on IAPObject info set in IAP Settings editor.
        /// Called by ShopManager
        /// </summary>
        public void Init(IAPObject prod)
        {
            //cache
            type = prod.type;
            string name = prod.title;
            string descr = prod.description.Replace("\\n", "\n");
            string lockText = prod.req.labelText;

            //store the item id for later purposes
            productId = prod.id;
            //set icon to the matching sprite
            if (icon) icon.sprite = prod.icon;

            //when 'uppercase' has been checked,
            //convert title and description text to uppercase,
            //otherwise just keep and set them as they are
            if (uppercase)
            {
                name = name.ToUpper();
                descr = descr.ToUpper();
                lockText = lockText.ToUpper();
            }

            if (title) title.text = name;
            if (description) description.text = descr;

            if (type == IAPType.consumable || type == IAPType.nonConsumable
                || type == IAPType.subscription)
            {
                //purchases for real money have only one price value,
                //so we just use the first entry of the price label array
                if (price.Length > 0) price[0].text = prod.realPrice;
            }
            else if (prod.virtualPrice.Count > 0)
            {
                //purchases for virtual currency could have more than one price value,
                //so we loop over all entries and set the corresponding price label
                for (int i = 0; i < price.Length; i++)
                    if (price[i]) price[i].text = prod.virtualPrice[i].amount.ToString();
            }

            //set locked label text in case a requirement has been set
            if (lockedLabel && !string.IsNullOrEmpty(prod.req.entry)
                && !string.IsNullOrEmpty(prod.req.labelText))
                lockedLabel.text = lockText;
        }
Пример #38
0
        /// <summary>
        /// initialize virtual or real item properties
        /// based on IAPObject info set in IAP Settings editor.
        /// Called by ShopManager
        /// </summary>
        public void Init(IAPObject prod)
        {
            //cache
            type = prod.type;
            string name = prod.title;
            string descr = prod.description.Replace("\\n", "\n");
            string lockText = prod.req.labelText;

            //store the item id for later purposes
            productId = prod.id;
            //set icon to the matching sprite
            if (icon) icon.sprite = prod.icon;

            //when 'uppercase' has been checked,
            //convert title and description text to uppercase,
            //otherwise just keep and set them as they are
            if (uppercase)
            {
                name = name.ToUpper();
                descr = descr.ToUpper();
                lockText = lockText.ToUpper();
            }

            //set descriptive labels
            if (title) title.text = name;
            if (description) description.text = descr;
            
            //set price value
            if (IAPManager.IsMarketItem(productId))
                price.text = prod.realPrice.ToString();
            else if (!string.IsNullOrEmpty(prod.virtualPrice.name))
                price.text = prod.virtualPrice.amount.ToString();

            //set locked label text in case a requirement has been set
            if (lockedLabel && !string.IsNullOrEmpty(prod.req.entry)
                && !string.IsNullOrEmpty(prod.req.labelText))
                lockedLabel.text = lockText;
        }
Пример #39
0
        //draws the in game content editor
        void DrawIGC(List<IAPGroup> list)
        {
            EditorGUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.yellow;

            //draw currencies up to a maximum of 7
            //(there is no limitation, but 7 currencies do fit in the window nicely,
            //and there really shouldnt be a reason to have 7+ different currencies)
            if (script.currency.Count < 7)
            {
                //button for adding a new currency
                if (GUILayout.Button("Add Currency"))
                {
                    //switch current currency selection to the first entry
                    currencyIndex = 0;
                    //create new currency, then loop over items
                    //and add a new currency slot for each of them
                    script.currency.Add(new IAPCurrency());
                    for (int i = 0; i < list.Count; i++)
                        for (int j = 0; j < list[i].items.Count; j++)
                            list[i].items[j].virtualPrice.Add(new IAPCurrency());
                    return;
                }
            }
            else
            {
                //for more than 7 currencies,
                //we show a transparent button with no functionality
                GUI.backgroundColor = new Color(1, 0.9f, 0, 0.4f);
                if (GUILayout.Button("Add Currency"))
                { }
            }

            //draw yellow button for adding a new IAP group
            if (GUILayout.Button("Add new Group"))
            {
                //create new group, give it a generic name based on
                //the current system time and add it to the list of groups
                IAPGroup newGroup = new IAPGroup();
                string timestamp = GenerateUnixTime();
                newGroup.name = "Grp " + timestamp;
                newGroup.id = timestamp;
                list.Add(newGroup);
                return;
            }

            GUI.backgroundColor = Color.white;
            EditorGUILayout.EndHorizontal();

            //begin a scrolling view inside tab, pass in current Vector2 scroll position
            scrollPosIGC = EditorGUILayout.BeginScrollView(scrollPosIGC, GUILayout.Height(350));
            GUILayout.Space(20);
            EditorGUILayout.BeginHorizontal();
            //only draw a box behind currencies if there are any
            if (script.currency.Count > 0)
                GUI.Box(new Rect(3, 15, 796, 95), "");

            //loop through currencies
            for (int i = 0; i < script.currency.Count; i++)
            {
                EditorGUILayout.BeginVertical();
                //draw currency properties,
                //such as name and amount
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Name", GUILayout.Width(44));
                script.currency[i].name = EditorGUILayout.TextField(script.currency[i].name, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Default", GUILayout.Width(44));
                script.currency[i].amount = EditorGUILayout.IntField(script.currency[i].amount, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();

                //button for deleting a currency
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(52);
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(54)))
                {
                    //ask again before deleting the currency,
                    //as deleting it could cause angry customers!
                    //it's probably better not to remove currencies in production versions
                    if (EditorUtility.DisplayDialog("Delete Currency?",
                        "Existing users might lose their funds associated with this currency when updating.",
                        "Continue", "Abort"))
                    {
                        //loop over items and remove the
                        //associated currency slot for each of them
                        for (int j = 0; j < list.Count; j++)
                            for (int k = 0; k < list[j].items.Count; k++)
                                list[j].items[k].virtualPrice.RemoveAt(i);
                        //then remove the currency
                        script.currency.RemoveAt(i);
                        //reposition current currency index
                        if (script.currency.Count > 0)
                            currencyIndex = 0;
                        else
                            currencyIndex = -1;
                        break;
                    }
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();
            }
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            //draw currency selector, if there are any
            if (script.currency.Count > 0)
            {
                GUILayout.Space(10);
                EditorGUILayout.BeginHorizontal();
                //get all currency names,
                //then draw a popup list for selecting the desired index
                currencyNames = GetCurrencyNames();
                EditorGUILayout.LabelField("Selected Currency:", GUILayout.Width(120));
                currencyIndex = EditorGUILayout.Popup(currencyIndex, currencyNames, GUILayout.Width(140));
                EditorGUILayout.EndHorizontal();
                GUILayout.Space(20);
            }

            //loop over IAP groups
            for (int i = 0; i < list.Count; i++)
            {
                //cache group
                IAPGroup group = list[i];
                //version 1.2 backwards compatibility fix (empty IAPGroup ids)
                if (string.IsNullOrEmpty(group.id))
                    group.id = GenerateUnixTime();

                Container shopGroup = null;
                if (shop)
                {
                    shopGroup = shop.GetContainer(group.id);
                    if (shopGroup == null)
                    {
                        shopGroup = new Container();
                        shopGroup.id = group.id;
                        shop.containers.Add(shopGroup);
                    }
                }

                EditorGUILayout.BeginHorizontal();
                GUI.backgroundColor = Color.yellow;
                //button for adding a new IAPObject (product) to this group
                if (GUILayout.Button("New Product", GUILayout.Width(120)))
                {
                    IAPObject newObj = new IAPObject();
                    for (int j = 0; j < script.currency.Count; j++)
                        newObj.virtualPrice.Add(new IAPCurrency());
                    group.items.Add(newObj);
                    break;
                }
                GUI.backgroundColor = Color.white;

                //draw group properties
                EditorGUILayout.LabelField("Group:", GUILayout.Width(45));
                group.name = EditorGUILayout.TextField(group.name, GUILayout.Width(90));
                GUILayout.Space(10);
                EditorGUILayout.LabelField("Sort:", GUILayout.Width(35));
                orderType = (OrderType)EditorGUILayout.EnumPopup(orderType, GUILayout.Width(60));
                GUILayout.Space(10);

                if (!shop)
                    EditorGUILayout.LabelField("No ShopManager prefab found in this scene!", GUILayout.Width(300));
                else
                {
                    EditorGUILayout.LabelField("Prefab:", GUILayout.Width(45));
                    shopGroup.prefab = (GameObject)EditorGUILayout.ObjectField(shopGroup.prefab, typeof(GameObject), false, GUILayout.Width(100));
                    GUILayout.Space(10);
                    EditorGUILayout.LabelField("Parent:", GUILayout.Width(45));
                    shopGroup.parent = (Transform)EditorGUILayout.ObjectField(shopGroup.parent, typeof(Transform), true, GUILayout.Width(100));
                }

                GUILayout.FlexibleSpace();
                if (orderType != OrderType.none)
                {
                    group.items = orderProducts(group.items);
                    break;
                }

                //same as in DrawIAP(),
                //move group up & down buttons
                int groupUpWidth = 22;
                int groupDownWidth = 22;
                if (i == 0) groupDownWidth = 48;
                if (i == list.Count - 1) groupUpWidth = 48;

                if (i > 0 && GUILayout.Button("▲", GUILayout.Width(groupUpWidth)))
                {
                    list[i] = list[i - 1];
                    list[i - 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }
                if (i < list.Count - 1 && GUILayout.Button("▼", GUILayout.Width(groupDownWidth)))
                {
                    list[i] = list[i + 1];
                    list[i + 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }

                //button for removing a group including items
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(20)))
                {
                    if(shop) shop.containers.Remove(shopGroup);
                    list.RemoveAt(i);
                    break;
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));

                //draw header information for each item property
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("ID:", GUILayout.Width(110));
                GUILayout.Space(20);
                EditorGUILayout.LabelField("Icon:", GUILayout.Width(75));
                GUILayout.Space(20);
                EditorGUILayout.LabelField("Type:", GUILayout.Width(100));
                GUILayout.Space(20);
                int spaceTitleToDescription = 90;
                if (group.items.Count == 1) spaceTitleToDescription = 110;
                EditorGUILayout.LabelField("Title:", GUILayout.Width(spaceTitleToDescription));
                GUILayout.Space(20);
                int spaceDescriptionToPrice = 135;
                if (group.items.Count == 1) spaceDescriptionToPrice = 145;
                EditorGUILayout.LabelField("Description:", GUILayout.Width(spaceDescriptionToPrice));
                GUILayout.Space(20);
                EditorGUILayout.LabelField("Price:", GUILayout.Width(40));
                GUILayout.Space(80);
                EditorGUILayout.EndHorizontal();

                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item reference
                    IAPObject obj = group.items[j];
                    EditorGUILayout.BeginHorizontal();

                    //draw IAPObject (item/product) properties
                    obj.id = EditorGUILayout.TextField(obj.id, GUILayout.Width(120));
                    obj.icon = EditorGUILayout.ObjectField(obj.icon, typeof(Sprite), false, GUILayout.Width(65)) as Sprite;
                    obj.type = (IAPType)EditorGUILayout.EnumPopup(obj.type, GUILayout.Width(110));
                    //don't allow consumable/non consumable/subscription IAP types, they must be virtual
                    if (obj.type == IAPType.consumable) obj.type = IAPType.consumableVirtual;
                    else if (obj.type == IAPType.nonConsumable || obj.type == IAPType.subscription)
                        obj.type = IAPType.nonConsumableVirtual;
                    //other item properties
                    obj.title = EditorGUILayout.TextField(obj.title);
                    obj.description = EditorGUILayout.TextField(obj.description);

                    //if a currency has been selected previously,
                    //draw an input field for the selected currency
                    if (currencyIndex > -1)
                    {
                        //version 1.1 compability fix (virtual currency int -> IAPCurrency conversion)
                        if (obj.virtualPrice == null || currencyIndex > obj.virtualPrice.Count - 1)
                        {
                            GUI.backgroundColor = Color.gray;
                            if (GUILayout.Button("X"))
                            {
                                group.items.RemoveAt(j);
                                break;
                            }
                            GUI.backgroundColor = Color.white;
                            EditorGUILayout.EndHorizontal();
                            continue;
                        }

                        EditorGUILayout.BeginHorizontal();
                        IAPCurrency cur = obj.virtualPrice[currencyIndex];
                        cur.name = currencyNames[currencyIndex];
                        EditorGUILayout.LabelField(cur.name, GUILayout.Width(40));
                        cur.amount = EditorGUILayout.IntField(cur.amount, GUILayout.Width(60));
                        EditorGUILayout.EndHorizontal();
                    }
                    else
                        GUILayout.FlexibleSpace();

                    //same as in DrawIAP(), requirement button
                    if (!string.IsNullOrEmpty(obj.req.entry) || !string.IsNullOrEmpty(obj.req.nextId))
                        GUI.backgroundColor = Color.yellow;
                    if (GUILayout.Button("R", GUILayout.Width(20)))
                    {
                        reqEditor = (RequirementEditor)EditorWindow.GetWindowWithRect(typeof(RequirementEditor), new Rect(0, 0, 300, 170), false, "Requirement");
                        reqEditor.obj = obj;
                    }

                    GUI.backgroundColor = Color.white;
                    //same as in DrawIAP(),
                    //move item up & down buttons
                    int buttonUpWidth = 22;
                    int buttonDownWidth = 22;
                    if (j == 0) buttonDownWidth = 48;
                    if (j == group.items.Count - 1) buttonUpWidth = 48;

                    if (j > 0 && GUILayout.Button("▲", GUILayout.Width(buttonUpWidth)))
                    {
                        group.items[j] = group.items[j - 1];
                        group.items[j - 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }
                    if (j < group.items.Count - 1 && GUILayout.Button("▼", GUILayout.Width(buttonDownWidth)))
                    {
                        group.items[j] = group.items[j + 1];
                        group.items[j + 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }

                    //button for removing an item of the group
                    GUI.backgroundColor = Color.gray;
                    if (GUILayout.Button("X"))
                    {
                        group.items.RemoveAt(j);
                        break;
                    }
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.EndHorizontal();
                }
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                GUILayout.Space(30);
            }

            //ends the scrollview defined above
            EditorGUILayout.EndScrollView();
        }
Пример #40
0
        //draws the in app purchase editor
        //for a specific OS
        void DrawIAP(List<IAPGroup> list)
        {
            //begin a scrolling view inside this tab, pass in current Vector2 scroll position
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            GUILayout.Space(10);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Virtual Currencies:", EditorStyles.boldLabel, GUILayout.Width(125), GUILayout.Height(20));

            //button for adding a new currency
            GUI.backgroundColor = Color.yellow;
            if (GUILayout.Button("Add Currency"))
            {
                //switch current currency selection to the first entry
                currencyIndex = 0;
                //create new currency, then loop over items
                //and add a new currency slot for each of them
                script.currency.Add(new IAPCurrency());
                for (int i = 0; i < list.Count; i++)
                    for (int j = 0; j < list[i].items.Count; j++)
                        list[i].items[j].virtualPrice.Add(new IAPCurrency());
                return;
            }
            GUI.backgroundColor = Color.white;
            EditorGUILayout.EndHorizontal();

            //begin a scrolling view inside this tab, pass in current Vector2 scroll position
            scrollPosCurrency = EditorGUILayout.BeginScrollView(scrollPosCurrency, GUILayout.Height(105));

            EditorGUILayout.BeginHorizontal();
            //only draw a box behind currencies if there are any
            if (script.currency.Count > 0)
                GUI.Box(new Rect(0, 0, iapEditor.maxSize.x, 90), "");

            //loop through currencies
            for (int i = 0; i < script.currency.Count; i++)
            {
                EditorGUILayout.BeginVertical();
                //draw currency properties,
                //such as name and amount
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Name", GUILayout.Width(44));
                script.currency[i].name = EditorGUILayout.TextField(script.currency[i].name, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Default", GUILayout.Width(44));
                script.currency[i].amount = EditorGUILayout.IntField(script.currency[i].amount, GUILayout.Width(54));
                EditorGUILayout.EndHorizontal();

                //button for deleting a currency
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(52);
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(54)))
                {
                    //ask again before deleting the currency,
                    //as deleting it could cause angry customers!
                    //it's probably better not to remove currencies in production versions
                    if (EditorUtility.DisplayDialog("Delete Currency?",
                        "Existing users might lose their funds associated with this currency when updating.",
                        "Continue", "Abort"))
                    {
                        //loop over items and remove the
                        //associated currency slot for each of them
                        for (int j = 0; j < list.Count; j++)
                            for (int k = 0; k < list[j].items.Count; k++)
                            {
                                if(list[j].items[k].virtualPrice != null
                                   && list[j].items[k].virtualPrice.Count > i)
                                    list[j].items[k].virtualPrice.RemoveAt (i);
                            }
                        //then remove the currency
                        script.currency.RemoveAt(i);
                        //reposition current currency index
                        if (script.currency.Count > 0)
                            currencyIndex = 0;
                        else
                            currencyIndex = -1;
                        break;
                    }
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();
            }

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            //draw currency selector, if there are any
            if (script.currency.Count > 0)
            {
                GUILayout.Space(10);
                EditorGUILayout.BeginHorizontal();
                //get all currency names,
                //then draw a popup list for selecting the desired index
                currencyNames = GetCurrencyNames();
                EditorGUILayout.LabelField("Selected Currency:", GUILayout.Width(120));
                currencyIndex = EditorGUILayout.Popup(currencyIndex, currencyNames, GUILayout.Width(140));
                EditorGUILayout.EndHorizontal();
            }

            //ends the scrollview defined above
            EditorGUILayout.EndScrollView();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("In App Purchases:", EditorStyles.boldLabel, GUILayout.Width(125), GUILayout.Height(20));

            //draw yellow button for adding a new IAP group
            GUI.backgroundColor = Color.yellow;
            if (GUILayout.Button("Add new Category"))
            {
                //create new group, give it a generic name based on
                //the current unix time and add it to the list of groups
                IAPGroup newGroup = new IAPGroup();
                string timestamp = GenerateUnixTime();
                newGroup.name = "Grp " + timestamp;
                newGroup.id = timestamp;
                list.Add(newGroup);
                return;
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            //loop over IAP groups for this OS
            for (int i = 0; i < list.Count; i++)
            {
                //cache group
                IAPGroup group = list[i];
                //populate shop container variables if ShopManager is present
                ShopContainer shopContainer = null;
                if (shop)
                {
                    shopContainer = shop.GetContainer(group.id);
                    if (shopContainer == null)
                    {
                        shopContainer = new ShopContainer();
                        shopContainer.id = group.id;
                        shop.containers.Add(shopContainer);
                    }
                }

                GUI.backgroundColor = Color.yellow;
                EditorGUILayout.BeginHorizontal();
                int productSelection = 0;
                productSelection = EditorGUILayout.Popup(productSelection, productTypes);

                //button for adding a new IAPObject (product) to this group
                if (productSelection > 0)
                {
                    IAPObject newObj = new IAPObject();

                    switch (productSelection)
                    {
                        case 1:
                            Debug.LogError("Can't create products for real money. You need to import a billing plugin first!" +
                                           " Open 'Window > Simple IAP System > Plugin Setup' if you want to use one.");
                            return;
                        case 2:
                            newObj.isVirtual = true;
                            for(int j = 0; j < script.currency.Count; j++)
                                newObj.virtualPrice.Add(new IAPCurrency());
                            break;
                    }

                    group.items.Add(newObj);
                    break;
                }

                //draw group properties
                GUI.backgroundColor = Color.white;
                EditorGUILayout.LabelField("Group:", GUILayout.Width(45));
                group.name = EditorGUILayout.TextField(group.name, GUILayout.Width(90));
                GUILayout.Space(10);
                EditorGUILayout.LabelField("Sort:", GUILayout.Width(35));
                orderType = (OrderType)EditorGUILayout.EnumPopup(orderType, GUILayout.Width(60));
                GUILayout.Space(10);

                if (!shop)
                {
                    GUI.contentColor = Color.yellow;
                    EditorGUILayout.LabelField("No ShopManager prefab found in this scene!", GUILayout.Width(300));
                    GUI.contentColor = Color.white;
                }
                else
                {
                    EditorGUILayout.LabelField("Prefab:", GUILayout.Width(45));
                    shopContainer.prefab = (GameObject)EditorGUILayout.ObjectField(shopContainer.prefab, typeof(GameObject), false, GUILayout.Width(100));
                    GUILayout.Space(10);
                    EditorGUILayout.LabelField("Container:", GUILayout.Width(65));
                    shopContainer.parent = (IAPContainer)EditorGUILayout.ObjectField(shopContainer.parent, typeof(IAPContainer), true, GUILayout.Width(100));
                }

                GUILayout.FlexibleSpace();
                //check for order type and, if it
                //isn't equal to 'none', start ordering
                if (orderType != OrderType.none)
                {
                    group.items = orderProducts(group.items);
                    break;
                }

                //button width for up & down buttons
                //these should always be at the same width, so if there's
                //only one button (e.g. if there's only one group),
                //the width must be extended
                int groupUpWidth = 22;
                int groupDownWidth = 22;
                if (i == 0) groupDownWidth = 48;
                if (i == list.Count - 1) groupUpWidth = 48;

                //draw up & down buttons for re-ordering groups
                //this will simply switch references in the list
                //hotControl and keyboardControl unsets current mouse focus
                if (i > 0 && GUILayout.Button("▲", GUILayout.Width(groupUpWidth)))
                {
                    list[i] = list[i - 1];
                    list[i - 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }
                if (i < list.Count - 1 && GUILayout.Button("▼", GUILayout.Width(groupDownWidth)))
                {
                    list[i] = list[i + 1];
                    list[i + 1] = group;
                    EditorGUIUtility.hotControl = 0;
                    EditorGUIUtility.keyboardControl = 0;
                }

                //button for removing a group including items
                GUI.backgroundColor = Color.gray;
                if (GUILayout.Button("X", GUILayout.Width(20)))
                {
                    if(shop) shop.containers.Remove(shopContainer);
                    list.RemoveAt(i);
                    break;
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                GUILayout.Space(20);

                List<Rect> headerRect = GetHeaders();

                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item reference
                    IAPObject obj = group.items[j];
                    EditorGUILayout.BeginHorizontal();

                    //new platforms future compatibility
                    int platforms = System.Enum.GetValues(typeof(IAPPlatform)).Length;
                    for (int k = obj.localIDs.Count; k < platforms; k++)
                        obj.localIDs.Add(new IAPIdentifier());

                    GUILayout.Box("", GUILayout.Height(15), GUILayout.Width(15));
                    Rect foldoutRect = GUILayoutUtility.GetLastRect();
                    if(!obj.isVirtual)
                        obj.platformFoldout = EditorGUI.Foldout(foldoutRect, obj.platformFoldout, "");

                    //draw IAPObject (item/product) properties
                    obj.id = EditorGUILayout.TextField(obj.id, GUILayout.MaxWidth(150));
                    obj.icon = EditorGUILayout.ObjectField(obj.icon, typeof(Sprite), false, GUILayout.MaxWidth(65)) as Sprite;

                    IAPType oldType = obj.type;
                    obj.type = (IAPType)EditorGUILayout.EnumPopup(obj.type, GUILayout.MaxWidth(110));
                    if (obj.type != oldType && !obj.type.ToString().Contains("Virtual"))
                    {
                        Debug.LogError("Can't create products for real money. You need to import a billing plugin first!" +
                                        "Open 'Window > Simple IAP System > Plugin Setup' if you want to use one.");
                        obj.type = IAPType.consumableVirtual;
                    }

                    obj.title = EditorGUILayout.TextField(obj.title, GUILayout.MaxWidth(180));
                    obj.description = EditorGUILayout.TextField(obj.description);

                    if (obj.isVirtual)
                    {
                        IAPCurrency cur = null;
                        if(currencyIndex >= 0 && obj.virtualPrice.Count > currencyIndex)
                            cur = obj.virtualPrice[currencyIndex];

                        if (cur == null)
                        {
                            EditorGUILayout.LabelField("No currency!", GUILayout.MinWidth(70), GUILayout.MaxWidth(100));
                        }
                        else
                        {
                            cur.name = currencyNames[currencyIndex];
                            EditorGUILayout.LabelField(cur.name, GUILayout.MinWidth(30), GUILayout.MaxWidth(40));
                            cur.amount = EditorGUILayout.IntField (cur.amount, GUILayout.MinWidth(35), GUILayout.Width(50));
                        }
                    }
                    else
                    {
                        obj.realPrice = EditorGUILayout.TextField (obj.realPrice, GUILayout.MaxWidth(80));
                        obj.fetch = EditorGUILayout.Toggle (obj.fetch, GUILayout.MaxWidth(20));
                    }

                    //button for adding a requirement to this item
                    if (!string.IsNullOrEmpty(obj.req.entry) || !string.IsNullOrEmpty(obj.req.nextId))
                        GUI.backgroundColor = Color.yellow;
                    if (GUILayout.Button("R", GUILayout.Width(20)))
                    {
                        reqEditor = (RequirementEditor)EditorWindow.GetWindowWithRect(typeof(RequirementEditor), new Rect(0, 0, 300, 170), false, "Requirement");
                        reqEditor.obj = obj;
                    }

                    GUI.backgroundColor = Color.white;
                    //do the same here as with the group up & down buttons
                    //(see above)
                    int buttonUpWidth = 22;
                    int buttonDownWidth = 22;
                    if (j == 0) buttonDownWidth = 48;
                    if (j == group.items.Count - 1) buttonUpWidth = 48;

                    //draw up & down buttons for re-ordering items in a group
                    //this will simply switch references in the list
                    if (j > 0 && GUILayout.Button("▲", GUILayout.Width(buttonUpWidth)))
                    {
                        group.items[j] = group.items[j - 1];
                        group.items[j - 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }
                    if (j < group.items.Count - 1 && GUILayout.Button("▼", GUILayout.Width(buttonDownWidth)))
                    {
                        group.items[j] = group.items[j + 1];
                        group.items[j + 1] = obj;
                        EditorGUIUtility.hotControl = 0;
                        EditorGUIUtility.keyboardControl = 0;
                    }

                    if(group.items.Count == 1)
                        GUILayout.Space(52);

                    //button for removing an item of the group
                    GUI.backgroundColor = Color.gray;
                    if (GUILayout.Button("X", GUILayout.Width(20)))
                    {
                        group.items.RemoveAt(j);
                        break;
                    }
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.EndHorizontal();

                    //draw platform override foldout
                    if (obj.platformFoldout)
                    {
                        EditorGUILayout.LabelField("Platform Overrides");
                        for (int k = 0; k < obj.localIDs.Count; k++)
                        {
                            EditorGUILayout.BeginHorizontal();
                            GUILayout.Space(40);
                            obj.localIDs[k].overridden = EditorGUILayout.BeginToggleGroup("", obj.localIDs[k].overridden);
                            EditorGUILayout.BeginHorizontal();
                            obj.localIDs[k].id = EditorGUILayout.TextField(obj.localIDs[k].id, GUILayout.Width(120));
                            EditorGUILayout.LabelField(((IAPPlatform)k).ToString());
                            EditorGUILayout.EndHorizontal();
                            EditorGUILayout.EndToggleGroup();
                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }

                for (int j = 0; j < headerRect.Count; j++)
                    EditorGUI.LabelField(new Rect(headerRect[j].x, headerRect[j].y - 20, 100, 20), headerNames[j]);

                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                GUILayout.Space(30);
            }

            //ends the scrollview defined above
            EditorGUILayout.EndScrollView();
        }
Пример #41
0
		/// <summary>
        /// Overload for purchasing virtual product based on its product identifier.
        /// </summary>
        public static void PurchaseProduct(IAPObject obj)
        {	
			string productId = obj.id;
			//product is set to already owned, do nothing
			if(DBManager.isPurchased(productId))
			{
				PurchaseFailed("Product already purchased.");
                return;
			}
		
            //check whether the player has enough funds
            bool didSucceed = DBManager.VerifyVirtualPurchase(obj);
            if (isDebug) Debug.Log("Purchasing virtual product " + productId + ", result: " + didSucceed);
            //on success, non-consumables are saved to the database. This automatically
			//saves the new substracted fund value, then and fire the succeeded event
            if (didSucceed)
            {
                if(obj.type != IAPType.consumableVirtual)
					DBManager.SetToPurchased(productId);
                purchaseSucceededEvent(productId);
            }
            else
                PurchaseFailed("Insufficient funds.");
        }