Unity数据持久化_PlayerPrefsPlayerPrefs的 存读删
Posted _ElecSheep
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity数据持久化_PlayerPrefsPlayerPrefs的 存读删相关的知识,希望对你有一定的参考价值。
1.PlayerPrefs是什么?
PlayerPrefs是Unity提供的可以用于存储和读取玩家数据的公共类
PlayerPrefs的数据存储 类似于键值对存储,一个键对应一个值
提供了三种可存储类型:int、float、string
键:string类型
值:int float string 对应三种API
PlayerPrefs的局限性是 它只能存储3种数据类型
如果想要存储别的数据类型,只能降低精度,或者提高精度来进行存储
优点:
简单、快捷、易懂
缺点:
1.重复工作量繁多,自定义数据类都需要自己去实现存储读取的功能,而且代码的相似度极高
2.数据容易被修改,只要找到文件位置,就可以轻易地进行修改
主要用处:
单独使用它的原生功能 非常适合存储一些对安全性要求不高的简单数据
但是也不能小看它,对它进行简单的封装,也可以让它变得方便又安全
2.存储相关
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson1_PlayerPrefs : MonoBehaviour
private void OnGUI()
//1.存int型
PlayerPrefs.SetInt("myAge", 18);
//2.存float型
PlayerPrefs.SetFloat("myHeight", 181.5f);
//3.存string型
PlayerPrefs.SetString("myName", "假面骑士");
//注意:直接调用Set方法,只是把数据存到了内存中
// 当游戏结束时,Unity会自动把数据存到硬盘中
// 如果游戏没有正常结束(报错或崩溃),那么数据将会丢失
//解决这一问题的办法,调用.Save()方法,只要一调用,就会马上把数据存到硬盘中
PlayerPrefs.Save();
//补充:如果不同类型用同一个键进行存储,会把上一个数据覆盖掉
PlayerPrefs.SetInt("myHeight", 181);
3.读取相关
注意:游戏运行时,只要Set了对应键值对,就算没有马上.Save到硬盘,也能读取出信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson1_PlayerPrefs : MonoBehaviour
private void OnGUI()
PlayerPrefs.SetInt("myAge", 18);
PlayerPrefs.SetFloat("myHeight", 181.5f);
PlayerPrefs.SetString("myName", "假面骑士");
PlayerPrefs.Save();
//1.读int型
int age = PlayerPrefs.GetInt("myAge");
//还有个重载
//参数2 如果找不到键为myAge的值,就返回默认值100
//参数2的作用:在得到不存在的数据时,就可以利用参数2来进行基础数据的初始化
age = PlayerPrefs.GetInt("myAge", 23);
//2.读float型
float height = PlayerPrefs.GetFloat("myHeight");
//3.读string型
string name = PlayerPrefs.GetString("myName");
//补充:判断数据是否存在
if (PlayerPrefs.HasKey("myName"))
print("存在键为myName的数据");
4.删除数据
//删除指定键值对
PlayerPrefs.DeleteKey("myName");
//删除所有数据
PlayerPrefs.DeleteAll();
unity持久化数据之Excel
tags: unity,unity excel,unity持久化数据
grammar_cjkRuby: true
---
unity持久化数据之Excel
第三方库
EPPlus:https://github.com/JanKallman/EPPlus
读取
using OfficeOpenXml;
using UnityEditor;
.... ....
[MenuItem("Excel/Load Excel")]
static void LoadExcel()
{
string path = Application.dataPath + "/Excel/test.xlsx";
//读取Excel文件
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (ExcelPackage excel = new ExcelPackage(fs))
{
//遍历工作表
ExcelWorksheets worksheets = excel.Workbook.Worksheets;
Debug.LogFormat("sheetcount:{0}",worksheets.Count);
for (int i = 1; i <=worksheets.Count ; i++)
{
ExcelWorksheet worksheet = worksheets[i];
int colCount = worksheet.Dimension.End.Column;
//获取工作表名称
Debug.Log("sheet:" + worksheet.Name);
for (int row = 1,count=worksheet.Dimension.End.Row; row <=count; row++)
{
for (int col = 1; col <= colCount; col++)
{
//读取单元格数据
var text = worksheet.Cells[row, col].Text ?? "";
Debug.LogFormat("下标{0},{1} 内容:{2}",row,col,text);
}
}
}
}
}
}
创建
[MenuItem("Excel/Write Excel")]
static void WirteExcel()
{
//创建Excel
string path = Application.dataPath + "/Excel/new.xlsx";
var file = new FileInfo(path);
using (ExcelPackage excel = new ExcelPackage(file))
{
//写入数据
ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add("sheet1");
worksheet.Cells[1, 1].Value = "nice";
worksheet.Cells[1, 2].Value = "job";
//保存
excel.Save();
}
AssetDatabase.Refresh();
}
其他
注:(套娃){摘自https://www.cnblogs.com/shuaichao/p/4262346.html}
//创建Excel工作表
private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName)
{
p.Workbook.Worksheets.Add(sheetName);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = sheetName; //Setting Sheet's name
ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet
return ws;
}
//合并的Excel列
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "Sample DataTable Export"; // Heading Name
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range
ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true; //Font should be bold
ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center
Excel单元格背景颜色
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
//Excel单元格边框
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
//Excel公式
//Setting Sum Formula
cell.Formula = "Sum(" + ws.Cells[3, colIndex].Address + ":" + ws.Cells[rowIndex - 1, colIndex].Address + ")";
//添加注释到Excel单元格
private static void AddComment(ExcelWorksheet ws, int colIndex, int rowIndex, string comment, string author)
{
//Adding a comment to a Cell
var commentCell = ws.Cells[rowIndex, colIndex];
commentCell.AddComment(comment, author);
}
//添加图像在Excel工作表
private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
//How to Add a Image using EP Plus
Bitmap image = new Bitmap(filePath);
ExcelPicture picture = null;
if (image != null)
{
picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
picture.From.Column = columnIndex;
picture.From.Row = rowIndex;
picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
picture.SetSize(100, 100);
}
}
//添加自定义对象到Excel工作表
private static void AddCustomShape(ExcelWorksheet ws, int colIndex, int rowIndex, eShapeStyle shapeStyle, string text)
{
ExcelShape shape = ws.Drawings.AddShape("cs" + rowIndex.ToString() + colIndex.ToString(), shapeStyle);
shape.From.Column = colIndex;
shape.From.Row = rowIndex;
shape.From.ColumnOff = Pixel2MTU(5);
shape.SetSize(100, 100);
shape.RichText.Add(text);
}
litjson
https://www.cnblogs.com/Firepad-magic/p/5532650.html
Unity最受欢迎的插件,可以让您的游戏如虎添翼,为您节省大量时间可以投入在游戏的创意和细节上
以上是关于Unity数据持久化_PlayerPrefsPlayerPrefs的 存读删的主要内容,如果未能解决你的问题,请参考以下文章
Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)
Unity 数据持久化之PlayerPrefs 如何存储数据