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

学习制作记录(选项UI以及存档系统)8.24

1.制作选项的UI

两个滑块和两个按钮,在玩家头上显示血条的按钮需要绑定函数,用来激活玩家头上的血条对象

删除Button组件添加Toogle当被点击时调用血条setactive

2.修改提示工具的显示

创建UI_ToolTip脚本:

    [SerializeField] private float xLimit=960;
[SerializeField] private float yLimit=540;

    [SerializeField] private float xOffest=150;
[SerializeField] private float yOffest=150;

    [SerializeField] public int DefaultFontSize;


    protected virtual void AdjustToolPosition()//和之前调整位置的方法一致
{
Vector2 mousePosition = Input.mousePosition;

        float newxOffest = 0;
float newyOffest = 0;


        if (mousePosition.x > xLimit)
{
newxOffest = -xOffest;
}
else
newxOffest = xOffest;

        if (mousePosition.y > yLimit)
{
newyOffest = -yOffest;
}
else
{
newyOffest = yOffest;
}

       transform.position = new Vector2(mousePosition.x + newxOffest, mousePosition.y + newyOffest);
}


    protected virtual void AdjustFontSize(TextMeshProUGUI _text)//设置文本大小,防止过大
{
if(_text.text.Length>12)
{
_text.fontSize =_text.fontSize*.8f;
}
}

UI_SkillToolTip脚本:


public class UI_SkillToolTip : UI_ToolTip//继承

    [SerializeField] private TextMeshProUGUI skillCost;//设置价格的文本

    public void ShowSkillToolTip(string _description,string _skillName,string _skillCost)
{
skillName.text = _skillName;
skillDescription.text = _description;
skillCost.text = "花费灵魂: "+_skillCost;

        AdjustToolPosition();//设置

        AdjustFontSize(skillName);


        gameObject.SetActive(true);
}

    public void HideSkillToolTip()
{
skillName.fontSize = DefaultFontSize;//恢复默认文本
gameObject.SetActive(false);
}

UI_ItemTip脚本:

    public void ShowToolTip(ItemData_Equipment item)
{
if (item == null) return;

        itemStringName.text = item.ItemName;
itemTypeName.text = item.equipmentType.ToString();
itemDescription.text = item.GetDescription();

        AdjustFontSize(itemTypeName);//同样,物品提示框

        AdjustToolPosition();

        gameObject.SetActive(true);
}

ItemEffect脚本:

    [TextArea]
public string itemEffectDescription;//为每个独特效果添加描述

        for(int i=0;i<itemEffects.Length;i++)//描述显示
{
if (itemEffects[i].itemEffectDescription.Length>0)
{
sb.AppendLine();
sb.Append("独特效果:"+ itemEffects[i].itemEffectDescription);

                DescriptionLength++;
}
}

UI_StatToolTip脚本:

    public void ShowStatToolTip(string _text)
{
statDescription.text = _text;

        AdjustToolPosition();//调整状态提示框的位置

        gameObject.SetActive(true);
}

3.制作存储系统

创建GameData脚本:

[System.Serializable]
public class GameData
{
public int currency;//储存当前的游戏数据

    public GameData()
{
this.currency = 0;
}
}

创建ISaveManager脚本:

public interface ISaveManager //接口
{
void LoadDate(GameData gameData);

    void SaveDate(ref GameData gameData);
}

创建FileDataHandler脚本:

public class FileDataHandler 
{
private string dataDirPath = "";//路径

    private string FileName = "";//文件名称

    public FileDataHandler(string _dataDirPath, string _fileName)
{
dataDirPath = _dataDirPath;
FileName = _fileName;
}

    public void Save(GameData data)
{
string fullPath = Path.Combine(dataDirPath, FileName);//合并路径

        try
{
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));//创建目录

            string dataToStore = JsonUtility.ToJson(data);//转化数据为json

