Per aspera ad astra.循此苦旅,以达天|

flyall

园龄:4年2个月粉丝:10关注:8

2023-03-05 23:57阅读: 34评论: 0推荐: 0

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 中国大陆许可协议进行许可。

posted @   flyall  阅读(34)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.