提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
unity3d 各类文件格式的解析和使用
<?xml version="1.0" encoding="utf-8"?>
<skills>
<skill>
<id>1</id>
<name>火魅指</name>
<damage>100</damage>
</skill>
<skill>
<id>2</id>
<name>幽冥指</name>
<damage>300</damage>
</skill>
<skill>
<id>3</id>
<name>铁砂掌</name>
<damage>450</damage>
</skill>
</skills>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\Unity\teststudy\Assets\Skills.xml");
XmlNode root = xmlDoc.ChildNodes[1];
XmlNodeList skillList = root.ChildNodes;
foreach (XmlNode skill in skillList){
foreach (XmlNode node in skill.ChildNodes){
Debug.Log(node.Name);
}
}
// 导入json解析库 使用Newtonsoft.Json
using Newtonsoft.Json;
// 序列化类
public class Skills{
public int id;
public String name;
public int damage;
}
// 反序列化
List<Skills> list = new List<Skills>();
list = JsonConvert.DeserializeObject<List<Skills>>(File.ReadAllText(@"D:\Unity\teststudy\Assets\Skills.json"));
foreach (var skills in list){
Debug.Log(skills.damage);
}
// 序列化
Skills skill = new Skills();
skill.id = 100;
skill.name = "贪狼斩";
skill.damage = 9999;
string str = JsonConvert.SerializeObject(skill);
Debug.Log(str);
// 使用Excel和ICSharpCode.SharpZipLib dll文件
// 文件后缀必须是 xlsx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Excel;
using System.IO;
using System.Text;
public class ExcelInput : MonoBehaviour
{
public List<Vector3> ReadExcel(string str)
{
List<Vector3> upDataList = new List<Vector3>();
//创建文件读取流
FileStream fs = new FileStream(Application.streamingAssetsPath + str, FileMode.Open, FileAccess.Read);
//创建Excel读取类
IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(fs);
while (reader.Read())
{
Vector3 v3 = new Vector3();
v3.x = reader.GetFloat(0);
v3.z = reader.GetFloat(1);
v3.y = reader.GetFloat(2);
upDataList.Add(v3);
}
return upDataList;
}
}
List<Vector3> _sheetDataList1 = ReadExcel("/123.xlsx");
//导入数据库连接库
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
public class OperationSql
{
private NpgsqlConnection _conn;
private string Connec = "Server=192.168.2.66;Port=5432;UserId=postgres;Password=1234;Database=postgres";
/// <summary>
/// 数据查询
/// </summary>
/// <param name="sql">sql查询</param>
/// <returns></returns>
public string SelectSql(string sql)
{
try
{
_conn = new NpgsqlConnection(Connec);
_conn.Open();
if (_conn.State == ConnectionState.Open)
{
var cmd = new NpgsqlCommand(sql, _conn);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
return reader.GetString(0);
}
}
}
catch (Exception ex)
{
Debug.Log(ex);
}
return null;
}
/// <summary>
/// 修改并返回修改的行数
/// </summary>
/// <param name="sql">更新sql</param>
/// <returns>更新行数</returns>
public int UpdateSql(string sql)
{
try
{
_conn = new NpgsqlConnection(Connec);
_conn.Open();
if (_conn.State == ConnectionState.Open)
{
var cmd = new NpgsqlCommand(sql, _conn);
var r = cmd.ExecuteNonQuery(); //执行查询并返回受影响的行数
_conn.Close();
return r;
}
}
catch (Exception ex)
{
Debug.Log(ex);
}
return 0;
}
}
using System.Collections.Generic;
using HslCommunication;
using HslCommunication.Enthernet.Redis;
using Newtonsoft.Json;
using UnityEngine;
public class RedisTool{
private RedisClient _rc;
public RedisClient RedisToolConnect(){
#if UNITY_EDITOR
//测试环境的链接
_rc = new RedisClient("127.0.0.1", 6379, "");
#else
//上线环境的链接
_rc = new RedisClient("192.168.2.138", 8888, "123");
#endif
_rc.SelectDB(0);
_rc.ConnectServer();
return _rc;
}
}
本博客主要总结了目前数字孪生和游戏方向比较常用的几种格式解析
本文用到库文件都整理好到下面链接里面,需要的可以点击跳转
unity3d 常用dll库