/// <summary> /// Deletes all data saved in prefs, for ensuring a clean test state. /// <summary> public void Reset() { if (DBManager.GetInstance()) { DBManager.ClearAll(); DBManager.GetInstance().Init(); } }
/// <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(); } } } }
/// <summary> /// Increases player level by 1 which unlocks new shop items. /// <summary> public void LevelUp() { if (DBManager.GetInstance()) { int level = DBManager.IncreasePlayerData("level", 1); if (ShopManager.GetInstance()) { ShopManager.RefreshAll(); } Debug.Log("Leveled up to level: " + level + "! Shop Manager tried to unlock new items."); } }
public static void Clear() { if (EditorUtility.DisplayDialog("Clear Local Database Entries", "Are you sure you want to clear the PlayerPref data for this project? (This includes Simple IAP System data, but also all other PlayerPrefs)", "Clear", "Cancel")) { string unityPurchasingPath = System.IO.Path.Combine(System.IO.Path.Combine(Application.persistentDataPath, "Unity"), "UnityPurchasing"); if (System.IO.Directory.Exists(unityPurchasingPath)) { System.IO.Directory.Delete(unityPurchasingPath, true); } DBManager.ClearAll(); if (DBManager.GetInstance() != null) { DBManager.GetInstance().Init(); } } }
void OnEnable() { //subscribe to successful purchase/update event, //it could be that the player obtained currency IAPManager.purchaseSucceededEvent += UpdateValue; DBManager.updatedDataEvent += UpdateValue; //update currency display right away if (!DBManager.GetInstance()) { return; } //get current currency value int funds = DBManager.GetFunds(currency); //display value in the UILabel label.text = funds.ToString(); //store value curValue = funds; }
/// <summary> /// Sets up shop items based on previous purchases, meaning we /// set them to 'purchased' thus not purchasable in the GUI. /// Also select the items that were selected by the player before. /// </summary> public static void SetItemState() { //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 array of purchased item ids, look them up in our //shop item dictionary and set them to purchased List <string> purchasedItems = DBManager.GetAllPurchased(); for (int i = 0; i < purchasedItems.Count; i++) { if (instance.IAPItems.ContainsKey(purchasedItems[i]) && IAPManager.GetIAPUpgrades(purchasedItems[i]).Count == 0) { instance.IAPItems[purchasedItems[i]].Purchased(true); } } //get dictionary of selected item ids, look them up in our //shop item dictionary and set the checkbox component to selected Dictionary <string, List <string> > selectedItems = DBManager.GetAllSelected(); foreach (string key in selectedItems.Keys) { for (int i = 0; i < selectedItems[key].Count; i++) { if (instance.IAPItems.ContainsKey(selectedItems[key][i])) { instance.IAPItems[selectedItems[key][i]].IsSelected(true); } } } }