Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nlgamers200 · Jan 11, 2021 at 05:10 PM · json

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nlgamers200 · Jan 11, 2021 at 07:41 PM 0
Share

Can you help me with how to do that?

avatar image CmdrZin · Jan 11, 2021 at 08:37 PM 0
Share

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.

avatar image nlgamers200 CmdrZin · Jan 11, 2021 at 09:06 PM 0
Share

Thanks for the help, all I need now is a way to loop through all JSON thingys to do it for all saved objects

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error on package.json: Unexpected token " at 1:1 2 Answers

How to save and load the state of dialogue when changing scenes 1 Answer

How to save and load any data type? 1 Answer

C# Converting json arrays using JsonUtility, 1 Answer

JSON Deserialization 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges