👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:unity数据存储
⭐PlayerPrefs⭐
🎶PlayerPrefs基本内容
-
API大全图解
🎶PalyerPrefs存储知识
-
windows平台存储路径
HKCU\Software[公司名称][产品名称] 项下的注册表中
公司和产品名称是 在“Project Settings”中设置的名称。
1.运行 regedit (win+R)
2/HKEY_CURRENT_USER
3/SOFTWARE
4.Unity
5.UnityEditor
6.公司名称
7.产品名称
-
Android平台存储路径
/data/data/包名/shared_prefs/pkg-name.xml
-
IOS平台存储路径
/Library/Preferences/[应用ID].plist
🎶PalyerPrefs知识小结
- 用palyerfabs存储多对象数据时的代码模块
1.list列表 List
2.存储方法 Save()
3.读取方法 Read()
🎶PalyerPrefs代码实践
>题目:现在有玩家信息类,有名字,年龄,攻击力,防御力等成员现在为其封装两个方法,—个用来存储数据,一个用来读取数据现在在装备信息类,装备类中有id,数量两个成员。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
class PalyerMess
{
private string name;
private int age;
private int atack;
private int defend;
/// <summary>
/// 存储数据
/// </summary>
public void Save()
{
PlayerPrefs.SetString("name","孙悟空");
PlayerPrefs.SetInt("age", 108);
PlayerPrefs.SetInt("atack", 999);
PlayerPrefs.SetInt("defend", 1000);
PlayerPrefs.Save();
}
public void Read()
{
string a = PlayerPrefs.GetString("name");
int b = PlayerPrefs.GetInt("age");
int c = PlayerPrefs.GetInt("atack");
int d = PlayerPrefs.GetInt("defend");
Debug.Log("玩家的姓名为"+a+ "\n年龄为"+b+"攻击力为"+c+"防御力为"+d);
}
}
public class _PalyerPrefs : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
PalyerMess boss = new PalyerMess();
boss.Save();
boss.Read();
}
// Update is called once per frame
void Update()
{
}
}
> 在题目一的基础上加上 装备信息的存储和读取
public class Play : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
PlayerMess boss = new PlayerMess("孙悟空", 18, 999, 666);
Equipment equipment1 = new Equipment("001", 1);
Equipment equipment2 = new Equipment("002", 3); ;
Equipment equipment3 = new Equipment("003", 4); ;
boss.AddStateToList(equipment1);
boss.AddStateToList(equipment2);
boss.AddStateToList(equipment3);
boss.Save();
boss.Read();
boss.SaveEquip();
boss.ReadEquip();
}
// Update is called once per frame
void Update()
{
}
}
/// <summary>
/// 装备信息类
/// </summary>
public class Equipment
{
private string id;
private int numb;
public string Id { get => id; }
public int Numb { get => numb; }
public Equipment(string v1, int v2)
{
this.id = v1;
this.numb = v2;
}
}
/// <summary>
/// 玩家信息类
/// </summary>
public class PlayerMess
{
private string name;
private int age;
private int atack;
private int defend;
public List<Equipment> equipMess; //存储装备类中的信息
public PlayerMess(string name, int age, int atack, int defend)
{
this.name = name;
this.age = age;
this.atack = atack;
this.defend = defend;
equipMess = new List<Equipment>();
}
public void AddStateToList(Equipment t)
{
equipMess.Add(t);
}
/// <summary>
/// 存储装备数据
/// </summary>
public void SaveEquip()
{
int i = 1;
foreach (Equipment item in equipMess)
{
string a = "Equip" + i + "ID";
string b = "Equip" + i + "Number";
PlayerPrefs.SetString(a, item.Id);
PlayerPrefs.SetInt(b, item.Numb);
i++;
}
}
/// <summary>
/// 存储玩家数据
/// </summary>
public void Save()
{
PlayerPrefs.SetString("name", name);
PlayerPrefs.SetInt("age", age);
PlayerPrefs.SetInt("atack", atack);
PlayerPrefs.SetInt("defend", defend);
PlayerPrefs.Save();
}
/// <summary>
/// 读取玩家数据·
/// </summary>
public void Read()
{
string a = PlayerPrefs.GetString("name");
int b = PlayerPrefs.GetInt("age");
int c = PlayerPrefs.GetInt("atack");
int d = PlayerPrefs.GetInt("defend");
Debug.Log("玩家的姓名为" + a);
Debug.Log("年龄为" + b);
Debug.Log("攻击力为" + c);
Debug.Log("防御力为" + d);
}
/// <summary>
/// 读取装备数量数据
/// </summary>
public void ReadEquip()
{
for (int i = 1; i <= 3; i++)
{
Debug.Log("Equip" + i + "Id为:" + PlayerPrefs.GetString("Equip" + i + "ID"));
Debug.Log("Equip" + i + "数量为:"+PlayerPrefs.GetInt("Equip" + i + "Number"));
}
}
}
>在题目的基础上实现多对象存储
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: _______________
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Play : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
PlayerMess boss = new PlayerMess("孙悟空", 18, 999, 666);
Equipment equipment1 = new Equipment("001", 1);
Equipment equipment2 = new Equipment("002", 3); ;
Equipment equipment3 = new Equipment("003", 4); ;
boss.AddStateToList(equipment1);
boss.AddStateToList(equipment2);
boss.AddStateToList(equipment3);
boss.Save("Boos1");
boss.Read();
boss.SaveEquip("Boss1");
boss.ReadEquip();
}
// Update is called once per frame
void Update()
{
}
}
/// <summary>
/// 装备信息类
/// </summary>
public class Equipment
{
private string id;
private int numb;
public string Id { get => id; }
public int Numb { get => numb; }
public Equipment(string v1, int v2)
{
this.id = v1;
this.numb = v2;
}
}
/// <summary>
/// 玩家信息类
/// </summary>
public class PlayerMess
{
private string name;
private int age;
private int atack;
private int defend;
private string NameFlag; //为实现多个对象存储的关键
public List<Equipment> equipMess; //存储装备类中的信息
public PlayerMess(string name, int age, int atack, int defend)
{
this.name = name;
this.age = age;
this.atack = atack;
this.defend = defend;
equipMess = new List<Equipment>();
}
public void AddStateToList(Equipment t)
{
equipMess.Add(t);
}
/// <summary>
/// 存储装备数据
/// </summary>
public void SaveEquip(string nameFlag)
{
NameFlag = nameFlag;
int i = 1;
foreach (Equipment item in equipMess)
{
string a = nameFlag+"_Equip" + i + "ID";
string b = nameFlag + "_Equip" + i + "Number";
PlayerPrefs.SetString(a, item.Id);
PlayerPrefs.SetInt(b, item.Numb);
i++;
}
}
/// <summary>
/// 存储玩家数据
/// </summary>
public void Save(string nameFlag)
{
NameFlag = nameFlag;
PlayerPrefs.SetString(nameFlag + "_name", name);
PlayerPrefs.SetInt(nameFlag+"_age", age);
PlayerPrefs.SetInt(nameFlag+"_atack", atack);
PlayerPrefs.SetInt(nameFlag+"_defend", defend);
PlayerPrefs.Save();
}
/// <summary>
/// 读取玩家数据·
/// </summary>
public void Read()
{
string a = PlayerPrefs.GetString(NameFlag+"_name");
int b = PlayerPrefs.GetInt(NameFlag + "_age");
int c = PlayerPrefs.GetInt(NameFlag + "_atack");
int d = PlayerPrefs.GetInt(NameFlag + "_defend");
Debug.Log("玩家的姓名为" + a);
Debug.Log("年龄为" + b);
Debug.Log("攻击力为" + c);
Debug.Log("防御力为" + d);
}
/// <summary>
/// 读取装备数量数据
/// </summary>
public void ReadEquip()
{
for (int i = 1; i <= 3; i++)
{
Debug.Log("Equip" + i + "Id为:" + PlayerPrefs.GetString(NameFlag+"_Equip" + i + "ID"));
Debug.Log("Equip" + i + "数量为:"+PlayerPrefs.GetInt(NameFlag+"_Equip" + i + "Number"));
}
}
}
>排行榜信息
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能:排行榜功能
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Chats : MonoBehaviour
{
public List<Player> listSave;
public void Save( ) //多对象存储
{
for (int i = 0; i < listSave.Count ; i++)
{
PlayerPrefs.SetString(listSave[i].PlayName + "_name", listSave[i].RealName);
PlayerPrefs.SetInt(listSave[i].PlayName + "_soccer", listSave[i].Soccer);
PlayerPrefs.SetInt(listSave[i].PlayName + "_time", listSave[i].Time);
}
PlayerPrefs.Save();
}
public void Print() //读取排行榜信息
{
string a;
int b;
int c;
for (int i = 0; i < listSave.Count; i++)
{
a = PlayerPrefs.GetString(listSave[i].PlayName + "_name");
b = PlayerPrefs.GetInt(listSave[i].PlayName + "_soccer");
c = PlayerPrefs.GetInt(listSave[i].PlayName + "_time");
print($"玩家{ listSave[i].PlayName} 的名字为:{a}");
print($"玩家{ listSave[i].PlayName} 的分数为:{b}");
print($"玩家{ listSave[i].PlayName} 的排名为:{a}");
}
}
// Start is called before the first frame update
void Start()
{
listSave = new List<Player>();
Player player1 = new Player("张三", "Baga", 99, 1);
Player player2 = new Player("李四", "Gaji", 56, 3);
Player player3 = new Player("王五", "Wujige", 92, 2);
listSave.Add(player1);
listSave.Add(player2);
listSave.Add(player3);
Save();
Print();
}
// Update is called once per frame
void Update()
{
}
}
题目:上一题的玩家类中包含一个List存储了拥有的所有装备信息。请在上一题的基础上,把装备信息的存储和读取加上
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏文章来源:https://www.toymoban.com/news/detail-439293.html
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!文章来源地址https://www.toymoban.com/news/detail-439293.html
到了这里,关于【unity之数据持久化】-Unity公共类PlayerPrefs的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!