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

news/2024/7/21 4:04:43 标签: 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;
    }
}  


http://www.niftyadmin.cn/n/880753.html

相关文章

7-236 输出数组元素 (15分)

7-236 输出数组元素 (15分) 本题要求编写程序&#xff0c;对顺序读入的n个整数&#xff0c;顺次计算后项减前项之差&#xff0c;并按每行三个元素的格式输出结果。 输入格式&#xff1a; 输入的第一行给出正整数n&#xff08;1<n≤10&#xff09;。随后一行给出n个整数&am…

[Unity基础]一个简单的状态机

状态机一般情况下只有两个类&#xff1a;状态基类以及管理状态的类。 状态机主要用于AI或者场景切换(不同场景对应的就是游戏的不同状态)&#xff0c;当然&#xff0c;这只是本人的想法。 using UnityEngine; using System.Collections;public abstract class State {public i…

7-209 找最长的字符串 (15分)

7-209 找最长的字符串 (15分) 本题要求编写程序&#xff0c;针对输入的N个字符串&#xff0c;输出其中最长的字符串。 输入格式&#xff1a; 输入第一行给出正整数N&#xff1b;随后N行&#xff0c;每行给出一个长度小于80的非空字符串&#xff0c;其中不会出现换行符&#x…

7-208 字符串排序 (15分)

7-208 字符串排序 (15分) 本题要求编写程序&#xff0c;读入5个字符串&#xff0c;按由小到大的顺序输出。 输入格式&#xff1a; 输入为由空格分隔的5个非空字符串&#xff0c;每个字符串不包括空格、制表符、换行符等空白字符&#xff0c;长度小于80。 输出格式&#xff1…

[C#基础]字符编码与二进制

参考链接&#xff1a; http://baike.baidu.com/link?url2pZNZUhdXpLZqvjzWdoCJjnOq-ncGt0jQveoZjvLq0HtxLKjLsrEvYA7t_TUaRxvh3aYWt0NFs4PQD2IBevgUa#7 http://www.cnblogs.com/laozuan/archive/2012/04/24/2467888.html http://www.xuanyusong.com/archives/1919 字符编…

7-166 打印矩形九九乘法表 (15分)

7-166 打印矩形九九乘法表 (15分) 输出九九乘法口诀。 输出格式: 以矩形的格式输出九九乘法表&#xff0c;每个式子按“a*bc”形式输出&#xff0c;其中a和b各占1位列宽&#xff0c;c占3位列宽并左对齐。 输出样例: 1*11 1*22 1*33 1*44 1*55 1*66 1*77 1*88 1*99 …

[Unity基础]自带寻路Navmesh

自带寻路Navmesh的三大组件&#xff1a; 1.Nav Mesh Agent&#xff1a;主要挂在寻路物体上 2.Off Mesh Link&#xff1a;实现区域转移功能(例如&#xff0c;有时不一定只是在地面上进行寻路&#xff0c;可能有些高高的平台&#xff0c;平台与地面是不相连的&#xff0c;使用…

7-234 实数排序 (10分)

7-234 实数排序 (10分) 本题要求编写程序&#xff0c;输入n个实数&#xff0c;使用指针引用的方式将它们按从大到小的顺序排列。 输入格式: 输入第一行给出一个正整数n&#xff08;2≤n≤10&#xff09;,输入第二行给出n个实数&#xff0c;其间以空格分隔。 输出格式: 输出从…