private void handleGameLevelUpdated(IAPGameLevel obj) { if (obj.uid == this.uid) { UpdateGameLevelTemplate(obj, level); } }
// Use this for initialization void Start() { if (targetType == IAPType.InAppPurchase) { IAPPackage obj = IAPInventoryManager.GetPackage(uid); if (obj != null) { handlePackageUpdated(obj); IAPInventoryManager.OnIAPInitialized += handleOnIAPInitialized; } } else if (targetType == IAPType.Currency) { IAPCurrency obj = IAPInventoryManager.GetCurrency(uid); if (obj != null) { int result = 0; if (textType == IAPTextType.price) { result = obj.price; } else if (textType == IAPTextType.amount) { result = obj.amount; } _lastNumber = result; handleCurrencyUpdated(obj); IAPInventoryManager.OnCurrencyUpdated += handleCurrencyUpdated; } } else if (targetType == IAPType.Inventory) { IAPInventory obj = IAPInventoryManager.GetInventory(uid); if (obj != null) { handleInventoryUpdated(obj); IAPInventoryManager.OnInventoryUpdated += handleInventoryUpdated; } } else if (targetType == IAPType.Ability) { IAPAbility obj = IAPInventoryManager.GetAbility(uid); if (obj != null) { handleAbilityUpdated(obj); IAPInventoryManager.OnAbilityUpdated += handleAbilityUpdated; } } else if (targetType == IAPType.GameLevel) { IAPGameLevel obj = IAPInventoryManager.GetGameLevel(uid); if (obj != null) { UpdateGameLevelTemplate(obj, 0); } } }
public void GotoSubLevel(IAPGameLevel level, int index) { Debug.Log(level.levels[index].locked); if (level.levels[index].locked) { // Do somthing if locked } else { GotoGame("Demo_Game_Play"); } }
protected void UpdateGameLevelTemplate(IAPGameLevel obj, int subLevelIndex) { if (obj != null && obj.levels != null && subLevelIndex < obj.levels.Count) { IAPGameSubLevel lv = obj.levels[subLevelIndex]; // Update images Image[] imgs = gameObject.GetComponentsInChildren <Image>(); foreach (Image img in imgs) { if (img.gameObject == gameObject || img.name == "icon") { // check if level locked bool islocked = false; if (obj.isLocked()) { islocked = (obj.GetPropertyValue("locked", subLevelIndex) > 0); } if (islocked) { if (obj.lockedIcon != null) { img.sprite = obj.lockedIcon; } } else { if (obj.icon != null) { img.sprite = obj.icon; } } } else if (img.name == "currency_icon") { IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency); if (currency != null && currency.icon != null) { img.sprite = currency.icon; } } else if (img.name == "level") { if (lv != null && lv.icon != null) { img.sprite = lv.icon; } } } // Update Property Image IAPPropertyImage[] propImgs = gameObject.GetComponentsInChildren <IAPPropertyImage>(); foreach (IAPPropertyImage propImg in propImgs) { propImg.level = subLevelIndex; propImg.obj = obj; propImg.UpdateImage(); } // Update text Text[] txts = gameObject.GetComponentsInChildren <Text>(); foreach (Text txt in txts) { string name = txt.name.ToLower(); if (name == "level") { txt.text = (subLevelIndex + 1).ToString(); } else { UpdateTextWithObject(txt, obj, name); } } } }
// Use this for initialization void Start() { // hide the background Image background = gameObject.GetComponent <Image>(); if (background != null) { background.enabled = false; } // Add item to viewport content // if(searchTag != null && searchTag != "") // if(searchTag != 0) // { if (itemType == IAPType.Inventory) { List <IAPInventory> inventoryList = IAPInventoryManager.GetInventoryListByTags(searchTag); if (inventoryList != null && itemTemplate != null) { DoLayout(inventoryList.Count); foreach (IAPInventory inventory in inventoryList) { GameObject obj = Instantiate(itemTemplate); IAPTemplate btn = obj.GetComponent <IAPTemplate>(); if (btn == null) { btn = obj.AddComponent <IAPTemplate>(); } btn.targetType = IAPType.Inventory; btn.uid = inventory.uid; btn.useConfirmDialog = useConfirmDialog; obj.transform.SetParent(_listView.transform); obj.transform.localScale = new Vector3(1, 1, 1); } } } else if (itemType == IAPType.InAppPurchase) { List <IAPPackage> packageList = IAPInventoryManager.GetPackageListByTags(searchTag); if (packageList != null && itemTemplate != null) { DoLayout(packageList.Count); foreach (IAPPackage package in packageList) { // Debug.Log("package " + package.uid + " type: "); GameObject obj = Instantiate(itemTemplate); IAPTemplate btn = obj.GetComponent <IAPTemplate>(); if (btn == null) { btn = obj.AddComponent <IAPTemplate>(); } btn.targetType = IAPType.InAppPurchase; btn.uid = package.uid; btn.useConfirmDialog = useConfirmDialog; obj.transform.SetParent(_listView.transform); obj.transform.localScale = new Vector3(1, 1, 1); } } } else if (itemType == IAPType.Ability) { List <IAPAbility> abilityList = IAPInventoryManager.GetAbilityListByTags(searchTag); // Debug.Log("abilityList: " + abilityList + " count : " + abilityList.Count); if (abilityList != null && itemTemplate != null) { DoLayout(abilityList.Count); foreach (IAPAbility ability in abilityList) { GameObject obj = Instantiate(itemTemplate); IAPTemplate btn = obj.GetComponent <IAPTemplate>(); if (btn == null) { btn = obj.AddComponent <IAPTemplate>(); } btn.targetType = IAPType.Ability; btn.uid = ability.uid; btn.useConfirmDialog = useConfirmDialog; obj.transform.SetParent(_listView.transform); obj.transform.localScale = new Vector3(1, 1, 1); } } } else if (itemType == IAPType.GameLevel) { IAPGameLevel gameLevel = IAPInventoryManager.GetGameLevel(uid); Debug.Log("GameLevel " + uid); if (gameLevel != null && itemTemplate != null) { Debug.Log("GameLevel " + gameLevel.levels.Count); DoLayout(gameLevel.levels.Count); for (int i = 0; i < gameLevel.levels.Count; i++) { GameObject obj = Instantiate(itemTemplate); IAPTemplate btn = obj.GetComponent <IAPTemplate>(); if (btn == null) { btn = obj.AddComponent <IAPTemplate>(); } btn.targetType = IAPType.GameLevel; btn.uid = gameLevel.uid; btn.level = i; btn.useConfirmDialog = useConfirmDialog; obj.transform.SetParent(_listView.transform); obj.transform.localScale = new Vector3(1, 1, 1); // Add Events IAPUIUtility.AddButtonCallback(obj, (GameObject go) => { IAPGameSubLevel lv = gameLevel.levels[btn.level]; bool islocked = false; islocked = (gameLevel.GetPropertyValue("locked", btn.level) > 0); Debug.LogFormat("Lock {0}", islocked); // if(gameLevel.isLocked()){ islocked = (gameLevel.GetPropertyValue("locked", btn.level) > 0); // } lv.locked = islocked; // if(islocked&&lv.price>0){ // } else { if (OnGameLevelSelect != null) { OnGameLevelSelect.Invoke(gameLevel, btn.level); } // } }, "self,select_button"); } } } // } }
private void handleOnGameLevelUpdated(IAPGameLevel gamelevel) { OnGameLevelUpdated.Invoke(gamelevel); }
/// <summary> /// Create the IAPInventoryManager instance. Call this before using any method. /// </summary> /// <param name="settings">Settings.</param> public static IAPInventoryManager Create(IAPSettings settings) { // Create instance of the IAPManager if (_instance == null) { // Check if settings valid if (settings != null) { _packageSettings = settings.packageList; _uiSettings = settings.uiSettings; _advancedSetting = settings.advancedSettings; // Create datastore switch (settings.databaseSettings.datastoreType) { case IAPDatastoreType.PlayerPref: _datastore = new IAPPlayerPrefDatastore(settings.databaseSettings.playerPrefPrefix); break; case IAPDatastoreType.TextFile: _datastore = new IAPTextFileDatastore(settings.databaseSettings.datastorePath); break; case IAPDatastoreType.XORBinaryFile: _datastore = new IAPBinaryFileDatastore(settings.databaseSettings.datastorePath); break; case IAPDatastoreType.AESEncryptedFile: string hash = settings.databaseSettings.cryptoHash; string salt = SystemInfo.deviceUniqueIdentifier.Substring(0, 8); string key = PlayerPrefs.GetString("RANDOM_KEY"); if (string.IsNullOrEmpty(key)) { key = IAPDatastore.Md5(DateTime.Now.ToString()).Substring(0, 16); PlayerPrefs.SetString("RANDOM_KEY", key); } _datastore = new IAPEncryptedFileDatastore(settings.databaseSettings.datastorePath, hash, key, salt); break; } _datastore.Build(); // Read the data from database Dictionary <string, IAPObjectData> currencyListData = _datastore.GetGroup("currencyList"); Dictionary <string, IAPObjectData> abilityListData = _datastore.GetGroup("abilityList"); Dictionary <string, IAPObjectData> inventoryListData = _datastore.GetGroup("inventoryList"); Dictionary <string, IAPObjectData> packageListData = _datastore.GetGroup("inventoryList"); Dictionary <string, IAPObjectData> gameListData = _datastore.GetGroup("gameList"); // Create the object context list _currencyIndex = new Dictionary <string, IAPCurrency>(); _inventoryIndex = new Dictionary <string, IAPInventory>(); _abilityIndex = new Dictionary <string, IAPAbility>(); _packageIndex = new Dictionary <string, IAPPackage>(); _gameIndex = new Dictionary <string, IAPGameLevel>(); _inventoryTagDictionary = new Dictionary <string, List <IAPInventory> >(); _abilityTagDictionary = new Dictionary <string, List <IAPAbility> >(); _packageTagDictionary = new Dictionary <string, List <IAPPackage> >(); _tags = settings.tagList; // check if need to save bool needSave = false; // Tags if (settings.tagList != null) { // Loop the settings and fill in the list foreach (string s in IAPSettings.DefaultTag) { _inventoryTagDictionary.Add(s, new List <IAPInventory>()); _packageTagDictionary.Add(s, new List <IAPPackage>()); _abilityTagDictionary.Add(s, new List <IAPAbility>()); } } // Check if currency list and inventory list valid if (settings.currencyList != null) { // Loop the settings and fill in the list foreach (IAPCurrencySetting s in settings.currencyList) { if (_currencyIndex.ContainsKey(s.uid)) { Log("Currency identify [" + s.uid + "] already exisit"); } else { IAPCurrency currency; // check if have saved data if (currencyListData.ContainsKey(s.uid)) { currency = new IAPCurrency(s.uid, s, currencyListData[s.uid], handleCurrencySave, handleCurrencyNotEnough); } else { // No saved data IAPObjectData d = s.data; currency = new IAPCurrency(s.uid, s, d, handleCurrencySave, handleCurrencyNotEnough); currencyListData.Add(s.uid, d); needSave = true; } _currencyIndex.Add(s.uid, currency); } } } // Check if ability list and inventory list valid if (settings.abilityList != null) { // Loop the settings and fill in the list foreach (IAPAbilitySetting s in settings.abilityList) { if (_abilityIndex.ContainsKey(s.uid)) { Log("Currency identify [" + s.uid + "] already exisit"); } else { IAPAbility ability; // Debug.Log("ability "+s.data); // check if have saved data if (abilityListData.ContainsKey(s.uid)) { ability = new IAPAbility(s.uid, s, abilityListData[s.uid], handleAbilitySave); } else { IAPObjectData d = s.data; ability = new IAPAbility(s.uid, s, d, handleAbilitySave); abilityListData.Add(s.uid, d); needSave = true; } // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; if (tag == "Ability") { ability.tags = ability.tags | 4; _abilityTagDictionary[tag].Add(ability); } else { int index = 1 << i; if ((index & s.tags) > 0) { if (!_abilityTagDictionary.ContainsKey(tag)) { _abilityTagDictionary.Add(tag, new List <IAPAbility>()); } _abilityTagDictionary[tag].Add(ability); } } } _abilityIndex.Add(s.uid, ability); } } } // Check if inventory list and inventory list valid if (settings.inventoryList != null) { foreach (IAPInventorySetting s in settings.inventoryList) { if (_inventoryIndex.ContainsKey(s.uid)) { Log("inventory identify [" + s.uid + "] already exisit"); } else { IAPInventory inventory; if (inventoryListData.ContainsKey(s.uid)) { inventory = new IAPInventory(s.uid, s, inventoryListData[s.uid], handleInventorySave); } else { IAPObjectData d = s.data; inventory = new IAPInventory(s.uid, s, d, handleInventorySave); inventoryListData.Add(s.uid, d); needSave = true; } // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; int index = 1 << i; if ((index & s.tags) > 0) { if (!_inventoryTagDictionary.ContainsKey(tag)) { _inventoryTagDictionary.Add(tag, new List <IAPInventory>()); } _inventoryTagDictionary[tag].Add(inventory); } } _inventoryIndex.Add(s.uid, inventory); } } } // Check if game list and inventory list valid if (settings.gameList != null) { // Loop the settings and fill in the list foreach (IAPGameLevelSetting s in settings.gameList) { if (_gameIndex.ContainsKey(s.uid)) { Log("Game identify [" + s.uid + "] already exisit"); } else { IAPGameLevel game; // Debug.LogFormat("Game {0}",s.uid); // check if have saved data if (gameListData.ContainsKey(s.uid)) { game = new IAPGameLevel(s.uid, s, gameListData[s.uid], handleGameLevelSave); } else { IAPObjectData d = s.data; game = new IAPGameLevel(s.uid, s, d, handleGameLevelSave); gameListData.Add(s.uid, d); needSave = true; } _gameIndex.Add(s.uid, game); } } } // Check if package list valid if (settings.packageList != null) { foreach (IAPPackageSetting s in settings.packageList) { if (_packageIndex.ContainsKey(s.productId)) { Log("Product ID [" + s.productId + "] already exisit"); } else { IAPPackage package; if (packageListData.ContainsKey(s.productId)) { package = new IAPPackage(s.productId, s, packageListData[s.productId], handlePackageSave); } else { IAPObjectData d = s.data; package = new IAPPackage(s.productId, s, d, handlePackageSave); inventoryListData.Add(s.productId, d); needSave = true; } _packageIndex.Add(s.productId, package); // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; if (tag == "IAP") { package.tags = package.tags | 1; _packageTagDictionary[tag].Add(package); } else if (tag == "IAP_Fetch" && package.fetchFromStore) { package.tags = package.tags | 2; _packageTagDictionary[tag].Add(package); } else { int index = 1 << i; if ((index & s.tags) > 0) { if (!_packageTagDictionary.ContainsKey(tag)) { _packageTagDictionary.Add(tag, new List <IAPPackage>()); } _packageTagDictionary[tag].Add(package); } } } } } } // Create instance _instance = new IAPInventoryManager(); // Save change if (needSave) { Save(); } } return(null); } return(_instance); }
// 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"); } } }