[Unity基础]解析excel以及动画切割

参考链接:

http://www.xuanyusong.com/archives/2429

http://blog.csdn.net/asd237241291/article/details/11788831

http://www.manew.com/thread-44458-1-1.html


excel转json:

http://pan.baidu.com/s/1gdvzsQZ

使用:

1.导入“ExcelToJson.unitypackage”工具包
2.把所有Excel表文件转成.csv文件。
3.然后把所有.csv文件copy到工程的Assets\Data文件夹目录
4.点击"Game->ExcelToJson"
5.然后默认会在“Assets\Json\Data”目录下生成所有Excel表文件对应的Json格式的数据。


解析excel:

一般 Excel的格式分为两种,一种是 .xls 还有一种是.xlsx ,这里我们只说.xlsx 。

需要使用第三方开发包:ICSharpCode.SharpZipLib,下载地址:http://yun.baidu.com/s/1pJ61ZUN

using UnityEngine;
using System.IO;
using Excel;
using System.Data;

//要引用System.Data.dll
//只能读取XLSX,且读取时XLSX文件要关闭
//在表格删除内容时,不能按del键,因为这样会使字符串变为空字符串,能读取到DataSet中,
//正确的删除方法是右键/删除
public class ReadXLSX {

    public static string[,] Read(string path, string sheetName)
    {
        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        DataSet dataSet = excelReader.AsDataSet();
        int rows = dataSet.Tables[sheetName].Rows.Count;
        int columns = dataSet.Tables[sheetName].Columns.Count;

        string[,] result = new string[rows,columns];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                result[i,j] = dataSet.Tables[sheetName].Rows[i][j].ToString();
                Debug.Log(result[i, j]);
            }
        }

        stream.Close();
        stream.Dispose();
        excelReader.Close();
        excelReader.Dispose();
        return result;
    } 
}

动画切割:

这里我有一个AnimClip.xlsx文件,文件中有两个sheet,一个叫all,用来记录需要切割的模型的名字,一个是rrobot,用来记录模型rrobot的动画切割信息





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

public static class AnimationClipConfig
{
    public static bool isInit = false;
    public static List<Model> modelList = new List<Model>();

    public static void Init()
    {
        if (isInit) return;
        isInit = true;

        string[,] names = ReadXLSX.Read(Application.dataPath + @"\AnimClip.xlsx", "all");
        for (int i = 1; i < names.GetLength(0); i++)
        {       
            string[,] clips = ReadXLSX.Read(Application.dataPath + @"\AnimClip.xlsx", names[i, 0]);
            Model model = new Model(names[i, 0]);

            for (int j = 1; j < clips.GetLength(0); j++)
            {
                string clipName = clips[j, 0];
                int clipFirstFrame = Convert.ToInt32(clips[j, 1]);
                int clipLastFrame = Convert.ToInt32(clips[j, 2]);
                bool clipIsLoop = Convert.ToBoolean(clips[j, 3]);
                model.animClips.Add(new AnimClip(clipName, clipFirstFrame, clipLastFrame, clipIsLoop));
            }

            modelList.Add(model);
        }   
    }

    #region 
    public class AnimClip
    {
        public string name;
        public int firstFrame;
        public int lastFrame;
        public bool isloop;

        public AnimClip(string name, int firstFrame, int lastFrame, bool isloop)
        {
            this.name = name;
            this.firstFrame = firstFrame;
            this.lastFrame = lastFrame;
            this.isloop = isloop;
        }
    }

    public class Model
    {
        public string name;
        public List<AnimClip> animClips = new List<AnimClip>();

        public Model(string name)
        {
            this.name = name;
        }
    }
    #endregion
}

using UnityEditor;  
using UnityEngine;  
  
public class FBXAnimationsCut : AssetPostprocessor {
  
    public void OnPreprocessModel()  
    {  
        //当前正在导入的模型  
        ModelImporter modelImporter = (ModelImporter) assetImporter;  
  
        AnimationClipConfig.Init();  
  
        foreach (AnimationClipConfig.Model item in AnimationClipConfig.modelList)  
        {  
            //当前导入模型的路径包含我们动画数据表中的模型名字,那就要对这个模型的动画进行切割  
            if (assetPath.Contains(item.name))  
            {  
                modelImporter.animationType = ModelImporterAnimationType.Legacy;               
                modelImporter.generateAnimations = ModelImporterGenerateAnimations.GenerateAnimations;
                //modelImporter.splitAnimations = true;  
  
                ModelImporterClipAnimation[] animations = new ModelImporterClipAnimation[item.animClips.Count];
                for (int i = 0; i < item.animClips.Count; i++)  
                {
                    animations[i] = SetClipAnimation(item.animClips[i].name, item.animClips[i].firstFrame, item.animClips[i].lastFrame, item.animClips[i].isloop);  
                }  
  
                modelImporter.clipAnimations = animations;  
            }  
        }  
    }

    ModelImporterClipAnimation SetClipAnimation(string name, int first, int last, bool isLoop)
    {
        ModelImporterClipAnimation tempClip = new ModelImporterClipAnimation();
        tempClip.name = name;
        tempClip.firstFrame = first;
        tempClip.lastFrame = last;
        tempClip.loop = isLoop;
        if (isLoop)
            tempClip.wrapMode = WrapMode.Loop;
        else
            tempClip.wrapMode = WrapMode.Default;

        return tempClip;
    }
}  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值