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();
}