Example #1
0
        // Use this for initialization
        void Start()
        {
            if (uid != null)
            {
                IAPObject obj = null;

                //
                if (targetType == IAPType.Inventory)
                {
                    obj = IAPInventoryManager.GetInventory(uid);
                }
                else if (targetType == IAPType.Currency)
                {
                    obj = IAPInventoryManager.GetCurrency(uid);
                }

                if (obj != null)
                {
                    UpdateTemplate(obj);

                    // Update text
                    Text[] txts = gameObject.GetComponentsInChildren <Text>();
                    // InAppPurchase
                    foreach (Text txt in txts)
                    {
                        if (txt.name == "amount_consume")
                        {
                            txt.text = amount.ToString();
                        }
                    }

                    IAPUIUtility.AddButtonCallback(
                        gameObject, (GameObject go) => {
                        if (useConfirmDialog)
                        {
                            // Construct confirm msg
                            string msg = IAPInventoryManager.uiSettings.consumeConfirmString.Replace("%title%", obj.title);
                            msg        = msg.Replace("%description%", obj.description);
                            msg        = msg.Replace("%amount_consume%", amount.ToString());
                            // Show confirm diaglog
                            IAPInventoryManager.ShowConfirmDialog(msg,
                                                                  delegate(IAPDialog diag){
                                if (obj.Consume(amount))
                                {
                                    UpdateTemplate(obj);
                                }
                            }
                                                                  );
                        }
                        else
                        {
                            obj.Consume(amount);
                        }
                    }
                        );
                }
            }
        }
        // Button callback

        private void handleButtonCallback(IAPObject obj)
        {
            if (useConfirmDialog)
            {
                // Construct confirm msg
                IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                if (currency != null)
                {
                    string msg = IAPInventoryManager.uiSettings.iapConfirmString.Replace("%title%", obj.title);
                    msg = msg.Replace("%description%", obj.description);
                    msg = msg.Replace("%price%", obj.price.ToString());
                    msg = msg.Replace("%currency_title%", currency.title);
                    msg = msg.Replace("%currency_description%", currency.description);
                    // Show confirm diaglog
                    IAPInventoryManager.ShowConfirmDialog(msg,
                                                          delegate(IAPDialog diag){
                        // Debug.Log("available: " + obj.available);
                        // Check if enough currency
                        if ((obj.available != 0) && currency != null && currency.Consume(obj.price))
                        {
                            obj.Refill(1);
                            if (obj is IAPInventory)
                            {
                                obj.Unlock();
                            }
                            UpdateTemplate(obj);
                        }
                    }
                                                          );
                }
            }
            else
            {
                // Get the currency
                IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                // Check if enough currency
                if (currency != null && currency.Consume(obj.price))
                {
                    obj.Refill(1);
                    if (obj is IAPInventory)
                    {
                        obj.Unlock();
                    }
                    UpdateTemplate(obj);
                }
            }
        }
        // Use this for initialization
        void Start()
        {
            if (uid != null)
            {
                // Purchase button action
                System.Action <GameObject> purchaseButtonCallback = null;

                //
                if (targetType == IAPType.Currency)
                {
                    IAPCurrency obj = IAPInventoryManager.GetCurrency(uid);

                    if (obj != null)
                    {
                        UpdateTemplate(obj);
                        IAPInventoryManager.OnCurrencyUpdated += handleCurrencyUpdated;
                        purchaseButtonCallback = (GameObject go) => {
                            handleButtonCallback(obj);
                        };
                    }
                }
                else if (targetType == IAPType.Inventory)
                {
                    IAPInventory obj = IAPInventoryManager.GetInventory(uid);

                    if (obj != null)
                    {
                        Debug.Log("UpdateTemplate " + obj.uid);
                        UpdateTemplate(obj);

                        IAPInventoryManager.OnInventoryUpdated += handleInventoryUpdated;
                        // Debug.Log("obj.available: " + obj.available);
                        // Check if inventory available
                        // if(obj.available==0){
                        //  IAPUIUtility.SetButtonActive(false,gameObject,"purchase_button");
                        // } else {
                        purchaseButtonCallback = (GameObject go) => {
                            // Debug.Log("213123");
                            handleButtonCallback(obj);
                        };
                        // }
                    }
                }
                else if (targetType == IAPType.Ability)
                {
                    IAPAbility obj = IAPInventoryManager.GetAbility(uid);

                    if (obj != null)
                    {
                        UpdateTemplate(obj);

                        IAPInventoryManager.OnAbilityUpdated += handleAbilityUpdated;
                        IAPAbility ability = (obj as IAPAbility);
                        if (ability.level < ability.levels.Count - 1)
                        {
                            // Get the currency
                            IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                            purchaseButtonCallback = (GameObject go) => {
                                if (useConfirmDialog)
                                {
                                    if (currency != null)
                                    {
                                        // Construct confirm msg
                                        IAPAbilityLevel lv  = ability.GetCurrentLevel();
                                        string          msg = IAPInventoryManager.uiSettings.abilityConfirmString.Replace("%title%", obj.title);
                                        msg = msg.Replace("%description%", lv.description.ToString());
                                        msg = msg.Replace("%price%", lv.price.ToString());
                                        msg = msg.Replace("%currency_title%", currency.title);
                                        msg = msg.Replace("%currency_description%", currency.description);
                                        // Show confirm diaglog
                                        IAPInventoryManager.ShowConfirmDialog(msg,
                                                                              delegate(IAPDialog diag){
                                            ability.Upgrade();
                                        }
                                                                              );
                                    }
                                }
                                else
                                {
                                    // Purchase the package
                                    ability.Upgrade();
                                }
                            };
                        }
                        else
                        {
                            // Disable the button
                            IAPUIUtility.SetButtonEnabled(gameObject, false);
                        }
                    }
                }
                else if (targetType == IAPType.InAppPurchase)
                {
                    IAPPackage obj = IAPInventoryManager.GetPackage(uid);

                    if (obj != null)
                    {
                        // UpdateTemplate(obj);

                        // if((obj.productType==IAPProductType.NonConsumable || obj.productType==IAPProductType.Subscription) && obj.amount>0)
                        // {
                        //  IAPUIUtility.SetButtonEnabled(gameObject,false,"purchase_button");

                        // } else {

                        if (obj.fetchFromStore)
                        {
                            // For Real Money IAP

                            IAPInventoryManager.OnIAPInitialized += handleOnIAPInitialized;

                            // Check if IAP initialized
                            if (IAPManager.IsInitialized())
                            {
                                Debug.LogFormat("uid: {0} type: {1}  amount: {2}", uid, obj.productType, obj.amount);

                                if ((obj.productType == IAPProductType.NonConsumable || obj.productType == IAPProductType.Subscription) && obj.amount > 0)
                                {
                                    IAPUIUtility.SetButtonEnabled(gameObject, false, "purchase_button");
                                }
                                else
                                {
                                    purchaseButtonCallback = (GameObject go) => {
                                        IAPUIUtility.SetButtonEnabled(gameObject, false, "purchase_button");
                                        obj.Purchase(handlePackageUpdated);
                                    };
                                }
                            }
                            else
                            {
                                IAPUIUtility.SetButtonEnabled(gameObject, false, "purchase_button");
                                IAPInventoryManager.InitIAPManager();
                            }
                        }
                        else
                        {
                            // For Virtual Currency IAP

                            purchaseButtonCallback = (GameObject go) => {
                                if (useConfirmDialog)
                                {
                                    // Construct confirm msg
                                    IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                                    if (currency != null)
                                    {
                                        string msg = IAPInventoryManager.uiSettings.iapConfirmString.Replace("%title%", obj.title);
                                        msg = msg.Replace("%description%", obj.description);
                                        msg = msg.Replace("%price%", obj.price.ToString());
                                        msg = msg.Replace("%currency_title%", currency.title);
                                        msg = msg.Replace("%currency_description%", currency.description);
                                        // Show confirm diaglog
                                        IAPInventoryManager.ShowConfirmDialog(msg,
                                                                              delegate(IAPDialog diag){
                                            // Purchase the package
                                            obj.Purchase();
                                        }
                                                                              );
                                    }
                                }
                                else
                                {
                                    // Purchase the package
                                    obj.Purchase();
                                }
                            };
                        }
                        // }

                        UpdateTemplate(obj);
                    }
                }
                else if (targetType == IAPType.GameLevel && level != -1)
                {
                    IAPGameLevel obj = IAPInventoryManager.GetGameLevel(uid);
                    IAPInventoryManager.OnGameLevelUpdated += handleGameLevelUpdated;

                    if (obj != null)
                    {
                        UpdateGameLevelTemplate(obj, level);

                        // IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
// Debug.LogFormat("obj {0}",level);

                        // purchaseButtonCallback=(GameObject go)=>{
                        IAPUIUtility.AddButtonCallback(gameObject, (GameObject go) => {
                            IAPGameSubLevel subLevel = obj.levels[level];
                            bool islocked            = (obj.GetPropertyValue("locked", level) > 0);
                            // Debug.LogFormat("obj {0} {1} {2}", level, useConfirmDialog, obj.currency);

                            // Check if price valid and
                            if (subLevel.price > 0 && islocked)
                            {
                                if (useConfirmDialog)
                                {
                                    // Construct confirm msg
                                    IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                                    if (currency != null)
                                    {
                                        string msg = IAPInventoryManager.uiSettings.iapConfirmString.Replace("%title%", obj.title);
                                        msg        = msg.Replace("%description%", obj.description + " Level " + level.ToString());
                                        msg        = msg.Replace("%price%", subLevel.price.ToString());
                                        msg        = msg.Replace("%currency_title%", currency.title);
                                        msg        = msg.Replace("%currency_description%", currency.description);
                                        // Show confirm diaglog
                                        IAPInventoryManager.ShowConfirmDialog(msg,
                                                                              delegate(IAPDialog diag){
                                            // Check if enough currency
                                            if (currency != null && currency.Consume(subLevel.price))
                                            {
                                                // obj.SetPropertyValue("locked",level,1);
                                                obj.UnlockLevel(level);
                                            }
                                        }
                                                                              );
                                    }
                                }
                                else
                                {
                                    // Get the currency
                                    IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency);
                                    // Check if enough currency
                                    if (currency != null && currency.Consume(subLevel.price))
                                    {
                                        obj.UnlockLevel(level);
                                    }
                                }
                            }
                        }, "self,select_button");
                    }
                }

                // Add the button callback to purchase_button
                if (purchaseButtonCallback != null)
                {
                    IAPUIUtility.AddButtonCallback(gameObject, purchaseButtonCallback, "purchase_button");
                }
            }
        }