学习游戏制作记录(保存装备物品技能树和删除存档文件)8.26

1.保存库存物品

创建SerializbleDictionary脚本:

**[System.Serializable]
public class SerializbleDictionary<Tkey, TValue> : Dictionary<Tkey, TValue>, ISerializationCallbackReceiver//手动实现字典序列化
{

SerializeField\] private List\ keys = new List\(); \[SerializeField\] private List\ values = new List\(); public void OnBeforeSerialize() {** **keys.Clear(); values.Clear();** **foreach (KeyValuePair\ pairs in this) { keys.Add(pairs.Key); values.Add(pairs.Value); } }** **public void OnAfterDeserialize() { this.Clear();** **if (keys.Count != values.Count) { Debug.Log("Not Equie"); }** **for (int i = 0; i \< keys.Count; i++) { this.Add(keys\[i\], values\[i\]); } }** **}** ### GameData脚本: **public SerializbleDictionary\ Inventory;//使用字典序列** **public GameData() { this.currency = 0;** **Inventory =new SerializbleDictionary\(); }** ### Inventory脚本: **\[Header("Data Base")

private List<InventoryItem> LoadedItems =new List<InventoryItem>();//待加载的物品**

private void AddStartingItem()
{
if(LoadedItems.Count>0)//初始化物品,是否要加载
{
foreach(InventoryItem item in LoadedItems)
{
for(int i = 0;i<item.stackSize;i++)
{
AddItem(item.data);
}
}

return;
}

for (int i = 0; i < startingItem.Count; i++)
{
if(startingItem[i] != null)
AddItem(startingItem[i]);
}
}

public void LoadDate(GameData gameData)//加载函数
{
foreach(KeyValuePair<string,int> pair in gameData.Inventory)
{
foreach(var item in GetItemDataBase())在保存的序列里搜索
{
if(item!=null&&item.ItemID==pair.Key)
{
InventoryItem itemToLoad =new InventoryItem(item);

itemToLoad.stackSize = pair.Value;

LoadedItems.Add(itemToLoad);//添加
}
}
}
}

public void SaveDate(ref GameData gameData)
{
gameData.Inventory.Clear();

foreach (KeyValuePair<ItemData, InventoryItem> pair in inventoryItemsDictionary)
{
gameData.Inventory.Add(pair.Key.ItemID, pair.Value.stackSize);//添加到字典序列
}
}

private List<ItemData> GetItemDataBase()//获取保存的序列
{
List<ItemData> itemDatas = new List<ItemData>();
string[] assestsName = AssetDatabase.FindAssets("", new[] { "Assets/Data/Equipment" });

foreach(string SQName in assestsName)
{
var SQpath =AssetDatabase.GUIDToAssetPath(SQName);
var itemData= AssetDatabase.LoadAssetAtPath<ItemData>(SQpath);

itemDatas.Add(itemData);
}

return itemDatas;
}

2.保存材料和已经装备的物品

GameData脚本:

public List<string> equipmentId;//已装备物品的id

Inventory脚本:

public List<ItemData_Equipment> loadedEquipments;//已装备物品的列表

private void AddStartingItem()
{
foreach(ItemData_Equipment item in loadedEquipments)//初始化已经装备的物品
{
EquipItem(item);
}

if(LoadedItems.Count>0)
{
foreach(InventoryItem item in LoadedItems)
{
for(int i = 0;i<item.stackSize;i++)
{
AddItem(item.data);
}
}

return;
}

for (int i = 0; i < startingItem.Count; i++)
{
if(startingItem[i] != null)
AddItem(startingItem[i]);
}
}

public void SaveDate(ref GameData gameData)
{
gameData.Inventory.Clear();
gameData.equipmentId.Clear();

foreach (KeyValuePair<ItemData, InventoryItem> pair in inventoryItemsDictionary)
{
gameData.Inventory.Add(pair.Key.ItemID, pair.Value.stackSize);
}

foreach (KeyValuePair<ItemData,InventoryItem>pair in stashItemsDictionary)//保存材料
{
gameData.Inventory.Add(pair.Key.ItemID,pair.Value.stackSize);
}

foreach(KeyValuePair<ItemData_Equipment,InventoryItem> pair in equipmentDictionary)//保存装备的物品
{
gameData.equipmentId.Add(pair.Key.ItemID);
}
}

public void LoadDate(GameData gameData)
{
foreach(KeyValuePair<string,int> pair in gameData.Inventory)
{
foreach(var item in GetItemDataBase())
{
if(item!=null&&item.ItemID==pair.Key)
{
InventoryItem itemToLoad =new InventoryItem(item);

itemToLoad.stackSize = pair.Value;

LoadedItems.Add(itemToLoad);
}
}
}

foreach(string loadedItemID in gameData.equipmentId)//将已保存的装备添加到列表中
{
foreach(var item in GetItemDataBase())
{
if(item!=null && item.ItemID==loadedItemID)
{
loadedEquipments.Add(item as ItemData_Equipment);
}
}
}
}

3.保存技能树

UI_SkillTreeSlot脚本:

public class UI_SkillTreeSlot : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler,ISaveManager实现ISaveManager接口

public void LoadDate(GameData gameData)
{
if(gameData.skilltree.TryGetValue(skillName,out bool value))//加载bool
unlocked = value;
}

public void SaveDate(ref GameData gameData)
{
if(gameData.skilltree.TryGetValue(skillName,out bool value))
{
gameData.skilltree.Remove(skillName);
gameData.skilltree.Add(skillName, unlocked);
}
else
{
gameData.skilltree.Add(skillName, unlocked);
}
}

Skill脚本:

protected virtual void Start()
{
player = PlayerManage.instance.player;

CheckUnlocked();
}

protected virtual void CheckUnlocked()//每个技能单独重写
{

}

以Blackhole_Skill脚本为例:

protected override void CheckUnlocked()
{

UnlockBlackHole();///调用解锁函数
}

4.删除保存的文档

FileDataHandler 脚本:

public void Delete()
{
string fullPath = Path.Combine(dataDirPath, FileName);

if(File.Exists(fullPath))//检测是否存在
{
File.Delete(fullPath);
}
}

SaveManager脚本:

[ContextMenu("Delete save data")]//右击脚本可以看到选项直接调用函数
private void DeletSavedData()
{
dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);
dataHandler.Delete();
}

相关推荐
2006yu15 分钟前
从零开始学习单片机13
单片机·嵌入式硬件·学习
风和日丽 随波逐流37 分钟前
java18学习笔记
笔记·学习·java18
幽络源小助理1 小时前
如何从零开始学习黑客技术?网络安全入门指南
网络·学习·web安全
2006yu2 小时前
从零开始学习单片机12
学习
爱炸薯条的小朋友4 小时前
C#由Dictionary不正确释放造成的内存泄漏问题与GC代系
开发语言·opencv·c#
神齐的小马4 小时前
计算机网络学习笔记
笔记·学习·计算机网络
小郝 小郝5 小时前
【51单片机】萌新持续学习中《矩阵 密码锁 点阵屏》
嵌入式硬件·学习·51单片机
weixin_456904275 小时前
C# .NET Framework 4.0 网络编程完全指南
网络·c#·.net
胡萝卜3.05 小时前
数据结构初阶:详解单链表(一)
数据结构·笔记·学习·单链表