using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class Game : MonoBehaviour
{
public Text scoreText;
private int score = 0;
private int bonus = 1;
private int workersCount = 0;
private int workersBonus = 1;
public Button[] shopBttns;
public float[] bonusTime;
[Header("Магазин")]
public int[] shopCosts;
public int[] shopBonuses;
public GameObject shopPan;
private void Start()
{
LoadGame();
StartCoroutine(BonusPerSec());
StartCoroutine(AutoSave());
}
private void Update()
{
scoreText.text = score + " печенек";
}
private void SaveGame()
{
PlayerPrefs.SetInt("Score", score);
PlayerPrefs.SetInt("Bonus", bonus);
PlayerPrefs.SetInt("WorkersCount", workersCount);
PlayerPrefs.SetInt("WorkersBonus", workersBonus);
PlayerPrefs.Save();
}
private void LoadGame()
{
score = PlayerPrefs.GetInt("Score", 0);
bonus = PlayerPrefs.GetInt("Bonus", 1);
workersCount = PlayerPrefs.GetInt("WorkersCount", 0);
workersBonus = PlayerPrefs.GetInt("WorkersBonus", 1);
}
IEnumerator AutoSave()
{
while (true)
{
yield return new WaitForSeconds(30);
SaveGame();
}
}
public void shopPan_ShowAndHide()
{
shopPan.SetActive(!shopPan.activeSelf);
}
public void shopBttn_addBonus(int index)
{
if (score >= shopCosts[index])
{
bonus *= shopBonuses[index];
score -= shopCosts[index];
SaveGame();
}
else
{
Debug.Log("no");
}
}
public void HireFWorker(int index)
{
if (score >= shopCosts[index])
{
workersCount++;
score -= shopCosts[index];
SaveGame();
}
else
{
Debug.Log("no");
}
}
public void startBonusTimer(int index)
{
StartCoroutine(bonusTimer(bonusTime[index], index));
}
IEnumerator BonusPerSec()
{
while (true)
{
score += (workersCount * workersBonus);
SaveGame();
yield return new WaitForSeconds(1);
}
}
IEnumerator bonusTimer(float time, int index)
{
yield return null;
}
public void Click()
{
score += bonus;
SaveGame();
}
}
И тут заткнулся на сохранениях. Все говорят про PlayerPrefs, но я всё равно не бум-бум. Так что кто знает - будьте добры помочь. Либо посоветуйте какие нибудь туториалы.
Вот код:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class Game : MonoBehaviour
{
public Text scoreText;
private int score = 0;
private int bonus = 1;
private int workersCount,workersBonus = 1;
public Button[] shopBttns;
public float[] bonusTime;
[Header("Магазин")]
public int[] shopCosts;
public int[] shopBonuses;
public GameObject shopPan;
private void Start()
{
StartCoroutine(BonusPerSec());
}
private void Update()
{
scoreText.text = score + " печенек";
}
public void shopPan_ShowAndHide()
{
shopPan.SetActive(!shopPan.activeSelf);
}
public void shopBttn_addBonus(int index)
{
if (score >= shopCosts[index])
{
bonus *= shopBonuses[index];
score -= shopCosts[index];
}
else
{
Debug.Log("no");
}
}
public void HireFWorker(int index)
{
if (score >= shopCosts[index])
{
workersCount++;
score -= shopCosts[index];
}
else
{
Debug.Log("no");
}
}
public void startBonusTimer(int index)
{
StartCoroutine(bonusTimer(bonusTime[index], index));
}
IEnumerator BonusPerSec()
{
while (true)
{
score += (workersCount * workersBonus);
yield return new WaitForSeconds(1);
}
}
IEnumerator bonusTimer(float time, int index)
{
shopBttns[index].interactable = false;
if (index == 0)
{
workersBonus *= 2;
yield return new WaitForSeconds(time);
workersBonus /= 2;
}
shopBttns[index].interactable = true;
}
public void OnClick()
{
score += bonus;
scoreText.text = score + " печенек";
}
}