Example #1
0
        /// <summary>
        /// Initializes all IAPItem in the scene and instantiates them with their correct state.
        /// Called by IAPManager.
        /// </summary>
        public void Init()
        {
            instance = this;
            IAPItems.Clear();
            DBManager.updatedDataEvent += Refresh;

            //get manually placed items in the scene
            IAPItem[] sceneItems = Resources.FindObjectsOfTypeAll(typeof(IAPItem)) as IAPItem[];
            for (int i = 0; i < sceneItems.Length; i++)
            {
                if (string.IsNullOrEmpty(sceneItems[i].productId))
                {
                    continue;
                }

                #if UNITY_EDITOR
                if (UnityEditor.EditorUtility.IsPersistent(sceneItems[i].gameObject))
                {
                    continue;
                }
                #endif

                IAPItems.Add(sceneItems[i].productId, sceneItems[i]);
            }


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

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

                //skip group if prefab or parent wasn't set
                if (container == null || container.prefab == null || container.parent == null)
                {
                    continue;
                }

                //loop over items
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item
                    IAPObject obj = group.items[j];
                    //the item has already been placed in the scene manually
                    //dont instantiate it in a container then
                    if (IAPItems.ContainsKey(obj.id))
                    {
                        continue;
                    }

                    //instantiate shop item in the scene and attach it to the defined parent transform
                    GameObject newItem = (GameObject)Instantiate(container.prefab);
                    newItem.transform.SetParent(container.parent.transform, false);
                    newItem.GetComponent <RectTransform>().anchoredPosition = Vector2.zero;
                    //rename item to force ordering as set in the IAP Settings editor
                    newItem.name = "IAPItem " + string.Format("{0:000}", index + j);
                    //get IAPItem component of the instantiated item
                    IAPItem item = newItem.GetComponent <IAPItem>();
                    if (item == null)
                    {
                        continue;
                    }

                    //add IAPItem to dictionary for later lookup
                    IAPItems.Add(obj.id, item);

                    //upgrades overwrite, an IAP Item gets replaced with its current level
                    List <string> upgrades = IAPManager.GetIAPUpgrades(obj.id);
                    if (upgrades != null && upgrades.Count > 0)
                    {
                        for (int k = 0; k < upgrades.Count; k++)
                        {
                            IAPItems.Add(upgrades[k], item);
                        }

                        string currentUpgrade = IAPManager.GetNextUpgrade(obj.id);

                        if (!string.IsNullOrEmpty(currentUpgrade))
                        {
                            obj = IAPManager.GetIAPObject(currentUpgrade);
                        }
                    }

                    //initialize and set up item properties based on the associated IAPObject
                    //they could get overwritten by online data later
                    item.Init(obj);
                }

                index += group.items.Count;
            }

            //refresh all products initially
            RefreshAll();
        }
        //instantiates shop item prefabs
        void InitShop()
        {
            //reset
            IAPItems.Clear();

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

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

                //skip group if prefab or parent wasn't set
                if (container == null || container.prefab == null || container.parent == null)
                {
                    if (IAPManager.isDebug)
                    {
                        Debug.LogWarning("Setting up Shop, but prefab or parent of Group: '"
                                         + group.name + "' isn't set. Skipping group.");
                    }
                    continue;
                }

                //loop over items
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item
                    IAPObject obj = group.items[j];
                    //instantiate shop item in the scene and attach it to the defined parent transform
                    GameObject newItem = (GameObject)Instantiate(container.prefab);
                    newItem.transform.SetParent(container.parent.transform, false);
                    newItem.GetComponent <RectTransform>().anchoredPosition = Vector2.zero;
                    //rename item to force ordering as set in the IAP Settings editor
                    newItem.name = "IAPItem " + string.Format("{0:000}", index + j);
                    //get IAPItem component of the instantiated item
                    IAPItem item = newItem.GetComponent <IAPItem>();
                    if (item == null)
                    {
                        continue;
                    }

                    //add IAPItem to dictionary for later lookup
                    IAPItems.Add(obj.id, item);

                    //upgrades overwrite, an IAP Item gets replaced with its current level
                    List <string> upgrades = IAPManager.GetIAPUpgrades(obj.id);
                    if (upgrades != null && upgrades.Count > 0)
                    {
                        for (int k = 0; k < upgrades.Count; k++)
                        {
                            IAPItems.Add(upgrades[k], item);
                        }

                        string currentUpgrade = IAPManager.GetNextUpgrade(obj.id);

                        if (!string.IsNullOrEmpty(currentUpgrade))
                        {
                            obj = IAPManager.GetIAPObject(currentUpgrade);
                        }
                    }

                    //initialize and set up item properties based on the associated IAPObject
                    //they could get overwritten by online data later
                    item.Init(obj);
                }

                index += group.items.Count;
            }
        }