最开始的想法是这样的:在GameManage中写一个public List<SkillData> skills=new List<SkillData>();与unity中的SkillData资源文件联系起来,在Save类中创建
public List<bool> IsUnlocked=new List<bool>();
public List<int> skillLevel=new List<int>();
来记录技能的解锁状态以及技能等级。
通过二进制来存储/加载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<EnemyController> enemies=new List<EnemyController>();
public List<SkillData> skills=new List<SkillData>();
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<float> positionX=new List<float>();
public List<float> positionY=new List<float>();
public List<bool> broken=new List<bool>();
//save SkillTree
public List<bool> IsUnlocked=new List<bool>();
public List<int> skillLevel=new List<int>();
}
把public List<SkillData> skills=new List<SkillData>();中的信息写入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<GameManage.instance.skills.Count;i++)
{
GameManage.instance.skills[i].isUnlocked=save.IsUnlocked[i];
GameManage.instance.skills[i].SkillLevel=save.skillLevel[i];
}
SkillManage.instance.updateSkillByLoad();
}
}
其中的updateSkillByLoad函数
public void updateSkillByLoad()
{
foreach(SkillData skill in GameManage.instance.skills)
{
if(skill.isUnlocked==true)
{
skillButtons[skill.SkillID].GetComponent<Image>().color=Color.white;
skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
skillButtons[skill.SkillID].transform.GetChild(1).GetComponent<Text>().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<Image>().color;
}
再在updateSkillByLoad函数中添加一个else文章来源:https://www.toymoban.com/news/detail-497080.html
public void updateSkillByLoad()
{
foreach(SkillData skill in GameManage.instance.skills)
{
if(skill.isUnlocked==true)
{
skillButtons[skill.SkillID].GetComponent<Image>().color=Color.white;
skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
skillButtons[skill.SkillID].transform.GetChild(1).GetComponent<Text>().text=
skill.SkillLevel.ToString();
}else{
skillButtons[skill.SkillID].GetComponent<Image>().color=origional;
skillButtons[skill.SkillID].transform.GetChild(1).gameObject.SetActive(false);
}
}
}
至此,技能树的UI功能已经大功告成了!文章来源地址https://www.toymoban.com/news/detail-497080.html
到了这里,关于游戏开发日志17(保存技能树信息)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!