• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by karthikrock · Sep 20, 2014 at 03:09 PM · soomla

Soomla - Game Restart - Not handled

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[i].ItemId);
 
                 }
 
     }
     
     
     string s = "<nothing>";
     
     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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by y_nizan · Sep 23, 2014 at 04:29 AM

Hi @karthikrock - did you try posting this to answers.soom.la? You will get faster responses there.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by keni4 · Feb 20, 2015 at 03:08 PM

You need to initialize Soomla once at the game start, and do not do this on another level loaded. And you need to unsubscribe from soomla events when your level or object with this methods are going to be destroyed (in OnDestroy()), because your methods subscribed to events are still live.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karthikrock · Feb 21, 2015 at 10:57 PM 0
Share

I had already solved this. But thanks a lot for answering, It was exactly "unsubscribing" issue. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

rotating at mouse 0 Answers

Coroutine to WaitForSeconds() on enemy AI causing all instances of AI to pause. Solution? 1 Answer

Question on UnityScript and C# 1 Answer

Diffrences between pro and free version of Unity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges