Advertisement
ayoung

ExampleWindowFix.cs

Aug 7th, 2014
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 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.  
  20.  
  21. //when the scene starts
  22. void Start ()
  23. {
  24. Application.LoadLevel ("test"); //Load actual scene
  25.  
  26. DontDestroyOnLoad(transform.gameObject); //Allows this gameObject to remain during level loads, solving restart crashes
  27.  
  28. StoreEvents.OnSoomlaStoreInitialized += onSoomlaStoreIntitialized; //Handle the initialization of store events
  29. SoomlaStore.Initialize (new ExampleAssets()); //Intialize the store
  30. }
  31.  
  32.  
  33. //when the store is intialized (not ideal but totally works)
  34. public void onSoomlaStoreIntitialized()
  35. {
  36. NonConsumableItems = StoreInfo.GetNonConsumableItems(); //get the list of nonconsumable items, set equal to NonConsumableItems list
  37. }
  38.  
  39. void OnLevelWasLoaded(int level) //Assigns the cube if the level is loaded to correct level
  40. {
  41. if (level == 1) //the second level in the build list (0 == first level, 1 == second level)
  42. {
  43. cube = GameObject.Find ("testCube").transform; //Assign cube by finding it the the hierarchy in the scene ( via its name)
  44. }
  45. }
  46.  
  47. //Assign cube color based on it (using playerprefs) (see CheckIAP_PurchaseStatus() function below to understand)
  48. void Update ()
  49. {
  50.  
  51. if (Time.timeSinceLevelLoad > totTime)
  52. {
  53. CheckIAP_PurchaseStatus (); //Check status of in app purchase (true/false if player has purchased it)
  54.  
  55. totTime = Time.timeSinceLevelLoad + secTime;
  56.  
  57.  
  58.  
  59. }
  60.  
  61. if(cube != null)
  62. {
  63. if(PlayerPrefs.GetInt("Cube_green") == 0) // if player has purchased item, turn the cube green
  64. cube.transform.renderer.material.color = Color.red;
  65.  
  66.  
  67. if(PlayerPrefs.GetInt("Cube_green") == 1) // if player has not purchased item (or hasnt restored previous purchases) turn the cube red
  68. cube.transform.renderer.material.color = Color.green;
  69. }
  70. }
  71.  
  72.  
  73. //Check the Status of the In App Purchase (true/false if player has bought it)
  74. void CheckIAP_PurchaseStatus()
  75. {
  76. NonConsumableItem turn_green = NonConsumableItems[0]; // create a variable to store the IAP (makes it easier to tell what it is)
  77. Debug.Log (StoreInventory.NonConsumableItemExists(turn_green.ItemId)); // Print the current status of the IAP
  78.  
  79. if (StoreInventory.NonConsumableItemExists (turn_green.ItemId)) // check if the non-consumable in app purchase has been bought or not
  80. {
  81. PlayerPrefs.SetInt ("Cube_green", 1); // if the IAP has been bought, make the cube green correspondign with the purchase
  82. }
  83. }
  84.  
  85.  
  86. //On Screen Interface
  87. void OnGUI()
  88. {
  89.  
  90. //Button To PURCHASE ITEM
  91. if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.4f, 100,100),"Make green?"))
  92. {
  93. NonConsumableItem turn_green = NonConsumableItems[0]; // create a variable to store the IAP (makes it easier to tell what it is)
  94.  
  95. try
  96. {
  97. StoreInventory.BuyItem (turn_green.ItemId); // if the purchases can be completed sucessfully
  98. }
  99.  
  100. catch (Exception e) // if the purchase cannot be completed trigger an error message connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  101. {
  102. Debug.Log ("SOOMLA/UNITY" + e.Message);
  103. }
  104. }
  105.  
  106. //Button to RESTORE PURCHASES
  107. if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.8f, 100,100),"Restore\nPurchases"))
  108. {
  109. try
  110. {
  111. SoomlaStore.RestoreTransactions(); // restore purchases if possible
  112. //StoreController.RestoreTransactions().
  113. }
  114.  
  115. catch (Exception e)
  116. {
  117. Debug.Log ("SOOMLA/UNITY" + e.Message); // if restoring purchases fails (connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  118. }
  119. }
  120.  
  121. //Button to RESTORE PURCHASES
  122. if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.8f, 100,100),"Restart"))
  123. {
  124. Application.LoadLevel (Application.loadedLevel);
  125. }
  126.  
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement