unity持久化数据之XML和Excel
unity持久化数据之XML
public class XMLDataMananger: Singleton<XMLDataMananger>
{
protected XMLDataMananger(){}
[XmlRoot]
public class GameData
{
public struct TransData
{
public float X;
public float Y;
public float Z;
}
public class PlayerData
{
public string Name;
public int Gold;
public TransData Pos;
}
public class EnemyData
{
public int Health;
public int EnemyID;
public TransData Pos;
}
public PlayerData Player = new PlayerData();
public List<EnemyData> Enemies = new List<EnemyData>();
}
public GameData GMData = new GameData();
public void Save(string FileName = "GameData.xml")
{
XmlSerializer Serializer = new XmlSerializer(typeof(GameData));
FileStream Stream = new FileStream(FileName, FileMode.Create);
Serializer.Serialize(Stream, GMData);
Stream.Close();
}
public void Load(string FileName = "GameData.xml")
{
XmlSerializer Serializer = new XmlSerializer(typeof(GameData));
FileStream Stream = new FileStream(FileName, FileMode.Open);
GMData = Serializer.Deserialize(Stream) as GameData;
Stream.Close();
}
}
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();
}
其他
注:(套娃)
//创建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
点赞鼓励下,(づ ̄3 ̄)づ╭❤~
作者:世纪末的魔术师
出处:https://www.cnblogs.com/Firepad-magic/
Unity最受欢迎插件推荐:点击查看
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。