学习游戏制作记录(保存装备物品技能树和删除存档文件)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();
}

相关推荐
wdfk_prog8 分钟前
[Linux]学习笔记系列 -- lib/timerqueue.c Timer Queue Management 高精度定时器的有序数据结构
linux·c语言·数据结构·笔记·单片机·学习·安全
懒人Ethan29 分钟前
解决一个C# 在Framework 4.5反序列化的问题
java·前端·c#
wdfk_prog1 小时前
构建基于Hexo、Butterfly、GitHub与Cloudflare的高性能个人博客
笔记·学习·github·hexo·blog
shixian10304111 小时前
Django 学习日志
数据库·学习·sqlite
mysolisoft2 小时前
Avalonia+ReactiveUI实现记录自动更新
c#·avalonia·reactiveui·sourcegenerator
丰锋ff3 小时前
2013 年真题配套词汇单词笔记(考研真相)
笔记·学习·考研
小小程序媛(*^▽^*)3 小时前
第十二届全国社会媒体处理大会笔记
人工智能·笔记·学习·ai
心疼你的一切3 小时前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
韩立学长4 小时前
【开题答辩实录分享】以《C#大型超市商品上架调配管理系统的设计与实现》为例进行答辩实录分享
开发语言·c#
毕设源码-邱学长4 小时前
【开题答辩全过程】以 爱学习教育网站为例,包含答辩的问题和答案
学习