public void SaveToFile()
{
for (int i = 0; i < ListofObjects.transform.childCount;)
{
var pos = ListofObjects.transform.GetChild(i).transform.position;
var rot = ListofObjects.transform.GetChild(i).transform.localEulerAngles;
var scale = ListofObjects.transform.GetChild(i).transform.localScale;
var name = ListofObjects.transform.GetChild(i).transform.name;
i++;
}
SaveObject saveObject = new SaveObject
{
HowManyObjects = ListofObjects.transform.childCount,
};
string json = JsonUtility.ToJson(saveObject);
Debug.Log(json);
}
private class SaveObject
{
public int HowManyObjects;
}
Im trying to save the position, rotation, scale, and name of all children of ListofObjects to a son file. Could anyone help me out?
public void SaveToFile()
{
GameObjectDataCollection gameObjectDataCollection = new GameObjectDataCollection();
for (int i = 0; i < ListofObjects.transform.childCount; ++i)
{
Transform child = ListofObjects.transform.GetChild(i);
gameObjectDataCollection.Add(new GameObjectData(child.gameObject));
}
string json = JsonUtility.ToJson(gameObjectDataCollection);
Debug.Log(json);
System.IO.File.WriteAllText(Application.persistentDataPath + “/file.json”, json);
}
[System.Serializable]
public class GameObjectData
{
public string Name;
public Vector3 Position;
public Vector3 LocalEulerAngles;
public Vector3 LocalScale;
public GameObjectData(GameObject gameObject)
{
Name = gameObject.name;
Position = gameObject.transform.position;
LocalEulerAngles = gameObject.transform.localEulerAngles;
LocalScale = gameObject.transform.localScale;
}
}
[System.Serializable]
public class GameObjectDataCollection
{
public List<GameObjectData> GameObjectData = new List<GameObjectData>();
public void Add(GameObjectData gameObjectData)
{
GameObjectData.Add(gameObjectData);
}
}