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 Broccoliguy04 · Feb 02, 2018 at 05:10 AM · serializationtimespan

Serializing a Timespan

So I am expanding on Roll a Ball and I was adding a high score system via a stopwatch and I found out that you can't serialize a timespan, directly at least. So I was wondering if there was a way around it. However, I could just have the code wrong. What happens is when I try to print the timespan after supposedly saving it, it just gives me 00:00:00.00. Here is the code.
My Save/Load function

 public void Load(){
         if (File.Exists (Application.persistentDataPath + "/ball.cid")) {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/ball.cid", FileMode.Open);
             Data col = (Data)bf.Deserialize (file);
             file.Close ();
             Colornum = col.Colornum;
             hs = col.hs;
             print ("Loaded");
         }
     }
     public void Save(){
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create (Application.persistentDataPath + "/ball.cid");
         Data col = new Data ();
         col.Colornum = Colornum;
         col.hs = hs;
         bf.Serialize (file, col);
         file.Close ();
         print ("Saved!");
     }
 
 }
 [Serializable]
 class Data{
     public int Colornum;
     public TimeSpan hs;
 }

My text function

 void Start () {
         string highScore = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
             SavingLoading.hi.hs.Hours, SavingLoading.hi.hs.Minutes, SavingLoading.hi.hs.Seconds,
             SavingLoading.hi.hs.Milliseconds / 10);
         score.text = highScore.ToString ();
         print (SavingLoading.hi.hs);
     }

My Other Text Function

 void Start () {
         SavingLoading.hi.mt = SavingLoading.hi.lvl1.Elapsed;
         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
             SavingLoading.hi.mt.Hours, SavingLoading.hi.mt.Minutes, SavingLoading.hi.mt.Seconds,
             SavingLoading.hi.mt.Milliseconds / 10);
         string highScore = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
             SavingLoading.hi.hs.Hours, SavingLoading.hi.hs.Minutes, SavingLoading.hi.hs.Seconds,
             SavingLoading.hi.hs.Milliseconds / 10);
         time.text = "You took: " + elapsedTime.ToString();
         hs.text = "Highs Score: " + highScore.ToString ();
         if (SavingLoading.hi.mt < SavingLoading.hi.hs) {
             SavingLoading.hi.hs = SavingLoading.hi.mt;
             print (highScore);
         }
         SavingLoading.hi.Save ();

Any help is appreciated! PS: If you need any other info, please tell me.

Comment
Add comment · Show 1
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 Bunny83 · Feb 02, 2018 at 10:53 AM 0
Share

Sorry but it's not clear when and where you actually run your Load method. Are you sure you actually load your highscore before you try to display it? Using the BinaryFormatter seems a bit overkill unless you explicitly want to store the highscore in a file that way. If you have a TimeSpan value you can just use it's "Ticks" property to get the ticks it represents. This long value can be stored and used to recreate a TimeSpan.

1 Reply

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

Answer by JimmyCushnie · Feb 02, 2018 at 05:18 AM

I think you're overengineering this. I would keep the score as an integer or float representing the number of seconds; this is easier to do math with and serialize. You can then convert it to the formatted string you want only when it is displayed to the player.

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 Bunny83 · Feb 02, 2018 at 10:48 AM 0
Share

That's what he does. The TimeSpan is just a struct that contains only a single long value representing the ticks that have passed.

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

79 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

Related Questions

How to assign a class ID at runtime? 0 Answers

Bool array to binaryformatter method 1 Answer

Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers

Serialized Fields go bye bye on Android 0 Answers

How do I save scripts as variables in the inspector? 0 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