首页 > 解决方案 > 用于导入 CSV 数据以动画对象的脚本

问题描述

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

这是(Unity C#):

public GameObject HipCenter;
public GameObject Spine;
public GameObject ShoulderCenter;
public GameObject Head;
public GameObject ShoulderLeft;
public GameObject ElbowLeft;
public GameObject WristLeft;
public GameObject HandLeft;
public GameObject ShoulderRight;
public GameObject ElbowRight;
public GameObject WristRight;
public GameObject HandRight;
public GameObject HipLeft;
public GameObject KneeLeft;
public GameObject AnkleLeft;
public GameObject FootLeft;
public GameObject HipRight;
public GameObject KneeRight;
public GameObject AnkleRight;
public GameObject FootRight;
public TextAsset csvFile;

float[][] dataArray;

void Start() => dataArray = readCSV(); 


public float [][] readCSV()
{
    
    string[] rows = csvFile.text.Split('\n');
    float[][] retArray = new float[rows.Length][];
    for (int i = 1; i < rows.Length; i++)
    {
        string[] s = rows[i].Split(',');
        for (int j = 1; j < s.Length; j++)
        {
            retArray[i][j] = float.Parse(s[j]); // FormatException: Input string was not in a correct format
        }
        return retArray;
    }
    
}

我有一个带有坐标的 CSV 文件在此处输入图像描述我想阅读此文件并在 Unity 中为游戏对象设置动画 在此处输入图像描述

标签: c#csvunity3danimation

解决方案


好的,这就是你如何实现它,这是超级初级的,如果 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;
}

推荐阅读