首页 > 编程语言 > 详细

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

时间:2015-03-27 10:32:24      阅读:370      评论:0      收藏:0      [点我收藏+]

做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域。项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料。如果学长们看到哪里写的不好欢迎吐槽并给予更好的解决方案。好了话也不多说了今天给大家亮出的是excel 数据解析和在unity项目中的应用。

 

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

注:ExcekControl 这个类必须放在Editor目录下
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<xmlSheetList> xmlDataList = new List<xmlSheetList> ();
        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 = xReader.Name;
            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<string> keys = new List<string> ();
        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<Row> rowList = new List<Row> ();
        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);
            }
        }
        /** 创建资源 */
        CreatXmlListAsset (rowList, xmlPath);
    }

    /** 生成数据文件 */
    static void CreatXmlListAsset(List<Row> 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"]);
                newData.nameIds = 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"]);
                newData.nameIds = 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 ();
            
            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"]);
                newData.nameIds = 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<string> sheetList = new List<string> ();

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

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

/** xml (key, Value) Cell */
public class Row
{
    public List<string> _cells= new List<string>();
    public List<string> _key = new List<string>();
    
    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;
        }
    }
} 
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<ActorData> ActorDataList = new List<ActorData> ();
}
ActorData自定义的数据格式根据项目需求

支持原创支持fastHro,转载请注明作者出处我的博客http://home.cnblogs.com/u/fastHro/

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

原文:http://www.cnblogs.com/fastHro/p/4370962.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!