/// <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(); } }
/// <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(); } } } }