C# 用于导入CSV数据以设置对象动画的脚本

C# 用于导入CSV数据以设置对象动画的脚本,c#,csv,unity3d,animation,C#,Csv,Unity3d,Animation,我需要通过CSV文件中的坐标来设置对象的动画。我写了一个代码,从文件中获取坐标。但我不明白该朝哪个方向前进。谁有什么想法 这是(Unity C#): 公共游戏对象中心; 公共游戏物体脊柱; 公共游戏对象中心; 公共游戏对象头部; 公共游戏对象左肩; 公共游戏对象ElbowLeft; 公共游戏对象腕带; 公共游戏对象左手边; 公共游戏对象肩权; 公共游戏对象ElbowRight; 公共游戏对象WristRight; 公共游戏对象权限; 公共游戏对象;左; 左下跪的公共游戏对象; 公共游戏对象Ank

我需要通过CSV文件中的坐标来设置对象的动画。我写了一个代码,从文件中获取坐标。但我不明白该朝哪个方向前进。谁有什么想法

这是(Unity C#):

公共游戏对象中心;
公共游戏物体脊柱;
公共游戏对象中心;
公共游戏对象头部;
公共游戏对象左肩;
公共游戏对象ElbowLeft;
公共游戏对象腕带;
公共游戏对象左手边;
公共游戏对象肩权;
公共游戏对象ElbowRight;
公共游戏对象WristRight;
公共游戏对象权限;
公共游戏对象;左;
左下跪的公共游戏对象;
公共游戏对象AnkleLeft;
公共游戏对象左下角;
公开游戏对象权;
公共游戏对象,右;
公共游戏对象踝关节;
公共游戏对象的足迹;
公共文本资产csvFile;
float[][]数据数组;
void Start()=>dataArray=readCSV();
公共浮动[][]读取CSV()
{
string[]rows=csvFile.text.Split('\n');
float[][]重新排列=新的float[rows.Length][];
for(int i=1;i

我有一个带有坐标的CSV文件,我想读取这个文件,并在Unity中设置游戏对象的动画。好的,下面是如何实现它的,这是非常基本的,如果CSV有任何其他结构,它将失败。因此,我建议实施检查,以确保您的程序在CSV具有不同结构时不会失败

public class Character
{
    public float Timestamp;
    public Vector3 Hip;
    public Vector3 Spine;
    public Vector3 ShoulderCenter;
    public Vector3 Head;
    //etc etc etc
}
还有你读CSV的班级

private CultureInfo ci;
List<Character> step;
// Start is called before the first frame update
void Start()
{
    //depending on your operation system you should set the CultureInfo (just to make sure that , is read as decimal)
    ci = CultureInfo.CreateSpecificCulture("de-DE");
    step = new List<Character>();
    LoadCSV();
}

public void LoadCSV()
{
    //read in data file
    StreamReader reader = new StreamReader("Assets/Resources/Data.csv");
    List<string> rows = new List<string>();
    while (!reader.EndOfStream)
    {
        rows.Add(reader.ReadLine());
    }
    reader.Close();

    //we start on line 1 because the first one is the header
    for(int i = 1; i < rows.Count; i++)
    {
        //delimiter csv has ';'
        var column = rows[i].Split(';');
        Character tmp = new Character();
        tmp.Timestamp = float.Parse(column[0], ci);
        tmp.Hip = ParseVector3(column[1], column[2], column[3]);
        tmp.Spine = ParseVector3(column[4], column[5], column[6]);
        tmp.ShoulderCenter = ParseVector3(column[7], column[8], column[9]);
        tmp.Head = ParseVector3(column[10], column[11], column[12]);
        //etc etc etc
        step.Add(tmp);
    }
}

public Vector3 ParseVector3(string x, string y, string z)
{
    Vector3 position = new Vector3();
    position.x = float.Parse(x, ci);
    position.y = float.Parse(y, ci);
    position.z = float.Parse(z, ci);

    return position;
}
私有文化信息ci;
列表步骤;
//在第一帧更新之前调用Start
void Start()
{
//根据您的操作系统,您应该设置CultureInfo(只是为了确保读取为十进制)
ci=CultureInfo.CreateSpecificCulture(“de”);
步骤=新列表();
LoadCSV();
}
公共void LoadCSV()
{
//读入数据文件
StreamReader=新的StreamReader(“资产/资源/数据.csv”);
列表行=新列表();
而(!reader.EndOfStream)
{
添加(reader.ReadLine());
}
reader.Close();
//我们从第1行开始,因为第一行是标题
对于(int i=1;i
您可以发布失败的值吗?添加到调试控制台的代码中;您好,欢迎来到苏。你能解释一下你的问题吗?你到底想归档什么?读取文件时有问题吗?您对动画有问题吗?如果有,具体是什么问题?如果您能够将问题缩小到一个特定的问题,那就太好了。您只向我们展示了一个返回
float[][]]
的方法。。。。你的目标是什么?这些值应该如何应用于对象。。。?您的CSV文件看起来如何?您是否愿意添加一个标识符列,例如按对象名称查找并使用字典?@nka_Zz我补充了我上面的问题。是的,我在读取CSV文件时遇到问题。即FormatException:输入字符串的格式不正确。System.Number.ParseSingle(System.String值、System.Globalization.NumberStyles选项、System.Globalization.NumberFormatInfo numfmt)(位于:0)
private CultureInfo ci;
List<Character> step;
// Start is called before the first frame update
void Start()
{
    //depending on your operation system you should set the CultureInfo (just to make sure that , is read as decimal)
    ci = CultureInfo.CreateSpecificCulture("de-DE");
    step = new List<Character>();
    LoadCSV();
}

public void LoadCSV()
{
    //read in data file
    StreamReader reader = new StreamReader("Assets/Resources/Data.csv");
    List<string> rows = new List<string>();
    while (!reader.EndOfStream)
    {
        rows.Add(reader.ReadLine());
    }
    reader.Close();

    //we start on line 1 because the first one is the header
    for(int i = 1; i < rows.Count; i++)
    {
        //delimiter csv has ';'
        var column = rows[i].Split(';');
        Character tmp = new Character();
        tmp.Timestamp = float.Parse(column[0], ci);
        tmp.Hip = ParseVector3(column[1], column[2], column[3]);
        tmp.Spine = ParseVector3(column[4], column[5], column[6]);
        tmp.ShoulderCenter = ParseVector3(column[7], column[8], column[9]);
        tmp.Head = ParseVector3(column[10], column[11], column[12]);
        //etc etc etc
        step.Add(tmp);
    }
}

public Vector3 ParseVector3(string x, string y, string z)
{
    Vector3 position = new Vector3();
    position.x = float.Parse(x, ci);
    position.y = float.Parse(y, ci);
    position.z = float.Parse(z, ci);

    return position;
}