            using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(dataToStore);//写入数据
}
}

        
}
catch (Exception e)
{
Debug.LogError(e.Message);
}

    }

    public GameData Load()
{
string fullPath =Path.Combine(dataDirPath, FileName);

        GameData loadData = null;

        if(File.Exists(fullPath))//如果存在文件
{
string dataToload = " ";

            try
{

                using (FileStream stream = new FileStream(fullPath, FileMode.Open))
{
using(StreamReader reader = new StreamReader(stream))
{
dataToload = reader.ReadToEnd();//写入文本
}


                }

            }
catch (Exception e)
{
Debug.LogError(e.Message);
}

            loadData = JsonUtility.FromJson<GameData>(dataToload);反json化
}
return loadData;
}

创建SaveManager脚本:

    public static SaveManager instance;//单例化处理

    [SerializeField] private string fileName;

    private GameData gameData;//游戏数据
private List<ISaveManager> saveManagers ;//所有保存管理器
private FileDataHandler dataHandler;//数据处理器

    private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
}
}
void Start()
{
saveManagers = FindAllSaveManagers();//初始化
dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);//设置Window的默认文件位置来储存文件

        LoadData();
}

    public void NewGame()//初始化数据
{
gameData =new GameData();
}
public void LoadData()//加载
{
gameData = dataHandler.Load();

        if(this.gameData==null)
{

            NewGame();
}

        foreach (ISaveManager manager in saveManagers)//依次处理所有实现了接口的函数
{
manager.LoadDate(gameData);
}

       
}

    public void SaveData()
{
foreach (ISaveManager manager in saveManagers)//同理
{
manager.SaveDate(ref gameData);//引用可以修改
}


        dataHandler.Save(gameData);
}

    private void OnApplicationQuit()//退出播放模式自动保存
{
SaveData();
}

    private List<ISaveManager> FindAllSaveManagers()//获取所有实现接口的管理器
{
IEnumerable<ISaveManager> saveManagers =FindObjectsOfType<MonoBehaviour>().OfType<ISaveManager>();

        return new List<ISaveManager>(saveManagers);
}

PlayerManage脚本:


实现ISaveManager接口

    public void LoadDate(GameData gameData)//加载货币和保存货币
{
this.currency = gameData.currency;
}

    public void SaveDate(ref GameData gameData)
{
gameData.currency = this.currency;
}

http://www.dtcms.com/a/348605.html

相关文章:

  • KVM虚拟化
  • Vue3 setup代替了vue2的哪些功能
  • 分布式事务的两种解决方案
  • MYSQL(DDL)
  • 前端 vs 后端请求:核心差异与实战对比
  • Qt——网络通信(UDP/TCP/HTTP)
  • 【Unity开发】Unity核心学习(二)
  • PAT 1081 Rational Sum
  • 【机器学习】8 Logistic regression
  • Power BI切片器自定义顺序
  • 智能油脂润滑系统:给设备一份 “私人定制” 的保养方案
  • Linux 学习笔记 - 集群管理篇
  • 【大模型LLM学习】Data Agent学习笔记
  • C++算法学习专题:二分查找
  • Kubernetes部署Prometheus+Grafana 监控系统NFS存储方案
  • Socket some functions
  • 让机器人“想象”未来?VLN导航迎来“理解力”新升级
  • 每日算法刷题Day64:8.24:leetcode 堆6道题,用时2h30min
  • 解密 Spring Boot 自动配置:原理、流程与核心组件协同
  • 人形机器人——电子皮肤技术路线:压电式电子皮肤及一种超越现有电子皮肤NeuroDerm的设计
  • 深度学习:CUDA、PyTorch下载安装
  • Leetcode 3659. Partition Array Into K-Distinct Groups
  • sqlite创建数据库,创建表,插入数据,查询数据的C++ demo
  • 商密保护迷思:经营秘密到底需不需要鉴定?
  • 对称二叉树
  • 机械学习综合练习项目
  • jar包项目自启动设置ubuntu
  • [论文阅读] 软件工程 | GPS算法:用“路径摘要”当向导,软件模型检测从此告别“瞎找bug”
  • 服务器硬件电路设计之 SPI 问答(四):3 线 SPI、Dual SPI 与 Qual SPI 的奥秘
  • 春秋云镜 Hospital