Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. [System.Serializable]
  6. public class UpDataShop
  7. {
  8.     public Text upName;
  9.     public Text upPrise;
  10.     public int Prise;
  11.     public int upModule;
  12.     public int prisePlus;
  13.     public Button But;
  14. }
  15. public class GameScript : MonoBehaviour {
  16.     [Header("Стандарт")]
  17.     int score = 0;
  18.     int module = 0;
  19.     int workerCount = 0;
  20.     int workersLvl = 1;
  21.     public Text scoreText;
  22.     [Header("Магазин"), Space(10)]
  23.     public UpDataShop[] Shop;
  24.     public GameObject shopPanel;
  25.  
  26.     private Save SaveGame = new Save();
  27.     void Awake()
  28.     {
  29.         if (PlayerPrefs.HasKey("Saves"))
  30.         {
  31.           SaveGame = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("Saves"));
  32.              score = SaveGame.scores;
  33.              module = SaveGame.modules;
  34.              workerCount =  SaveGame.workerCounts;
  35.              workersLvl = SaveGame.workersLvls;
  36.             for (int i = 0; i < Shop.Length; i++)
  37.             {
  38.             Shop[i].Prise = SaveGame.Prises[i];
  39.              Shop[i].upModule = SaveGame.upModules[i];
  40.                Shop[i].prisePlus = SaveGame.prisePluss[i];
  41.             }
  42.             DateTime dt = new DateTime(SaveGame.Date[0]);
  43.             TimeSpan ts = DateTime.Now - dt;
  44.  
  45.         }
  46.        
  47.            
  48.  
  49.     }
  50.     IEnumerator PlusPerSecond()
  51.     {
  52.         while (true)
  53.         {
  54.             score += (workerCount * workersLvl);
  55.             yield return new WaitForSeconds(1);
  56.         }
  57.     }
  58.     public void OnShopButtonCklick(int id)
  59.     {
  60.         if (id == -1)
  61.             shopPanel.SetActive(!shopPanel.activeSelf);
  62.         if (id == 0)
  63.         {
  64.             if (score >= Shop[id].Prise)
  65.             {
  66.                 score -= Shop[id].Prise;
  67.                 module += Shop[id].upModule;
  68.                 Shop[id].Prise *= Shop[id].prisePlus;
  69.             }
  70.         }
  71.         if (id == 1)
  72.         {
  73.             if (score >= Shop[id].Prise)
  74.             {
  75.                 score -= Shop[id].Prise;
  76.                 workerCount += Shop[id].upModule;
  77.                 Shop[id].Prise *= Shop[id].prisePlus;
  78.             }
  79.         }
  80.         if (id == 2)
  81.         {
  82.             if (score >= Shop[id].Prise)
  83.             {
  84.                 score -= Shop[id].Prise;
  85.                 workersLvl += Shop[id].upModule;
  86.                 Shop[id].Prise *= Shop[id].prisePlus;
  87.             }
  88.         }
  89.  
  90.     }
  91.     public void OnClick(int id)
  92.     {
  93.         if (id == 0)
  94.             score += (1 + module);
  95.     }
  96.     void Update()
  97.     {
  98.         scoreText.text = "Score: " + score.ToString() + '$';
  99.         for (int a = 0; a < Shop.Length; a++)
  100.         {
  101.             Shop[a].upPrise.text = Shop[a].Prise.ToString() + '$';
  102.             if (Shop[a].Prise > score)
  103.             {
  104.                 Shop[a].But.interactable = false;
  105.             }
  106.             else
  107.             {
  108.                 Shop[a].But.interactable = true;
  109.             }
  110.         }
  111.     }
  112.     void Start()
  113.     {
  114.         StartCoroutine(PlusPerSecond());
  115.     }
  116. #if UNITY_ANDROID && !UNITY_EDITOR
  117.     void OnApplicationPaused()
  118.     {
  119.         SaveGame.Prises = new int[Shop.Length];
  120.         SaveGame.upModules = new int[Shop.Length];
  121.         SaveGame.prisePluss = new int[Shop.Length];
  122.         for (int i = 0; i < Shop.Length; i++)
  123.         {
  124.             SaveGame.Prises[i] = Shop[i].Prise;
  125.             SaveGame.upModules[i] = Shop[i].upModule;
  126.             SaveGame.prisePluss[i] = Shop[i].prisePlus;
  127.         }
  128.         SaveGame.scores = score;
  129.         SaveGame.modules = module;
  130.         SaveGame.workerCounts = workerCount;
  131.         SaveGame.workersLvls = workersLvl;
  132.         PlayerPrefs.SetString("Saves", JsonUtility.ToJson(SaveGame));
  133.     }
  134. #else
  135.     void OnApplicationQuit()
  136.         {
  137.         SaveGame.Prises = new int[Shop.Length];
  138.         SaveGame.upModules = new int[Shop.Length];
  139.         SaveGame.prisePluss = new int[Shop.Length];
  140.         for (int i = 0; i < Shop.Length; i++)
  141.         {
  142.             SaveGame.Prises[i] = Shop[i].Prise;
  143.             SaveGame.upModules[i] = Shop[i].upModule;
  144.             SaveGame.prisePluss[i] = Shop[i].prisePlus;
  145.         }
  146.         SaveGame.scores = score;
  147.         SaveGame.modules = module;
  148.         SaveGame.workerCounts = workerCount;
  149.         SaveGame.workersLvls = workersLvl;
  150.         SaveGame.Date[0] = DateTime.Now.Second;
  151.         PlayerPrefs.SetString("Saves", JsonUtility.ToJson(SaveGame));
  152.         }
  153. #endif
  154. }
  155. [System.Serializable]
  156. public class Save
  157. {
  158.     public int scores;
  159.     public int modules;
  160.     public int workerCounts;
  161.     public int workersLvls;
  162.  
  163.     public int[] Prises;
  164.     public int[] upModules;
  165.     public int[] prisePluss;
  166.  
  167.     public int[] Date;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement