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

1.保存库存物品

创建SerializbleDictionary脚本:

System.Serializable
public class SerializbleDictionary<Tkey, TValue> : Dictionary<Tkey, TValue>, ISerializationCallbackReceiver//手动实现字典序列化
{
SerializeField private List<Tkey> keys = new List<Tkey>();
SerializeField private List<TValue> values = new List<TValue>();
public void OnBeforeSerialize()
{

keys.Clear();
values.Clear();

foreach (KeyValuePair<Tkey, TValue> 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(keysi, valuesi);
}
}

}

GameData脚本:

public SerializbleDictionary<string, int> Inventory;//使用字典序列

public GameData()
{
this.currency = 0;

Inventory =new SerializbleDictionary<string, int>();
}

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(startingItemi != null)
AddItem(startingItemi);
}
}

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(startingItemi != null)
AddItem(startingItemi);
}
}

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

相关推荐
woshihuanglaoshi25 分钟前
事务加持防超卖:ArkTS 在鸿蒙里玩转 TRANSACTION 出入库
学习·华为·harmonyos
世人万千丶33 分钟前
去重插入与超限淘汰:ArkTS 实现鸿蒙搜索历史的 LIMIT 艺术
运维·服务器·学习·华为·harmonyos·鸿蒙
2601_949950632 小时前
在线练题更方便,用练题簿打造属于自己的移动题库
学习·考研·刷题·小程序推荐
woshihuanglaoshi2 小时前
十二商品二十流水:鸿蒙进销存种子数据装满预警看板
学习·华为·harmonyos
MartinYeung53 小时前
[论文学习]ProAct:针对LLM越狱的主动防禦框架
网络·学习·安全
weixin_431600444 小时前
NestJS 入门(10):日志——为什么常用 Winston?
后端·学习·日志·nest.js·winston
何事误红尘4 小时前
ZYNQ学习ARM裸机笔记():VITIS
学习
不懂的浪漫5 小时前
吴恩达《AI Engineering Skills Map》译读:四项核心能力与持续学习底座
人工智能·学习
蒸蒸yyyyzwd6 小时前
cpp选手秋招准备 学习笔记day7 webserver
笔记·学习
lifallen6 小时前
DeepSeek Harness:把 Agent 做成可替换的运行时插件树
人工智能·学习·ai·开源软件·ai编程