Advertisement
ayoung

ExampleWindow.cs - 2015

Feb 6th, 2015
1,796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. //ExampleWindowScript.cs
  2. //Alexander Young
  3. //February 5, 2015
  4. //Description - Creates the functionality to allow for in-app purchasing, specifically with reguards the GUI and using purchases to make changes to the game
  5.  
  6. using System;
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10.  
  11. namespace Soomla.Store.Example
  12. { //Allows for access to Soomla API
  13. public class ExampleWindow : MonoBehaviour
  14. {
  15. public Transform cube; // Stores the scene cube as a variable
  16. //secTime/floatTime are used to check for IAP changes every 2 seconds (ideally, you should only check for updates when you absoultely need to. This just shows a way check for the IAP changes)
  17. public float secTime = 2.0f;
  18. public float totTime = 0.0f;
  19. public bool greenCubeIAPOwned = false;
  20.  
  21. //Load the Scene with the cube/ setup the soomla intergration
  22. void Start ()
  23. {
  24. Application.LoadLevel ("test"); //Load actual scene
  25. DontDestroyOnLoad(transform.gameObject); //Allows this gameObject to remain during level loads, solving restart crashes
  26. StoreEvents.OnSoomlaStoreInitialized += onSoomlaStoreIntitialized; //Handle the initialization of store events (calls function below - unneeded in this case)
  27. SoomlaStore.Initialize (new ExampleAssets()); //Intialize the store
  28. }
  29.  
  30. //this is likely unnecessary, but may be required depending on how you plan on doing IAPS
  31. public void onSoomlaStoreIntitialized(){
  32. }
  33.  
  34. //ASSIGN CUBE TO BE COLORED
  35. void OnLevelWasLoaded(int level)
  36. { //Assigns the cube if the level is loaded to correct level{
  37. if (level == 1) { //the second level in the build list (0 == first level, 1 == second level)
  38. cube = GameObject.Find ("testCube").transform; //Assign cube by finding it the the hierarchy in the scene ( via its name)
  39. }
  40. }
  41.  
  42. //UPDATE CUBE COLOR
  43. //Assign cube color based on it (using playerprefs) (see CheckIAP_PurchaseStatus() function below to understand)
  44. void Update ()
  45. {
  46. if (Time.timeSinceLevelLoad > totTime)
  47. {
  48. CheckIAP_PurchaseStatus (); //Check status of in app purchase (true/false if player has purchased it)
  49. totTime = Time.timeSinceLevelLoad + secTime;
  50. }
  51. if(cube != null)
  52. {
  53. if(!greenCubeIAPOwned)
  54. {
  55. cube.transform.renderer.material.color = Color.red; // if player has purchased item, turn the cube green
  56. }
  57.  
  58. if(greenCubeIAPOwned)
  59. {
  60. cube.transform.renderer.material.color = Color.green; // if player has not purchased item (or hasnt restored previous purchases) turn the cube red
  61. }
  62. }
  63. }
  64.  
  65. //CHECK IAP STATUS (0 = not owned, 1 = owned for GetItemBalance())
  66. //Check the Status of the In App Purchase (true/false if player has bought it)
  67. void CheckIAP_PurchaseStatus(){
  68. Debug.Log (StoreInventory.GetItemBalance("turn_green_item_id")); // Print the current status of the IAP
  69. if (StoreInventory.GetItemBalance("turn_green_item_id") >= 1)
  70. {
  71. greenCubeIAPOwned = true; // check if the non-consumable in app purchase has been bought or not
  72. }
  73. }
  74.  
  75. //GUI ELEMENTS
  76. void OnGUI() {
  77. //Button To PURCHASE ITEM
  78. if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.4f, 150,150),"Make green?"))
  79. {
  80. try {
  81. Debug.Log("attempt to purchase");
  82.  
  83. StoreInventory.BuyItem ("turn_green_item_id"); // if the purchases can be completed sucessfully
  84. }
  85. catch (Exception e)
  86. { // if the purchase cannot be completed trigger an error message connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  87. Debug.Log ("SOOMLA/UNITY" + e.Message);
  88. }
  89. }
  90. //Button to RESTORE PURCHASES
  91. if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.8f, 150,150),"Restore\nPurchases")) {
  92. try
  93. {
  94. SoomlaStore.RestoreTransactions(); // restore purchases if possible
  95. }
  96. catch (Exception e)
  97. {
  98. Debug.Log ("SOOMLA/UNITY" + e.Message); // if restoring purchases fails (connectivity issue, IAP doesnt exist on ItunesConnect, etc...)
  99. }
  100. }
  101.  
  102. //Button to RESTART LEVEL (ensure it doesnt crash)
  103. if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.8f, 150,150),"Restart"))
  104. {
  105. Application.LoadLevel (Application.loadedLevel);
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement