Hi, I am using Soomla plugin for Unity. Following are the implementation for my IStoreAssets and the UI Code that calls the Soomla functions:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Soomla;
using Soomla.Store;
using System;
public class StoreModel : IStoreAssets
{
/// <summary>
/// see parent.
/// </summary>
public int GetVersion() {
return 0;
}
/// <summary>
/// see parent.
/// </summary>
public VirtualCurrency[] GetCurrencies() {
return new VirtualCurrency[]{GOLD_COINS};
}
/// <summary>
/// see parent.
/// </summary>
public VirtualGood[] GetGoods() {
return new VirtualGood[] {};
}
/// <summary>
/// see parent.
/// </summary>
public VirtualCurrencyPack[] GetCurrencyPacks() {
VirtualCurrencyPack[] vp = new VirtualCurrencyPack[]{MY_5000_COINS, MY_10000_COINS, MY_20000_COINS};
return vp;
};
}
public NonConsumableItem[] GetNonConsumableItems() {
return new NonConsumableItem[]{};
}
/// <summary>
/// see parent.
/// </summary>
public VirtualCategory[] GetCategories() {
return new VirtualCategory[]{};
}
/// <summary>
/// see parent.
/// </summary>
public const string COINS_CURRENCY_ITEM_ID = "gold_coin";
public const string MY_5000_COINS_PRODUCT_ID = "android.test.purchased";
public const string MY_10000_COINS_PRODUCT_ID = "android.test.canceled";
public const string MY_20000_COINS_PRODUCT_ID = "20000_coins";
public static VirtualCurrency GOLD_COINS = new VirtualCurrency(
"Gold Coins", // name
"Game's Gold Coins", // description
COINS_CURRENCY_ITEM_ID // item id
);
public static VirtualCurrencyPack MY_5000_COINS = new VirtualCurrencyPack(
"5000 Coins", // name
"5,000 Gold Coins for you to purchase those cool Power Ups and Basic Needs.", // description
"5000_coins", // item id
5000, // number of currencies in the pack
COINS_CURRENCY_ITEM_ID, // the currency associated with this pack
new PurchaseWithMarket(MY_5000_COINS_PRODUCT_ID, 0.99)
);
public static VirtualCurrencyPack MY_10000_COINS = new VirtualCurrencyPack(
"10,000 Coins", // name
"10,000 Gold Coins for you to purchase those cool Power Ups and Basic Needs.", // description
"10000_coins", // item id
10000, // number of currencies in the pack
COINS_CURRENCY_ITEM_ID, // the currency associated with this pack
new PurchaseWithMarket(MY_10000_COINS_PRODUCT_ID, 1.99)
);
public static VirtualCurrencyPack MY_20000_COINS = new VirtualCurrencyPack(
"20,000 Coins", // name
"20,000 Gold Coins for you to purchase those cool Power Ups and Basic Needs.", // description
"20000_coins", // item id
20000, // number of currencies in the pack
COINS_CURRENCY_ITEM_ID, // the currency associated with this pack
new PurchaseWithMarket(MY_20000_COINS_PRODUCT_ID, 2.49)
);
}
UI Code:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Soomla;
using Soomla.Store;
public class IAPHandler : MonoBehaviour
{
public static List<VirtualCurrencyPack> VirtualCurrencyPacks = null;
public delegate void coinPurchaseEventHandler(int coins);
public static event coinPurchaseEventHandler giveCoins;
int currentPurchase;
// Use this for initialization
void Start ()
{
Debug.Log ("Soomla Init");
if (!iapInitialized) {
SoomlaStore.Initialize (new StoreModel());
iapInitialized = true;
StoreEvents.OnSoomlaStoreInitialized += OnSoomlaStoreInitialized;
StoreEvents.OnMarketPurchaseStarted += OnMarketPurchaseStarted;
StoreEvents.OnMarketPurchase += OnMarketPurchase;
StoreEvents.OnItemPurchaseStarted += OnItemPurchaseStarted;
StoreEvents.OnItemPurchased += OnItemPurchased;
StoreEvents.OnUnexpectedErrorInStore += OnUnexpectedErrorInStore;
StoreEvents.OnMarketPurchaseCancelled += OnMarketPurchaseCancelled;
}
}
public void OnSoomlaStoreInitialized()
{
VirtualCurrencyPacks = StoreInfo.GetVirtualCurrencyPacks ();
for (int i = 0; i < VirtualCurrencyPacks.Count; i++) {
Debug.Log("CSK Order: " + VirtualCurrencyPacks*.ItemId);*
-
}* -
}*
-
string s = “”;*
-
public void OnMarketPurchaseStarted( PurchasableVirtualItem pvi ) {*
-
Debug.Log( "CSK: OnMarketPurchaseStarted: " + pvi.ItemId );* -
s += "OnMarketPurchaseStarted: " + pvi.ItemId;* -
}*
-
public void OnMarketPurchaseCancelled( PurchasableVirtualItem pvi ) {*
-
Debug.Log( "CSK: OnMarketPurchaseCancelled: " + pvi.ItemId );* -
s += "OnMarketPurchaseCancelled: " + pvi.ItemId;* -
Debug.Log("CSK: after purchase cancelled: " + StoreInventory.GetItemBalance(StoreInfo.GetVirtualCurrencies()[0].ItemId));* -
}*
-
public void OnMarketPurchase( PurchasableVirtualItem pvi , string a, string b) {*
-
Debug.Log( "CSK: OnMarketPurchase: " + pvi.ItemId );* -
}*
-
public void OnItemPurchaseStarted( PurchasableVirtualItem pvi ) {*
-
Debug.Log( "CSK: OnItemPurchaseStarted: " + pvi.ItemId );* -
}*
-
public void OnItemPurchased( PurchasableVirtualItem pvi, string a ) {*
-
Debug.Log( "CSK: OnItemPurchased: " + pvi.ItemId );* -
giveCoins(currentPurchase);* -
Debug.Log("CSK: after purchase: " + StoreInventory.GetItemBalance(StoreInfo.GetVirtualCurrencies()[0].ItemId));* -
}*
-
public void OnStoreControllerInitialized( ) {*
-
Debug.Log( "CSK: OnStoreControllerInitialized" );* -
s += "OnStoreControllerInitialized";* -
}*
-
public void OnUnexpectedErrorInStore( string err ) {*
-
Debug.Log( "CSK: OnUnexpectedErrorInStore" + err );* -
s += "OnUnexpectedErrorInStore" + err;* -
}*
-
public void buyGoldCoinsPackUsingIndex()*
-
{*
-
int index = 0;* -
string callerBtn = UIButton.current.name;* -
Debug.Log ("CSK Btn Clicked: " + callerBtn);* -
if (callerBtn == "5000BuyBtn")* -
index = 0;* -
else if( callerBtn == "10000BuyBtn")* -
index = 1;* -
else if( callerBtn == "20000BuyBtn")* -
index = 2;* -
Debug.Log ("CSK Index selected: " + index);* -
Debug.Log("CSK: before purchase" + StoreInventory.GetItemBalance(StoreInfo.GetVirtualCurrencies()[0].ItemId));* -
VirtualCurrencyPack selectedPack = VirtualCurrencyPacks [index];* -
try{* -
Debug.Log ("CSK selectedPack: " + selectedPack.ItemId);* -
StoreInventory.BuyItem(selectedPack.ItemId);* -
currentPurchase = selectedPack.CurrencyAmount;* -
}* -
catch(UnityException e)* -
{* -
}* -
}*
}
Everything is fine the first time the game runs. But when I restart the level woth Application.LoadLevel(Application.loadedLevel); everything goes wrong.
I think Soomla is initialized again, so the event handlers like OnItemPurchased are called once after first restart, twice after 2nd restart and keeps adding. Also my below VirtualCurrencyPack[] ‘s elements’ indexes are changed from the first restart to the order 20000, 5000 and 10000.
VirtualCurrencyPack[] vp = new VirtualCurrencyPack[]{MY_5000_COINS, MY_10000_COINS, MY_20000_COINS};
Can someone please tell me if Soomla hasnt handled the level restarts and anything has to be done manually ?