Advertisement
ardakaya

myMobyShop

Dec 8th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. namespace MobyShop {
  6.  
  7.     public class myMobyShop : MonoBehaviour {
  8.  
  9.     public Transform cube;
  10.  
  11.     public float secTime          = 2.0f;
  12.     public float totTime          = 0.0f;
  13.     public bool greenCubeIAPOwned = false;
  14.  
  15.     // Use this for initialization
  16.     void Start () {
  17.         cube = GameObject.Find ("testCube").transform;
  18.    
  19.     }
  20.    
  21.  
  22.     //UPDATE CUBE COLOR
  23.     //Assign cube color based on it (using playerprefs) (see CheckIAP_PurchaseStatus() function below to understand)
  24.     void Update ()
  25.     {
  26.         if (Time.timeSinceLevelLoad > totTime)
  27.         {
  28.             CheckIAP_PurchaseStatus ();                                                                                                                             //Check status of in app purchase (true/false if player has purchased it)
  29.             totTime = Time.timeSinceLevelLoad + secTime;
  30.         }
  31.         if(cube != null)
  32.         {
  33.             if(!greenCubeIAPOwned)
  34.             {
  35.                 cube.transform.GetComponent<Renderer>().material.color = Color.red;                                                                     // if player has purchased item, turn the cube green
  36.             }
  37.  
  38.             if(greenCubeIAPOwned)
  39.             {
  40.                 cube.transform.GetComponent<Renderer>().material.color = Color.green;                                                           // if player has not purchased item (or hasnt restored previous purchases) turn the cube red
  41.             }
  42.         }
  43.     }
  44.  
  45.     void CheckIAP_PurchaseStatus()
  46.     {
  47.             if( Shop.HasProductBeenBought("green") == true ) {
  48.             greenCubeIAPOwned = true;
  49.            
  50.         } else {
  51.             Debug.Log("Ignored showing greencube since the user has not bought the greencube purchase");
  52.         }
  53.        
  54.  
  55.     }
  56.  
  57.         //GUI ELEMENTS
  58.         void OnGUI() {
  59.             //Button To PURCHASE ITEM
  60.             if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.4f, 150,150),"Make green?"))
  61.             {
  62.                
  63.                 Shop.BuyProduct( "green");
  64.            
  65.    
  66.             }
  67.             //Button to RESTORE PURCHASES
  68.             if (GUI.Button(new Rect(Screen.width * 0.2f, Screen.height * 0.8f, 150,150),"Restore\nPurchases")) {
  69.                 Shop.RestorePurchases( ( bool ok ) =>  {
  70.                     Debug.Log("RestorePurchases");
  71.                 }, (string productIdRestored)=>{
  72.                     Debug.Log("Just restored product: " + productIdRestored );
  73.                 } );
  74.  
  75.             }
  76.  
  77.             //Button to RESTART LEVEL (ensure it doesnt crash)
  78.             if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.8f, 150,150),"Restart"))  
  79.             {
  80.                 Application.LoadLevel (Application.loadedLevel);
  81.             }
  82.         }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement