搜档网
当前位置:搜档网 › unity 读取excel表 生成asset资源文件

unity 读取excel表 生成asset资源文件

*导入excel,解析xml,生成c#数据表,生成asset数据文件
*原理 在导入xml文件的时候通过XmlReader读取xml文件
*把需要的内容按照读取的顺序保存,然后在生成key,最后生成key,value格式,然后自己手动定义要保存的内容数据格式最后通过自定义的数据格式生成数据文件
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Xml;

using System;
using System.Collections.Generic;


public class ExcekControl : AssetPostprocessor {
static string FILE_PATH = "Assets\\Data\\";

/** 在导入所有资源之后 */
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths){
string excelPath = "";
for (int i = 0; i < importedAssets.Length; i++) {
/** 拦截Assets/Excel目录下面所有.xml文件进行解析 */
excelPath = importedAssets[i];
if(excelPath.Contains(".xml"))
{
ReadXml(excelPath);
Debug.Log("Excel import asset path : " + excelPath);
}
}
}

static void ReadXml(string xmlPath)
{
/** 读取xml 文件 */
XmlReader xReader = null;
try{
xReader = XmlReader.Create(xmlPath);
}
catch(Exception e){
Debug.LogError("Read Excel Error! Path = " + xmlPath);
}
if (xReader == null) {
return;
}

/** 保存所有数据到表中 */
List xmlDataList = new List ();
xmlSheetList xsl = null;

/** 记录每个节点的开始 */
bool sheetBegin = false;
bool rowBegin = false;
bool cellBegin = false;
bool dataBegin = false;

/** 行数 */
int rowNum = 0;
/** 列数 */
int cellNum = 0;
/** 计数器 */
int num = 0;
while (xReader.Read ()) {
string name = https://www.sodocs.net/doc/453481176.html,;
if(name == "Worksheet")
{
/** 开始 */
if(xReader.NodeType == XmlNodeType.Element)
{
sheetBegin = true;
/** xmlSheet */
xsl = new xmlSheetList();
xsl.Clear();
}
/** 结束 */
else if(xReader.NodeType == XmlNodeType.EndElement){
sheetBegin = false;
xmlDataList.Add(xsl);
}
}
else if(name == "Row")
{
/** 开始 */
if(xReader.NodeType == XmlNodeType.Element)
{
rowBegin = true;
rowNum++;
}
/** 结束 */
else if(xReader.

NodeType == XmlNodeType.EndElement){
rowBegin = false;
}
}
else if(name == "Cell"){
/** 开始 */
if(xReader.NodeType == XmlNodeType.Element)
{
cellBegin = true;
if(rowNum == 1)
{
cellNum++;
}
if(!string.IsNullOrEmpty(xReader.GetAttribute("ss:Index")))
{

Debug.LogError(string.Format("xml read error! may be empty! : {0} Row = {1} Cell = {2}.", xmlPath, (int)(num / cellNum), ((num % cellNum)+1)));
return;
}
}
/** 结束 */
else if(xReader.NodeType == XmlNodeType.EndElement){
cellBegin = false;
}
}
else if(name == "Data"){
/** 开始 */
if(xReader.NodeType == XmlNodeType.Element)
{
dataBegin = true;
}
/** 结束 */
else if(xReader.NodeType == XmlNodeType.EndElement){
dataBegin = false;
}
}
else{
/** 过滤数据 */
if(sheetBegin && rowBegin && cellBegin && dataBegin)
{
num++;
if(xsl != null)
{
xsl.Add(xReader.Value);
}
}
}
}
int an = xmlDataList.Count;
int bn = 0;

List keys = new List ();
for (int i = 0; i < xmlDataList.Count; i++) {
bn = xmlDataList[i].sheetList.Count;
for(int k = 0; k < bn; k++)
{
if(k < cellNum * 2)
{
if(k >= cellNum)
{
if(i == 0)
{
keys.Add(xmlDataList[i].sheetList[0]);
}
}
xmlDataList[i].sheetList.RemoveAt(0);
}else{
break;
}
}
}

List rowList = new List ();
for(int k = 0; k < xmlDataList.Count; k++)
{
for(int i = 0; i < xmlDataList[k].sheetList.Count / cellNum; i++)
{
Row row = new Row();
row._key = keys;
row._cells.Add(xmlDataList[k].sheetList[i * 3]);
row._cells.Add(xmlDataList[k].sheetList[i * 3 + 1]);
row._cells.Add(xmlDataList[k].sheetList[i * 3 + 2]);
rowList.Add(row);
}
}
/** 创建资源 */
CreatXm

lListAsset (rowList, xmlPath);
}

/** 生成数据文件 */
static void CreatXmlListAsset(List rl, string xmlPath)
{
if (xmlPath.Contains ("ActorData.xml")) {
GActorDataList actorDataList = null;

UnityEngine.Object oldFile = AssetDatabase.LoadAssetAtPath (xmlPath, typeof(GActorDataList));
bool newFile = false;
if (oldFile) {
actorDataList = oldFile as GActorDataList;
}else{
actorDataList = new GActorDataList();
newFile = true;
}
actorDataList.ActorDataList.Clear ();

for (int i = 0; i < rl.Count; i++) {
ActorData newData = new ActorData();
Row data = rl[i];
newData.id = data["id"];
newData.type = GetInt(data["type"]);
https://www.sodocs.net/doc/453481176.html,Ids = data["nameIds"];
actorDataList.ActorDataList.Add(newData);
}

if (newFile) {
AssetDatabase.CreateAsset(actorDataList, FILE_PATH + "ActorDataList.asset");
}else{
EditorUtility.SetDirty(actorDataList);
}
}
else if (xmlPath.Contains ("ActorDataA.xml")) {
GActorDataList actorDataList = null;

UnityEngine.Object oldFile = AssetDatabase.LoadAssetAtPath (xmlPath, typeof(GActorDataList));
bool newFile = false;
if (oldFile) {
actorDataList = oldFile as GActorDataList;
}else{
actorDataList = new GActorDataList();
newFile = true;
}
actorDataList.ActorDataList.Clear ();

for (int i = 0; i < rl.Count; i++) {
ActorData newData = new ActorData();
Row data = rl[i];
newData.id = data["id"];
newData.type = GetInt(data["type"]);
https://www.sodocs.net/doc/453481176.html,Ids = data["nameIds"];
actorDataList.ActorDataList.Add(newData);
}

if (newFile) {
AssetDatabase.CreateAsset(actorDataList, FILE_PATH + "ActorDataListA.asset");
}else{
EditorUtility.SetDirty(actorDataList);
}
}
else if (xmlPath.Contains ("ActorDataB.xml")) {
GActorDataList actorDataList = null;

UnityEngine.Object oldFile = AssetDatabase.LoadAssetAtPath (xmlPath, typeof(GActorDataList));
bool newFile = false;
if (oldFile) {
actorDataList = oldFile as GActorDataList;
}else{
actorDataList = new GActorDataList();
newFile = true;
}
actorDataList.ActorDataList.Clear ();

fo

r (int i = 0; i < rl.Count; i++) {
ActorData newData = new ActorData();
Row data = rl[i];
newData.id = data["id"];
newData.type = GetInt(data["type"]);
https://www.sodocs.net/doc/453481176.html,Ids = data["nameIds"];
actorDataList.ActorDataList.Add(newData);
}

if (newFile) {
AssetDatabase.CreateAsset(actorDataList, FILE_PATH + "ActorDataListB.asset");
}else{
EditorUtility.SetDirty(actorDataList);
}
}
}

static int GetInt (string data)
{
float val = 0;
try {
val = float.Parse (data);
} catch (Exception e) {
Debug.LogError (e.Message + " data[" + data + "]");
}
return Mathf.RoundToInt (val);
}

static float GetFloat (string data)
{
float val = 0;
try {
val = float.Parse (data);
} catch (Exception e) {
Debug.LogError (e.Message + " data[" + data + "]");
}
return val;
}

static bool GetBool (string data)
{
int val = 0;
try {
val = int.Parse (data);
} catch (Exception e) {
Debug.LogError (e.Message + " data[" + data + "]");
}
return val == 0 ? false : true;
}
}

/** xmlsheel所有字符串数据 */
public class xmlSheetList{
public List sheetList = new List ();

public void Add(string str)
{
sheetList.Add (str);
}

public void Clear(){
sheetList.Clear();
}
}

/** xml (key, Value) Cell */
public class Row
{
public List _cells= new List();
public List _key = new List();

public string this[string key]
{
get{
string val = "";
int pos = _key.IndexOf(key);
if(pos < 0 || pos >= _cells.Count)
{
Debug.LogError("can't find the value of the key:["+key+"]");
}else{
val = _cells[pos];
}
return val;
}
}
}
上面代码主要是读取和解析excel
GActorDataList 是自己定义的数据格式,根据自己需求自己定义

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class ActorData{
public string id;
public int type;
public string nameIds;
}

public class GActorDataList : ScriptableObject {
public List ActorDataList = new List ();
}

相关主题