unity3D创建Save保存类19
新建脚本
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();
}
}
本文作者:flyall
本文链接:https://www.cnblogs.com/flyall/p/17182265.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步