游戏开发日志17(保存技能树信息)

最开始的想法是这样的:在GameManage中写一个public List skills=new List();与unity中的SkillData资源文件联系起来,在Save类中创建

 public List IsUnlocked=new List();

 public List skillLevel=new List();

来记录技能的解锁状态以及技能等级。

通过二进制来存储/加载Save类,

以及通过Playerprefs来记录技能点数,实现如下:

首先记录技能点数:

 public void SaveByPlayerprefs()
    {
        PlayerPrefs.SetInt("projectile",projectile.itemHeld);

        PlayerPrefs.SetFloat("PlayerPosX",rubby.transform.position.x);
        PlayerPrefs.SetFloat("PlayerPosY",rubby.transform.position.y);
        //save ruby's hp infomation
        PlayerPrefs.SetFloat("hp",rubby.CurrentHealth);
        //save SkillTree
        //save Points
        PlayerPrefs.SetInt("Points",SkillManage.instance.skillPoint);
    }
 public void LoadByPlayerprefs()
    {
        projectile.itemHeld=PlayerPrefs.GetInt("projectile");
        InventoryManage.RefreshItem();

        position.x=PlayerPrefs.GetFloat("PlayerPosX");
        position.y=PlayerPrefs.GetFloat("PlayerPosY");
        rubby.transform.position=new Vector2(position.x,position.y);
        rubby.CurrentHealth=PlayerPrefs.GetFloat("hp");
        UIHealth.instance.SetValue( rubby.CurrentHealth/ rubby.MaxHealth);
        //Read Points
        SkillManage.instance.skillPoint=PlayerPrefs.GetInt("Points");
        SkillManage.instance.updatePointUI();
    }

记录技能信息:

using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManage : MonoBehaviour
{
    public static GameManage instance;
    public bool IsPaused;

    public List enemies=new List(); 
    public List skills=new List();
   

    private void Awake() {
        if(instance==null)
        { 
            instance=this;
        }else{
            if(instance!=this)
            {
                Destroy(gameObject);
            }
        }
        DontDestroyOnLoad(gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Save 
{
    //save enemies position and hp information
    public List positionX=new List();
    public List positionY=new List();
    public List broken=new List();
    //save SkillTree
    public List IsUnlocked=new List();
    public List skillLevel=new List();

}

把public List skills=new List();中的信息写入Save类

 private Save createSaveGameObject()
    {
        Save save=new Save();
        foreach(EnemyController enemy in GameManage.instance.enemies)
        {
            save.broken.Add(enemy.broken);
            save.positionX.Add(enemy.PositionX);
            save.positionY.Add(enemy.PositionY);
        }
        foreach(SkillData skill in GameManage.instance.skills)
        {
            save.IsUnlocked.Add(skill.isUnlocked);
            save.skillLevel.Add(skill.SkillLevel);
        }

        
        return save;
    }

将Save类以二进制类型存储

 private void SaveBySerialization()
    {
        Save save=createSaveGameObject();
        BinaryFormatter bf=new BinaryFormatter();
        FileStream fileStream=File.Create(@"F:\unity games\learning\My project\Assets\scripts\LoadGame\Data.text");
        bf.Serialize(fileStream,save); //将save以二进制存入fileStream
        fileStream.Close();
    }

读取Save类里的信息至skills

private void LoadByDeSerialization()
    {
        
            //load SkillTree
            for(int i=0;i

其中的updateSkillByLoad函数

 public void updateSkillByLoad()
    {
        foreach(SkillData skill in GameManage.instance.skills)
        {
            if(skill.isUnlocked==true)
            {
                skillButtons[skill.SkillID].GetComponent().color=Color.white;
                skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
                skillButtons[skill.SkillID].transform.GetChild(1).GetComponent().text=
                skill.SkillLevel.ToString();
            }
        }
    }

经测试,通过Save/Load按钮,可以正确实现技能树信息存储了,但是会有问题,当保存后继续加点其它技能,再load后,保存后继续加点的其它技能依然显示,经检查数据保存是没有问题的,只是忘了把之后技能的颜色返回原本的灰色,所有我先在Awake中去记录最初的阴影颜色

 private Color origional;
    private void Awake() {
        if(instance==null)
        {
            instance=this;
        }
        else{
            if(instance!=this)
            {
                Destroy(gameObject);
            }
        }
        DontDestroyOnLoad(gameObject);

        origional=skillButtons[0].GetComponent().color;
    }

再在updateSkillByLoad函数中添加一个else

public void updateSkillByLoad()
    {
        foreach(SkillData skill in GameManage.instance.skills)
        {
            if(skill.isUnlocked==true)
            {
                skillButtons[skill.SkillID].GetComponent().color=Color.white;
                skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
                skillButtons[skill.SkillID].transform.GetChild(1).GetComponent().text=
                skill.SkillLevel.ToString();
            }else{
                skillButtons[skill.SkillID].GetComponent().color=origional;
                skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(false);
                }
        }
    }

至此,技能树的UI功能已经大功告成了!

你可能感兴趣的:(unity数据储存,游戏开发日志,unity)