当前位置: 首页 > 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.xdnf.cn/news/1361179.html

相关文章:

  • 基于RISC-V架构的国产MCU在eVTOL领域的应用研究与挑战分析
  • 【Ollama】本地OCR
  • 波兰密码破译机bomba:二战密码战的隐形功臣
  • Shell 循环实战:while 与 until 的趣味编程之旅
  • 3.4 磁盘存储器 (答案见原书 P194)
  • 【重学MySQL】八十八、8.0版本核心新特性全解析
  • Unity的Cursor.lockState
  • DeepSeek对采用nginx实现透传以解决OpenShift 4.x 私有数据中心和公有云混合部署一套集群的解答
  • 【SBP】Unity 打包构建管线原理解析于对比
  • 联想win11笔记本音频失效,显示差号(x)
  • 半年网络安全转型学习计划表(每天3小时)
  • 从成本中心到价值创造者:网络安全运维的实施框架与价值流转
  • VMware centos磁盘容量扩容教程
  • Windows 系统下 Android SDK 配置教程
  • 使用 Frida 运行时检测 Android 应用的真实权限状态 (App Ops)
  • 强逆光干扰漏检率↓78%!陌讯多模态融合算法在光伏巡检的实战优化
  • Java全栈开发面试实战:从基础到高并发场景的深度解析
  • Python性能优化实战(二):让循环跑得比博尔特还快
  • 27.编程思想
  • 【golang长途旅行第30站】channel管道------解决线程竞争的好手
  • Teams Bot机器人实时语音识别的多引擎的处理
  • TCP--执行Linux命令(虚拟xshell)
  • 数据建模怎么做?一文讲清数据建模全流程
  • 一、基因组选择(GS)与基因组预测(GP)
  • 网络安全转型书籍清单
  • 【Java开发日记】我们来讲一讲 Channel 和 FileChannel
  • 深度学习之第一课深度学习的入门
  • VirtualBox安装openEuler24.03
  • daily notes[5]
  • 前端 vs 后端请求:核心差异与实战对比