C# 将数据存储到unity中的csv文件中
我有一个立方体,它在一个方向上不断移动,我想在excel表格的行和列中,以每秒x、y和z坐标的形式存储它的位置。我编写了一个用于收集和存储数据的代码:C# 将数据存储到unity中的csv文件中,c#,unity3d,C#,Unity3d,我有一个立方体,它在一个方向上不断移动,我想在excel表格的行和列中,以每秒x、y和z坐标的形式存储它的位置。我编写了一个用于收集和存储数据的代码: using UnityEngine; using System.Collections; using System.IO; using System; public class fileMaker: MonoBehaviour { public static void putBytes(ref byte[] output, int in
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class fileMaker: MonoBehaviour {
public static void putBytes(ref byte[] output, int index, float value)
{
//turns a float into its 4 bytes and then puts them into the output array
//at the given index
byte[] data = BitConverter.GetBytes(value);
output[index] = data[0];
output[index + 1] = data[1];
output[index + 2] = data[2];
output[index + 3] = data[3];
}
public static void makeFile(Vector3 position)
{
//each float is 4 bytes.
//3 floats in a vector 3(x,y,z) and 3x4 =12!
byte[] output = new byte[12];
//get bytes for each part of our lil vector3
putBytes(ref output, 0, position.x);
putBytes(ref output, 4, position.y);
putBytes(ref output, 8, position.z);
File.WriteAllBytes(Application.dataPath + "/log.csv", output);
}
public static void loadFile()
{
//converts it all back into pretty print
if (File.Exists(Application.dataPath + "/log.csv"))
{
byte[] input = File.ReadAllBytes(Application.dataPath + "/log.csv");
int length = input.Length;
if (length == 12)
{
Vector3 ourVector3 = new Vector3();
ourVector3.x = (float)BitConverter.ToSingle(input, 0);
ourVector3.y = (float)BitConverter.ToSingle(input, 4);
ourVector3.z = (float)BitConverter.ToSingle(input, 8);
print("Position saved in file (" + Application.dataPath + "/log.csv): " + ourVector3.ToString());
}
}
}
}
但是这个代码有两个问题
那么我该如何移除这个按钮,让unity每次都记录下这个位置呢。我对unity和编程非常陌生。除非您的代码比您向我们展示的内容更多,否则我只能问“为什么?”。您没有将数据保存为CSV。您正在输入字节数据,并试图手动读回 在这里,在我的脑海中,有两个选择是非常少的头痛:
- 创建一个
类,然后将引用存储在一个Serializable
脚本中,或者使用.Net二进制序列化程序monobhavior
- 使用Json存储数据,然后再次将字符串存储为
中的引用,或者将其序列化MonoBahviour
- 将数据存储在
中ScriptableObject
using UnityEngine;
using System.Collections.Generic;
public class Test : MonoBehaviour
{
float timer = 0.0f;
public TestSO so;
public void Update ( )
{
timer += Time.deltaTime;
if ( timer > 1.0f )
{
timer -= 1.0f;
so.Vectors.Add ( new Vector3 ( UnityEngine.Random.Range ( 0, 10 ), UnityEngine.Random.Range ( 0, 10 ), UnityEngine.Random.Range ( 0, 10 ) ) );
}
}
}
[CreateAssetMenu ( fileName = "TestSO", menuName = "Create TestSO", order = 0 )]
public class TestSO : ScriptableObject
{
public List<Vector3> Vectors;
}
完成编辑后,只需将
pollCube
设置为false,因为这将无限期运行。(您也可以在其中设置一些保护措施,以便在一定时间后将pollCube设置为false,以防忘记)。csv文件是文本,值之间带有逗号。你在写一个二进制文件。二进制文件通常不是人类可读的。这实际上不是我的代码,我在论坛上找到了:。我想我会使用你提到的自定义编辑器窗口,但是我如何将数据存储为可编写脚本的对象?我不需要使用自定义编辑器统一网格,我的主要目标是提取位置数据并将其存储到csv文件中。我只是想知道使用什么代码来实现这一点。我更新了答案以解决ScriptableObject序列化问题。这并没有明确回答您对csv文件的读写请求,尽管我不认为这是您的实际意图/最终要求,如果我可以假定的话。相信我,如果不编写自定义csv(de)序列化程序,它不会变得像这样简单或健壮。
#if UNITY_EDITOR
public async void PollCubePosition ( )
{
if ( pollCube ) return;
pollCube = true;
do
{
// Read cube data and perform whatever functions you want here..
// Wait 1 seconds.
await Task.Delay ( 1000 );
} while ( pollCube );
}
#endif