当前位置: 首页 > news >正文

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

}

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

http://www.xdnf.cn/news/1372735.html

相关文章:

  • 数据结构的线性表 之 链表
  • 深度学习——神经网络(PyTorch 实现 MNIST 手写数字识别案例)
  • 2026 届最新大数据专业毕设选题推荐,毕业设计题目汇总
  • typescript 中的访问修饰符
  • 工业数据消费迎来“抖音式”革命:TDengine IDMP 让数据自己开口说话
  • 利用3台机器搭建Kubernetes集群
  • 工业大模型五层架构全景解析:从算力底座到场景落地的完整链路
  • 《分布式任务调度中“任务重复执行”的隐性诱因与根治方案》
  • 算法练习-合并两个有序数组
  • Java大厂面试全真模拟:从Spring Boot到微服务架构实战
  • HTML应用指南:利用GET请求获取中国银行人民币存款利率数据
  • 【系统架构设计(二)】系统工程与信息系统基础中:信息系统基础
  • 数据结构青铜到王者第四话---LinkedList与链表(1)
  • [Mysql数据库] 知识点总结3
  • 深度学习:卷积神经网络(CNN)
  • Docker中如何记录非交互式连接ssh用户操作的所有命令记录?
  • QT(QTableWidget)
  • 机器学习:前篇
  • Linux系统的网络管理(二)
  • SELinux相关介绍
  • 质押、ETF、财库三箭齐发:以太坊价值逻辑的重构与演进
  • [灵动微电子 霍尔FOC MM32BIN560C]从引脚到应用
  • Ubuntu操作系统下使用mysql、mongodb、redis
  • 系统架构设计师-【2025上半年论文题目分享】
  • 探寻跨语言统一真理及其对NLP的未来启示
  • Agent实战教程:LangGraph关于智能体的架构模式与核心概念
  • 知行——同为科技24周年庆典
  • 【软件测试面试】全网最全,自动化测试面试题总结大全(付答案)
  • 二维费用背包 分组背包
  • Git命令