/// <summary> /// This method checks user's funds for a virtual purchase. Returns true and /// substracts funds if they own enough virtual currency for the product price. /// </summary> public static bool VerifyVirtualPurchase(IAPObject obj) { //get a list of all currencies to check against Dictionary <string, int> curs = GetCurrencies(); //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, 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[currencyKey][cur.name].AsInt -= cur.amount; } } //verification succeeded Save(); return(true); }
//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; } } } }
public static List <IAPCurrency> FromJSON(string text) { List <IAPCurrency> currency = new List <IAPCurrency>(); JSONNode data = JSON.Parse(text); for (int i = 0; i < data.Count; i++) { IAPCurrency cur = new IAPCurrency(); cur.name = data[i]["DisplayName"].Value; cur.amount = data[i]["InitialDeposit"].AsInt; currency.Add(cur); } return(currency); }
public static string ToJSON(List <IAPCurrency> currency) { JSONNode data = new JSONClass(); JSONArray curArray = new JSONArray(); for (int i = 0; i < currency.Count; i++) { IAPCurrency cur = currency[i]; curArray[i]["CurrencyCode"] = new JSONData(cur.name.Substring(0, 2).ToUpper()); curArray[i]["DisplayName"] = new JSONData(cur.name); curArray[i]["InitialDeposit"] = new JSONData(cur.amount); } data = curArray; return(data.ToString()); }
//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(); }
//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(); }
public static List <IAPGroup> FromJSON(string text, List <IAPCurrency> currencies) { List <IAPGroup> IAPs = new List <IAPGroup>(); JSONNode data = JSON.Parse(text)["Catalog"]; for (int i = 0; i < data.Count; i++) { IAPGroup g = IAPs.SingleOrDefault(x => x.name == data[i]["SIS"]["Group"]["Name"].Value); if (g == null) { g = new IAPGroup(); g.id = data[i]["SIS"]["Group"]["Id"].Value; g.name = data[i]["SIS"]["Group"]["Name"].Value; g.items = new List <IAPObject>(); IAPs.Add(g); } IAPObject obj = new IAPObject(); obj.id = data[i]["ItemId"].Value; obj.title = data[i]["DisplayName"].Value; obj.description = data[i]["Description"].Value; obj.type = (ProductType)data[i]["SIS"]["Type"].AsInt; obj.realPrice = data[i]["SIS"]["Price"].Value; obj.fetch = data[i]["SIS"]["Fetch"].AsBool; obj.icon = (Sprite)AssetDatabase.LoadAssetAtPath(data[i]["SIS"]["Icon"].Value, typeof(Sprite)); if (!string.IsNullOrEmpty(data[i]["Bundle"].ToString())) { obj.editorType = IAPType.Currency; obj.virtualPrice = new List <IAPCurrency>(); foreach (string key in data[i]["Bundle"]["BundledVirtualCurrencies"].AsObject.Keys) { for (int j = 0; j < currencies.Count; j++) { if (currencies[j].name.StartsWith(key, System.StringComparison.OrdinalIgnoreCase)) { IAPCurrency cur = new IAPCurrency(); cur.name = currencies[j].name; cur.amount = data[i]["Bundle"]["BundledVirtualCurrencies"][key].AsInt; obj.virtualPrice.Add(cur); break; } } } } else if (string.IsNullOrEmpty(data[i]["VirtualCurrencyPrices"]["RM"].Value)) { obj.editorType = IAPType.Virtual; obj.virtualPrice = new List <IAPCurrency>(); foreach (string key in data[i]["VirtualCurrencyPrices"].AsObject.Keys) { for (int j = 0; j < currencies.Count; j++) { if (currencies[j].name.StartsWith(key, System.StringComparison.OrdinalIgnoreCase)) { IAPCurrency cur = new IAPCurrency(); cur.name = currencies[j].name; cur.amount = data[i]["VirtualCurrencyPrices"][key].AsInt; obj.virtualPrice.Add(cur); break; } } } } if (obj.editorType != IAPType.Virtual) { int platformCount = System.Enum.GetValues(typeof(IAPPlatform)).Length; for (int j = 0; j < platformCount; j++) { if (string.IsNullOrEmpty(data[i]["SIS"]["PlatformId"][j.ToString()].Value)) { continue; } obj.storeIDs.Add(new StoreID(((IAPPlatform)j).ToString(), data[i]["SIS"]["PlatformId"][j.ToString()].Value)); } } if (!string.IsNullOrEmpty(data[i]["SIS"]["Requirement"].ToString())) { obj.req.entry = data[i]["SIS"]["Requirement"]["Id"].Value; obj.req.target = data[i]["SIS"]["Requirement"]["Value"].AsInt; obj.req.labelText = data[i]["SIS"]["Requirement"]["Text"].Value; obj.req.nextId = data[i]["SIS"]["Requirement"]["Next"].Value; } g.items.Add(obj); } return(IAPs); }
/// <summary> /// 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. /// </summary> public static void ConvertToIAPs(string jsonText) { //skip empty strings if (string.IsNullOrEmpty(jsonText)) { return; } //parse string JSONNode data = new JSONClass(); data = JSON.Parse(jsonText); //iterate over product ids foreach (string key in data.AsObject.Keys) { //skip non-existing ids IAPObject obj = IAPManager.GetIAPObject(key); if (obj == null) { continue; } //overwrite IAP properties JSONNode node = data[key]; if (!string.IsNullOrEmpty(node["title"])) { obj.title = node["title"]; } if (!string.IsNullOrEmpty(node["description"])) { obj.description = node["description"]; } if (!string.IsNullOrEmpty(node["type"])) { switch (node["type"]) { case "consumable": obj.type = IAPType.consumableVirtual; break; default: obj.type = IAPType.nonConsumableVirtual; break; } } if (!string.IsNullOrEmpty(node["virtualPrice"].ToString())) { JSONNode virtualPrice = node["virtualPrice"]; List <IAPCurrency> curList = new List <IAPCurrency>(); for (int j = 0; j < virtualPrice.Count; j++) { IAPCurrency cur = new IAPCurrency(); cur.name = virtualPrice[j]["name"]; cur.amount = virtualPrice[j]["amount"].AsInt; curList.Add(cur); } obj.virtualPrice = curList; } if (!string.IsNullOrEmpty(node["requirement"].ToString())) { JSONNode requirement = node["requirement"]; obj.req.entry = requirement["entry"]; obj.req.labelText = requirement["labelText"]; obj.req.target = requirement["target"].AsInt; } } }
/// <summary> /// 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. /// </summary> public static void ConvertToIAPs(string jsonText) { //skip empty strings if (string.IsNullOrEmpty(jsonText)) return; //parse string JSONObject data = new JSONObject(jsonText); //iterate over product ids foreach(string key in data.keys) { //skip non-existing ids IAPObject obj = IAPManager.GetIAPObject(key); if (obj == null) continue; //overwrite IAP properties JSONObject node = data[key]; if (node["title"] != null) obj.title = node["title"].str; if (node["description"] != null) obj.description = node["description"].str; if (node["virtualPrice"] != null) { JSONObject virtualPrice = node["virtualPrice"]; IAPCurrency cur = new IAPCurrency(); cur.name = virtualPrice["name"].str; cur.amount = (int)virtualPrice["amount"].n; obj.virtualPrice = cur; } if (node["requirement"] != null) { JSONObject requirement = node["requirement"]; obj.req.entry = requirement["entry"].str; obj.req.labelText = requirement["labelText"].str; obj.req.target = (int)requirement["target"].n; } } }
/// <summary> /// 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. /// </summary> public static void ConvertToIAPs(string jsonText) { //skip empty strings if (string.IsNullOrEmpty(jsonText)) return; //parse string JSONNode data = new JSONClass(); data = JSON.Parse(jsonText); //iterate over product ids foreach (string key in data.AsObject.Keys) { //skip non-existing ids IAPObject obj = IAPManager.GetIAPObject(key); if (obj == null) continue; //overwrite IAP properties JSONNode node = data[key]; if(!string.IsNullOrEmpty(node["title"])) obj.title = node["title"]; if(!string.IsNullOrEmpty(node["description"])) obj.description = node["description"]; if (!string.IsNullOrEmpty(node["type"])) { switch (node["type"]) { case "consumable": obj.type = IAPType.consumableVirtual; break; default: obj.type = IAPType.nonConsumableVirtual; break; } } if (!string.IsNullOrEmpty(node["virtualPrice"].ToString())) { JSONNode virtualPrice = node["virtualPrice"]; List<IAPCurrency> curList = new List<IAPCurrency>(); for (int j = 0; j < virtualPrice.Count; j++) { IAPCurrency cur = new IAPCurrency(); cur.name = virtualPrice[j]["name"]; cur.amount = virtualPrice[j]["amount"].AsInt; curList.Add(cur); } obj.virtualPrice = curList; } if (!string.IsNullOrEmpty(node["requirement"].ToString())) { JSONNode requirement = node["requirement"]; obj.req.entry = requirement["entry"]; obj.req.labelText = requirement["labelText"]; obj.req.target = requirement["target"].AsInt; } } }
//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); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Import from JSON")) { string path = EditorUtility.OpenFolderPanel("Import IAP Settings from JSON", "", ""); if (path.Length != 0) { script.currencies = IAPEditorExporter.FromJSON(System.IO.File.ReadAllText(path + "/SimpleIAPSystem_Currencies.json")); currencyIndex = script.currencies.Count > 0 ? 0 : -1; script.IAPs = IAPEditorExporter.FromJSON(System.IO.File.ReadAllText(path + "/SimpleIAPSystem_IAPSettings.json"), script.currencies); return; } } if (GUILayout.Button("Export to JSON")) { string path = EditorUtility.SaveFolderPanel("Save IAP Settings as JSON", "", ""); if (path.Length != 0) { System.IO.File.WriteAllBytes(path + "/SimpleIAPSystem_IAPSettings.json", System.Text.Encoding.UTF8.GetBytes(IAPEditorExporter.ToJSON(script.IAPs))); System.IO.File.WriteAllBytes(path + "/SimpleIAPSystem_IAPSettings_PlayFab.json", System.Text.Encoding.UTF8.GetBytes(IAPEditorExporter.ToJSON(script.IAPs, true))); System.IO.File.WriteAllBytes(path + "/SimpleIAPSystem_Currencies.json", System.Text.Encoding.UTF8.GetBytes(IAPEditorExporter.ToJSON(script.currencies))); } } EditorGUILayout.EndHorizontal(); 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.currencies.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.currencies.Count > 0) { GUI.Box(new Rect(0, 0, iapEditor.maxSize.x, 90), ""); } //loop through currencies for (int i = 0; i < script.currencies.Count; i++) { EditorGUILayout.BeginVertical(); //draw currency properties, //such as name and amount EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Name", GUILayout.Width(44)); script.currencies[i].name = EditorGUILayout.TextField(script.currencies[i].name, GUILayout.Width(54)).ToLower(); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Default", GUILayout.Width(44)); script.currencies[i].amount = EditorGUILayout.IntField(script.currencies[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.currencies.RemoveAt(i); //reposition current currency index if (script.currencies.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.currencies.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 2: newObj.editorType = IAPType.Currency; for (int j = 0; j < script.currencies.Count; j++) { newObj.virtualPrice.Add(new IAPCurrency()); } break; case 3: newObj.editorType = IAPType.Virtual; for (int j = 0; j < script.currencies.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(); GUILayout.Box("", GUILayout.Height(15), GUILayout.Width(15)); Rect foldoutRect = GUILayoutUtility.GetLastRect(); if (obj.editorType != IAPType.Virtual) { obj.platformFoldout = EditorGUI.Foldout(foldoutRect, obj.platformFoldout, ""); } //draw IAPObject (item/product) properties obj.id = EditorGUILayout.TextField(obj.id, GUILayout.MaxWidth(150)); if (!string.IsNullOrEmpty(obj.id)) { obj.id = obj.id.Replace(" ", ""); } obj.icon = EditorGUILayout.ObjectField(obj.icon, typeof(Sprite), false, GUILayout.MaxWidth(65)) as Sprite; obj.editorType = (IAPType)EditorGUILayout.EnumPopup(obj.editorType, GUILayout.MaxWidth(70)); EditorGUI.BeginDisabledGroup(obj.editorType == IAPType.Currency); if (obj.editorType == IAPType.Currency) { obj.type = ProductType.Consumable; } obj.type = (ProductType)EditorGUILayout.EnumPopup(obj.type, GUILayout.MaxWidth(110)); EditorGUI.EndDisabledGroup(); obj.title = EditorGUILayout.TextField(obj.title, GUILayout.MaxWidth(180)); obj.description = EditorGUILayout.TextField(obj.description); EditorGUI.BeginDisabledGroup(obj.editorType == IAPType.Virtual); obj.realPrice = EditorGUILayout.TextField(obj.realPrice, GUILayout.MaxWidth(55)); EditorGUI.EndDisabledGroup(); if (obj.editorType == IAPType.Virtual && (int)obj.type > 1) { Debug.LogWarning("Subscriptions are not available for virtual products. Resetting to Consumable."); obj.type = ProductType.Consumable; } EditorGUI.BeginDisabledGroup(obj.editorType == IAPType.Default); IAPCurrency cur = null; if (obj.editorType != IAPType.Default && currencyIndex >= 0 && obj.virtualPrice.Count > currencyIndex) { cur = obj.virtualPrice[currencyIndex]; } if (cur == null) { EditorGUILayout.LabelField("", GUILayout.MinWidth(75), GUILayout.MaxWidth(104)); } else { cur.name = currencyNames[currencyIndex]; EditorGUILayout.LabelField(cur.name, GUILayout.MinWidth(35), GUILayout.MaxWidth(50)); cur.amount = EditorGUILayout.IntField(cur.amount, GUILayout.MinWidth(35), GUILayout.MaxWidth(50)); } EditorGUI.EndDisabledGroup(); EditorGUI.BeginDisabledGroup(obj.type != ProductType.Consumable || obj.editorType == IAPType.Currency || obj.id == "restore"); obj.usageCount = Mathf.Clamp(EditorGUILayout.IntField(obj.usageCount, GUILayout.MaxWidth(35)), 0, int.MaxValue); EditorGUI.EndDisabledGroup(); if (obj.type != ProductType.Consumable) { obj.usageCount = 1; } 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)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"); if (GUILayout.Button("+", GUILayout.MaxWidth(35))) { obj.storeIDs.Add(new StoreID("None", "")); } for (int k = 0; k < obj.storeIDs.Count; k++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(40); obj.storeIDs[k].store = EditorGUILayout.EnumPopup((IAPPlatform)System.Enum.Parse(typeof(IAPPlatform), obj.storeIDs[k].store), GUILayout.MaxWidth(150)).ToString(); obj.storeIDs[k].id = EditorGUILayout.TextField(obj.storeIDs[k].id, GUILayout.Width(150)); if (!string.IsNullOrEmpty(obj.storeIDs[k].id)) { obj.storeIDs[k].id = obj.storeIDs[k].id.Replace(" ", ""); } GUI.backgroundColor = Color.gray; if (GUILayout.Button("X", GUILayout.MaxWidth(20))) { obj.storeIDs.RemoveAt(k); if (obj.storeIDs.Count == 0) { obj.platformFoldout = false; } break; } GUI.backgroundColor = Color.white; EditorGUILayout.EndHorizontal(); } GUILayout.Space(10); } } 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(); }