GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public List<Bird> birds;
public List<Break> pigs;
private bool Win = false,Lose = false;
public Vector3 BirdsBornPos;
private GameObject LoseResult;
private GameObject WinResult;
private GameObject BackAppear;
public GameObject[] Stars;
private GameObject pausePanel;
private GameObject pauseButton;
public static GameManager _instance;
private int CurrentStars;
private int sum;
private GameObject LevelResult;
private Camera UIcamera;
private void Awake()
{
_instance = this;
if (birds.Count > 0) BirdsBornPos = birds[0].gameObject.transform.position;
LevelResult = GameObject.Find("LevelResult");
UIcamera = GameObject.Find("UICamera").GetComponent<Camera>();
BackAppear = LevelResult.transform.Find("BackAppear").gameObject;
WinResult = LevelResult.transform.Find("Win").gameObject;
LoseResult = LevelResult.transform.Find("Lose").gameObject;
BackAppear.transform.SetSiblingIndex(0);
pauseButton = GameObject.Find("Pause").transform.Find("pauseButton").gameObject;
pausePanel = GameObject.Find("Pause").transform.Find("pausePanel").gameObject;
Stars[0] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("left").gameObject;
Stars[1] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("middle").gameObject;
Stars[2] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("right").gameObject;
Canvas canvas = LevelResult.GetComponent<Canvas>();
canvas.renderMode= RenderMode.ScreenSpaceCamera;
canvas.worldCamera = UIcamera;
}
private void Start()
{
Initialize();
}
public void Initialize()//重新对小鸟们的属性控制
{
for(int i = 0; i < birds.Count; i++)
{
if (i == 0)
{
birds[i].gameObject.transform.position = BirdsBornPos;
birds[i].enabled = true;
birds[i].sp.enabled = true;
}
else
{
birds[i].enabled = false;
birds[i].sp.enabled = false;
}
}
}
public void JudgeLevelResult()
{
if (Win == false&&pigs.Count == 0)
{
Win = true;
WinResult.SetActive(true);
BackAppear.SetActive(true);
StartCoroutine("ShowStars");
}
if (Lose == false&&pigs.Count > 0 && birds.Count == 0)
{
Lose = true;
LoseResult.SetActive(true);
BackAppear.SetActive(true);
}
}
IEnumerator ShowStars()
{
for(CurrentStars = 0; CurrentStars < birds.Count&&CurrentStars<3; CurrentStars++)
{
yield return new WaitForSeconds(0.5f);
Stars[CurrentStars].SetActive(true);
}
SaveData();
}
private void SaveData()
{
if (CurrentStars > PlayerPrefs.GetInt(PlayerPrefs.GetString("CurrentTheme")+ PlayerPrefs.GetString("CurrentLevel")))
{
PlayerPrefs.SetInt(PlayerPrefs.GetString("CurrentTheme")+PlayerPrefs.GetString("CurrentLevel"), CurrentStars);
}
switch (PlayerPrefs.GetString("CurrentTheme"))
{
case "T1":
sum = 0;
for(int i = 1; i <= 12; i++)
{
sum += PlayerPrefs.GetInt("T1"+"level"+i);
}
break;
case "T2":
sum = 0;
for (int i = 1; i <= 12; i++)
{
sum += PlayerPrefs.GetInt("T1" + "level" + i);
}
for (int i = 1; i <= 8; i++)
{
sum += PlayerPrefs.GetInt("T2"+"level" + i);
}
break;
case "T3":
sum = 0;
for (int i = 1; i <= 12; i++)
{
sum += PlayerPrefs.GetInt("T1" + "level" + i);
}
for (int i = 1; i <= 8; i++)
{
sum += PlayerPrefs.GetInt("T2" + "level" + i);
}
for (int i = 1; i <= 1; i++)
{
sum += PlayerPrefs.GetInt("T3"+"level" + i);
}
break;
}
PlayerPrefs.SetInt("CollectedNum", sum);
}
public void Pause()
{
Time.timeScale