- Home /
Load from json and instantiate same object with same location rotation and scale
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);
Debug.Log(Application.persistentDataPath);
}
public IEnumerator OpenFromfile()
{
WWW w = new WWW("https://srv-store4.gofile.io/download/udTOwj/file.json");
yield return w;
if (w.error != null)
{
Debug.Log("Error .. " + w.error);
// for example, often 'Error .. 404 Not Found'
}
else
{
Debug.Log("Found ... ==>" + w.text + "<==");
string saveString = w.text;
Debug.Log(saveString);
GameObjectDataCollection saveObject = JsonUtility.FromJson<GameObjectDataCollection>(saveString);
Debug.Log(JsonUtility.FromJson<GameObjectDataCollection>(saveString).GameObjectData.Count);
Debug.Log(JsonUtility.FromJson<GameObjectDataCollection>(saveString).GameObjectData);
for (int i = 0; i < JsonUtility.FromJson<GameObjectDataCollection>(saveString).GameObjectData.Count; ++i)
{
Instantiate()
//JsonUtility.FromJson<GameObjectDataCollection>(saveString).GameObjectData.ToArray();
}
}
}
public void OpenFromFileButton()
{
StartCoroutine(OpenFromfile());
}
[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);
}
}
Anyone that could help me out? This is the code I already have
Answer by CmdrZin · Jan 11, 2021 at 07:39 PM
You'll need two things: A prefab for the object and a data object to store it's information.
You save the data object in the JSON file. After Instantiating the prefab, update it's transform.position and other elements from saved data in the data object.
I use a minimal script for the prefab that just has a ID. I set this to the id in the data object to link the two together. All the other data (health, power, etc.) is maintained in the data object.
Your code seems to have every thing to save the data. All you need is a prefab for you GameObject that is displayed. You could use a Cube for now. Create a script like this and attach to the prefab.
// Attached to a Prefab
public class Node : $$anonymous$$onoBehaviour
{
public int ID; // set to id of GameObjectData object that supports this new object.
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
Right after you instantiate the prefab into a real GameObject, set it's Id to the id of the GameObjectData. You can set it's position that's stored in the GameObjectData at that time also.
If you keep a Directory(id, GameObject) and a Directory(id, GameObjectData) they are linked by the ID which is an int and used as the Key to access the directory.
The Directory(id, GameObjectData) is what you save and load with JSON.
The Directory(id, GameObject) is what you build each time the scene is loaded. Hope that helps.
Thanks for the help, all I need now is a way to loop through all JSON thingys to do it for all saved objects