Hey, I need to load a json file to place GOs based on the data.
Now, I need to do this Async since I’m using AR that’s using the main thread.
Here’s my typical Stream reader. I’m not sure how to go about this. I know there’s a async await function for .NET 4.5 and I’m using Unity 2019.2.
private void PlaceObject()
{
Vector3 pos = placementPose.position;
Quaternion rot = placementPose.rotation;
ObjClass[] obj;
using (StreamReader stream = new StreamReader(Application.persistentDataPath + "/objects.json"))
{
string json = stream.ReadToEnd();
obj = JsonHelper.FromJson<ObjClass>(json);
//populate
container = Instantiate(TankObjectReference.Instance.tankObjectDictionary["ObjectContainer"], pos, rot) as GameObject;
foreach (ObjClass item in obj)
{
//Debug.Log("****objectID: "+item.objectID);
if (item.objectID != "")
{
try
{
GameObject objID = TankObjectReference.Instance.tankObjectDictionary[item.objectID];
var p = item.pos;
var r = item.rot;
var scale = item.scale;
var go = Instantiate(objID, container.transform) as GameObject;
go.transform.localPosition = p;
go.transform.localRotation = r;
go.transform.localScale = scale;
}
catch
{
Debug.Log("obj doesn't exist");
}
}
}
}
}