Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dbrowndev · Feb 10, 2020 at 11:51 AM · jsoncsv

Saving the names of objects in a List to an external file

Hello! I am building an app that lets a user dynamically add and remove gameobjects from a scene. The gameobjects are currently being saved to a public List named furniture. I am wanting to allow users to save the names of the gameobjects within furniture to an external file, such as XML, CSV or JSON. I have been experimenting with JsonUtility.ToJson(this) and Sinbad.CsvUtil.SaveObjects but I'm struggling to get this to work.

Here is my current code:

 void pickingListTaskOnClick ()
     {
         foreach (GameObject go in furniture)
         {
             furnitureNames.Add (go.name);
             Debug.Log(go.name); 
         }
 
         SaveToString ();
 
         //Sinbad.CsvUtil.SaveObjects (furnitureNames, "pickingList.csv");
     }
 
     public string SaveToString ()
     {
         return JsonUtility.ToJson (furnitureNames);
     }

To potentially add complications, the user will be running IOS or Android as this is a mobile app.

Any help would be appreciated, thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Getsumi3 · Feb 11, 2020 at 09:32 AM

Hi @danbrownemotivevm

As @ajaykewat said, you need a class in which your data will be stored.

Create a new C# script. Let's say DataManager.cs
And inside this script create another class that will serializae your data
Here is an example (not tested):

 public class DataManager : MonoBehaviour
 {
     public static DataManager i;
 
     private void Awake()
     {
         if (i != null)
         {
             Destroy(gameObject);
         }
         else
         {
             i = this;
             DontDestroyOnLoad(gameObject);
         }
     Load();
     }
 
     public void Load()
     {
         DataModel.Load();
     }
     void OnApplicationPause(bool pauseStatus) 
     {
         if(pauseStatus) {
             DataModel.Save();
         }
     }
 
     public void OnApplicationQuit() 
     {
         DataModel.Save();
     }
 
     public void Trigger_ButtonSave() 
     {
         DataModel.Save();
     }
 
     public DataModel DataModel = new DataModel("save");
 
 }
 
 [System.Serializable]
 public class DataModel
 {
         public List<string> furnitureNames;
     
         private string name;
     
         public DataModel(string _name)
         {
             this.name = _name;
         }
     
         public void Save()
         {
            
             string json = JsonUtility.ToJson(this);
             
             System.IO.File.WriteAllText(System.IO.Path.Combine(Application.persistentDataPath, "save.json"), json);
         }
     
         public void Load()
         {
             string jsonToFile = System.IO.File.ReadAllText(System.IO.Path.Combine(path, "save.json"));
     
             DataModel packedData = JsonUtility.FromJson<DataModel>(jsonToFile);
     
             this.furnitureNames= packedData.furnitureNames;
     
         }
     
         public void SetFurniture(string newFurniture)
         {
              furnitureNames.Add(newFurniture);
         }
         public void GetFurniture(int i)
         {
             return furnitureNames[i];
         }
 }
 
Comment
Add comment · Show 1 · 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 dbrowndev · Feb 11, 2020 at 04:21 PM 0
Share

It works, thank you very much!!

avatar image
1

Answer by ajaykewat · Feb 10, 2020 at 01:56 PM

Hi @danbrownemotivevm , First of all you need a class in which your data should be store and then pass this class object to JsonUtility.ToJson method so after that you can save your data into json file.

Comment
Add comment · Show 1 · 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 dbrowndev · Feb 11, 2020 at 04:21 PM 0
Share

thank you!

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

122 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

.xml .csv .txt .json which one can be load and save on Android ? 0 Answers

JSON string to List 1 Answer

JSON Serialization and Instance IDs 0 Answers

Parseing JTokens 0 Answers

Looping through my JSON to spawn objects 1 Answer


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