1. 程式人生 > >2D獵寶行動(類掃雷小遊戲)DAY10

2D獵寶行動(類掃雷小遊戲)DAY10

1.資源池的使用

將各處的生成特效和刪除特效進行修改。如:

//生成特效
PoolManager._instance.GetInstance(EffectType.GoldEffect, transform).name = "GoldEffect";
//刪除特效
PoolManager._instance.StoreInstance(EffectType.GoldEffect, goldEffect.gameObject);

對自動銷燬的方法進行修改

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AutoStore : MonoBehaviour {

    public EffectType effectType;
    public float delay;
    public bool needStore = true;

    private void Start()
    {
        if (needStore)
        {
            Invoke("Delay", delay);
        }
        else
        {
            Destroy(gameObject, delay);
        }
    }

    private void OnEnable()
    {
        Invoke("Delay", delay);
    }

    private void Delay()
    {
        PoolManager._instance.StoreInstance(effectType, gameObject);
    }
}

2.設計開始場景

3.設計資料管理器以管理無盡關卡

寫DataManager,用來進行資料資訊的管理

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataManager : MonoBehaviour {

    public static DataManager _instance;

    public int w;
    public int h;

    public int lv;
    public int hp;
    public int armor;
    public int key;
    public int hoe;
    public int tnt;
    public int map;
    public int gold;

    private void Awake()
    {
        _instance = this;
        DontDestroyOnLoad(gameObject);
        UnityEngine.SceneManagement.SceneManager.LoadScene(1);
    }

    public void LoadData()
    {
        lv = PlayerPrefs.GetInt("lv", 1);
        hp = PlayerPrefs.GetInt("hp", 3);
        armor = PlayerPrefs.GetInt("armor", 0);
        key = PlayerPrefs.GetInt("key", 0);
        hoe = PlayerPrefs.GetInt("hoe", 0);
        tnt = PlayerPrefs.GetInt("tnt", 0);
        map = PlayerPrefs.GetInt("map", 0);
        gold = PlayerPrefs.GetInt("gold", 0);
        w = 20 + (lv * 3);
        h = Random.Range(9, 12);
    }

    public void ResetData()
    {
        SaveData(1, 3, 0, 0, 0, 0, 0, 0);
    }

    public void SaveData(int lv,int hp,int armor,int key,int hoe,int tnt,int map,int gold)
    {
        this.lv = lv;
        this.hp = hp;
        this.armor = armor;
        this.key = key;
        this.hoe = hoe;
        this.tnt = tnt;
        this.map = map;
        this.gold = gold;
        PlayerPrefs.SetInt("lv", lv);
        PlayerPrefs.SetInt("hp", hp);
        PlayerPrefs.SetInt("armor", armor);
        PlayerPrefs.SetInt("key", key);
        PlayerPrefs.SetInt("hoe", hoe);
        PlayerPrefs.SetInt("tnt", tnt);
        PlayerPrefs.SetInt("map", map);
        PlayerPrefs.SetInt("gold", gold);
    }
}

4.關卡資料的儲存與讀取

在GameManager中新增儲存與讀取的方法

 private void LoadData()
    {
        DataManager._instance.LoadData();
        w = DataManager._instance.w;
        h = DataManager._instance.h;
        lv = DataManager._instance.lv;
        hp = DataManager._instance.hp;
        armor = DataManager._instance.armor;
        key = DataManager._instance.key;
        hoe = DataManager._instance.hoe;
        tnt = DataManager._instance.tnt;
        map = DataManager._instance.map;
        gold = DataManager._instance.gold;
    }

    public void OnLevelPass()
    {
        if (hp < 3)
        {
            hp = 3;
        }
        _instance.lv++;
        DataManager._instance.SaveData(
            lv,
            hp,
            armor,
            key,
            hoe,
            tnt,
            map,
            gold
            );
        ani.SetBool("Pass", true);
    }

然後在出口出進行資訊儲存

    public override void OnPlayerStand()
    {
        GameManager._instance.OnLevelPass();
    }