I am working on a simple AR 3d scanner project, and as a beginning I am trying to reconstruct a 3d model from a list of recorded point cloud data. I am kind of new to unity scripting and don’t actually know how I can convert this list of added point clouds to a .json file to be referenced to later.
So I need the list of vector3 postitions of the point cloud to be saved to a .json file in a persistent storage Location. Can anyone tell me how I can do this? I already have a list of vector3 data.
Any help is greatly appreciated.
Unity’s got a built in JSON converter, but it’s a bit finicky - it can’t directly serialize lists or arrays, so you have to stick the list/array inside a serializable class or struct. Here’s an example:
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public struct VectorContainer {
public List<Vector3> vectors;
}
public class TestScript : MonoBehaviour {
private void Start() {
var vectorContainer = new VectorContainer();
// This would be your vectors:
vectorContainer.vectors = new List<Vector3> {
Vector3.back,
Vector3.down,
Vector3.up
};
var str = JsonUtility.ToJson(vectorContainer, true);
// You'd write them to file instead of just printing them
Debug.Log(str);
}
}