Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheFuzzyketchup · Dec 23, 2017 at 07:22 AM · serializationsavingscriptable object

How To Save User Created Custom Object

I have created a Scene that allows me to create simple cards using the UI InputFields and UI Text to create a game object called Card in the scene. What I'm trying to find out is the best way to save the cards GameObject that is created as well as the different data that it contains (Name, Type, Description, etc.)

To start I know that I will be creating the initial cards for the game in this tool, and from what I have been able to find a Scriptable Object would work for me being able to create them, but I also want the users to be able to create their own cards with the editor in the game, and it seems that you can't use Scriptable Objects the same in a deployment build as you can in the editor sessions (from what I can tell, I don't know very much about Scriptable Objects just what I could find while trying to answer this question).

alt text

examplecard.png (274.3 kB)
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
Best Answer

Answer by Larry-Dietz · Dec 24, 2017 at 05:44 AM

The way I would go about doing this would be to create a serializable class that holds all the information needed to create the card. Then when they create a card, populate an instance of the class with all the data and save a serialized copy of the class to persistent storage.

Here is the Save and Load functions I am using to do essentially this. My game has a class called SpecialOffer. These save a list of special offers to persistent storage.

    public void Save()
     {
         string dataPath = string.Format("{0}/SpecialOffers.dat", Application.persistentDataPath);
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         FileStream fileStream;
 
         try
         {
             if (File.Exists(dataPath))
             {
                 File.WriteAllText(dataPath, string.Empty);
                 fileStream = File.Open(dataPath, FileMode.Open);
             }
             else
             {
                 fileStream = File.Create(dataPath);
             }
 
             binaryFormatter.Serialize(fileStream, SpecialOfferList);
             fileStream.Close();
 
             if (Application.platform == RuntimePlatform.WebGLPlayer)
             {
                 SyncFiles();
             }
         }
         catch (Exception e)
         {
             Debug.Log("Failed to Save: " + e.Message);
         }
     }
 
     public void Load()
     {
 
         string dataPath = string.Format("{0}/SpecialOffers.dat", Application.persistentDataPath);
 
         try
         {
             if (File.Exists(dataPath))
             {
                 BinaryFormatter binaryFormatter = new BinaryFormatter();
                 FileStream fileStream = File.Open(dataPath, FileMode.Open);
 
                 SpecialOfferList = (List<SpecialOffer>)binaryFormatter.Deserialize(fileStream);
                 fileStream.Close();
             }
         }
         catch (Exception e)
         {
             Debug.Log("Failed to Load: " + e.Message);
         }
     }
 

Hope this helps, -Larry

Comment
Add comment · Show 2 · 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 TheFuzzyketchup · Dec 25, 2017 at 12:38 PM 0
Share

Thank you very much, exactly what I needed.

avatar image Larry-Dietz TheFuzzyketchup · Dec 25, 2017 at 04:13 PM 0
Share

Glad I could help!

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

77 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

Related Questions

Save/Load Animation State of Instantiated Prefabs 0 Answers

Saving changes to MeshRenderer at edit time 0 Answers

Android Saving Differently Than Engine Tests 0 Answers

Serialization Location 1 Answer

Saving class data without having lots of references? 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