Advertisement
ayoung

ExampleWindow.cs

Jul 28th, 2014
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. namespace Soomla.Store.Example                                                      //Allows for access to Soomla API                                      
  8. {
  9.     public class ExampleWindow : MonoBehaviour                                     
  10.     {
  11.         //variables
  12.         public static List<NonConsumableItem> NonConsumableItems = null;            //Creates a list to store non-consumable items (ex. levels, maps, etc)
  13.  
  14.         public Transform cube;                                                      // Stores the scene cube as a variable
  15.        
  16.         public float secTime    = 2.0f;
  17.         public float totTime    = 0.0f;
  18.        
  19.         //when the scene starts
  20.         void Start ()
  21.         {
  22.             StoreEvents.OnSoomlaStoreInitialized += onSoomlaStoreIntitialized;      //Handle the initialization of store events
  23.             SoomlaStore.Initialize (new ExampleAssets());                           //Intialize the store
  24.        
  25.         }
  26.        
  27.        
  28.         //when the store is intialized (not ideal but totally works)
  29.         public void onSoomlaStoreIntitialized()                                    
  30.         {
  31.             NonConsumableItems       = StoreInfo.GetNonConsumableItems();                   //get the list of nonconsumable items, set equal to NonConsumableItems list
  32.  
  33.        
  34.         }
  35.        
  36.        
  37.         //Assign cube color based on it (using playerprefs) (see CheckIAP_PurchaseStatus() function below to understand)
  38.         void Update ()
  39.         {
  40.             if (Time.timeSinceLevelLoad > totTime)
  41.             {
  42.                 CheckIAP_PurchaseStatus ();                                             //Check status of in app purchase (true/false if player has purchased it)
  43.  
  44.                 totTime = Time.timeSinceLevelLoad + secTime;
  45.  
  46.  
  47.                
  48.             }
  49.             if(PlayerPrefs.GetInt("Cube_green") == 0)                                   // if player has purchased item, turn the cube green
  50.                 cube.transform.renderer.material.color = Color.red;
  51.            
  52.            
  53.             if(PlayerPrefs.GetInt("Cube_green") == 1)                                   // if player has not purchased item (or hasnt restored previous purchases) turn the cube red
  54.                 cube.transform.renderer.material.color = Color.green;
  55.         }
  56.        
  57.        
  58.         //Check the Status of the In App Purchase (true/false if player has bought it)
  59.         void CheckIAP_PurchaseStatus()
  60.         {
  61.             NonConsumableItem turn_green = NonConsumableItems[0];                       // create a variable to store the IAP (makes it easier to tell what it is)
  62.             Debug.Log (StoreInventory.NonConsumableItemExists(turn_green.ItemId));      // Print the current status of the IAP
  63.            
  64.             if (StoreInventory.NonConsumableItemExists (turn_green.ItemId))             // check if the non-consumable in app purchase has been bought or not
  65.             {
  66.                 PlayerPrefs.SetInt ("Cube_green", 1);                                   // if the IAP has been bought, make the cube green correspondign with the purchase
  67.             }
  68.         }
  69.        
  70.        
  71.         //On Screen Interface
  72.         void OnGUI()
  73.         {
  74.  
  75.             //Button To PURCHASE ITEM
  76.             if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.4f, 100,100),"Make green?"))
  77.             {
  78.                 NonConsumableItem turn_green = NonConsumableItems[0];                   // create a variable to store the IAP (makes it easier to tell what it is)
  79.                
  80.                 try
  81.                 {
  82.                     StoreInventory.BuyItem (turn_green.ItemId);                         // if the purchases can be completed sucessfully
  83.                 }
  84.                
  85.                 catch (Exception e)                                                 // if the purchase cannot be completed trigger an error message connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  86.                 {
  87.                     Debug.Log ("SOOMLA/UNITY" + e.Message);                        
  88.                 }
  89.             }
  90.  
  91.             //Button to RESTORE PURCHASES
  92.             if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.8f, 100,100),"Restore\nPurchases"))
  93.             {
  94.                 try
  95.                 {
  96.                     SoomlaStore.RestoreTransactions();                              // restore purchases if possible
  97.                     //StoreController.RestoreTransactions().
  98.                 }
  99.                
  100.                 catch (Exception e)
  101.                 {
  102.                     Debug.Log ("SOOMLA/UNITY" + e.Message);                         // if restoring purchases fails (connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  103.                 }
  104.             }
  105.            
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement