Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Soomla.Store.Example //Allows for access to Soomla API
- {
- public class ExampleWindow : MonoBehaviour
- {
- //variables
- public static List<NonConsumableItem> NonConsumableItems = null; //Creates a list to store non-consumable items (ex. levels, maps, etc)
- public Transform cube; // Stores the scene cube as a variable
- public float secTime = 2.0f;
- public float totTime = 0.0f;
- //when the scene starts
- void Start ()
- {
- Application.LoadLevel ("test"); //Load actual scene
- DontDestroyOnLoad(transform.gameObject); //Allows this gameObject to remain during level loads, solving restart crashes
- StoreEvents.OnSoomlaStoreInitialized += onSoomlaStoreIntitialized; //Handle the initialization of store events
- SoomlaStore.Initialize (new ExampleAssets()); //Intialize the store
- }
- //when the store is intialized (not ideal but totally works)
- public void onSoomlaStoreIntitialized()
- {
- NonConsumableItems = StoreInfo.GetNonConsumableItems(); //get the list of nonconsumable items, set equal to NonConsumableItems list
- }
- void OnLevelWasLoaded(int level) //Assigns the cube if the level is loaded to correct level
- {
- if (level == 1) //the second level in the build list (0 == first level, 1 == second level)
- {
- cube = GameObject.Find ("testCube").transform; //Assign cube by finding it the the hierarchy in the scene ( via its name)
- }
- }
- //Assign cube color based on it (using playerprefs) (see CheckIAP_PurchaseStatus() function below to understand)
- void Update ()
- {
- if (Time.timeSinceLevelLoad > totTime)
- {
- CheckIAP_PurchaseStatus (); //Check status of in app purchase (true/false if player has purchased it)
- totTime = Time.timeSinceLevelLoad + secTime;
- }
- if(cube != null)
- {
- if(PlayerPrefs.GetInt("Cube_green") == 0) // if player has purchased item, turn the cube green
- cube.transform.renderer.material.color = Color.red;
- if(PlayerPrefs.GetInt("Cube_green") == 1) // if player has not purchased item (or hasnt restored previous purchases) turn the cube red
- cube.transform.renderer.material.color = Color.green;
- }
- }
- //Check the Status of the In App Purchase (true/false if player has bought it)
- void CheckIAP_PurchaseStatus()
- {
- NonConsumableItem turn_green = NonConsumableItems[0]; // create a variable to store the IAP (makes it easier to tell what it is)
- Debug.Log (StoreInventory.NonConsumableItemExists(turn_green.ItemId)); // Print the current status of the IAP
- if (StoreInventory.NonConsumableItemExists (turn_green.ItemId)) // check if the non-consumable in app purchase has been bought or not
- {
- PlayerPrefs.SetInt ("Cube_green", 1); // if the IAP has been bought, make the cube green correspondign with the purchase
- }
- }
- //On Screen Interface
- void OnGUI()
- {
- //Button To PURCHASE ITEM
- if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.4f, 100,100),"Make green?"))
- {
- NonConsumableItem turn_green = NonConsumableItems[0]; // create a variable to store the IAP (makes it easier to tell what it is)
- try
- {
- StoreInventory.BuyItem (turn_green.ItemId); // if the purchases can be completed sucessfully
- }
- catch (Exception e) // if the purchase cannot be completed trigger an error message connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
- {
- Debug.Log ("SOOMLA/UNITY" + e.Message);
- }
- }
- //Button to RESTORE PURCHASES
- if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.8f, 100,100),"Restore\nPurchases"))
- {
- try
- {
- SoomlaStore.RestoreTransactions(); // restore purchases if possible
- //StoreController.RestoreTransactions().
- }
- catch (Exception e)
- {
- Debug.Log ("SOOMLA/UNITY" + e.Message); // if restoring purchases fails (connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
- }
- }
- //Button to RESTORE PURCHASES
- if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.8f, 100,100),"Restart"))
- {
- Application.LoadLevel (Application.loadedLevel);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement