新建脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Save
{
public List<int> livingTargetPositions = new List<int>();
public List<int> livingMonsterTypes = new List<int>();
public int shootNum = 0;
public int score = 0;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager _instance;
//1.设置暂停
public bool isPaused = true;
//2.获得组件
public GameObject menuGo;
//3.设置数组
public GameObject[] targetGOs;
private void Awake()
{
_instance = this;
Pause();
}
private void Update()
{
//判断是否按下esc键 是并暂停游戏
if (Input.GetKeyDown(KeyCode.Escape))
{
Pause();
}
}
private void Pause()
{
isPaused = true;
menuGo.SetActive(true);
Time.timeScale = 0;
//隐藏鼠标图标
Cursor.visible = true;
}
private void UnPause()
{
isPaused = false;
menuGo.SetActive(false);
Time.timeScale = 1;
Cursor.visible=false;
}
//创建save对象并存储当前游戏状态
private Save CreateSaveGO()
{
//新建save对象
Save save = new Save();
//遍历所有的target
//如果其中有处于激活状态的怪物,就把该target的位置信息和激活状态的怪物的类型添加到List中
foreach(GameObject targetGO in targetGOs)
{
TargetManager targetManager = targetGO.GetComponent<TargetManager>();
if(targetManager.activeMonster != null) {
save.livingTargetPositions.Add(targetManager.targetPosition);
int type = targetManager.activeMonster.GetComponent<MonsterManager>().monsterType;
save.livingMonsterTypes.Add(type);
}
}
//把shootNum和score保存在Save对象中
save.shootNum = UIManager._instance.shootNum;
save.score = UIManager._instance.score;
//返回该Save对象
return save;
}
public void ContinueGame()
{
UnPause();
}
public void NewGame()
{
//3. 要更新9个位置的怪物 先设置数组
foreach(GameObject targetGO in targetGOs)
{
targetGO.GetComponent<TargetManager>().UpdateMonsters();
}
UIManager._instance.shootNum = 0;
UIManager._instance.score = 0;
UnPause();
}
public void QuitGame()
{
Application.Quit();
}
}