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 jpthek9 · Feb 28, 2015 at 06:57 PM · runtimedatadictionarycachecalculation

How to save run-time variables to dictionary?

I want to cache some calculations and save them to a dictionary (or similar data structure) during runtime. How can I do this? Thanks in advance.

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
1
Best Answer

Answer by _foa · Feb 28, 2015 at 08:27 PM

If you are using C# you can use a Dictionary data type and its method Add(Key, Value).
See: https://msdn.microsoft.com/en-GB/library/xfhwa508%28v=vs.110%29.aspx)
If you need more than one game object to access this Dictionary you can make the variable static so it is shared among game objects.
See: https://msdn.microsoft.com/en-GB/library/98f28cdx.aspx
Of course you need to attach a script with the Dictionary declaration to the game object or prefab that will use it.
If different game objects or prefabs needs to access the Dictionary you should move your Dictionary declaration to a separate static class in the Unity project Visual Studio (MonoDevelop) solution and use this class to access the Dictionary from different scripts.
I don't know how much you know C# and programming in general so if you need a more in depth explanation with an example let me know :)

I hope it helps !

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 jpthek9 · Feb 28, 2015 at 08:49 PM 0
Share

I'm intermediately experienced with C# (watched all the Unity tutorials and made a few games) but I'm not very familiar with serialization and data storage. After getting the dictionary saved in an instance of a script, how can I save that to a file to be accessed in later runs?

I'm caching all the sine calculations for cross-platform deter$$anonymous$$ism and to help the performance of my game but I'm having trouble saving these cached calculations.

avatar image _foa · Mar 01, 2015 at 04:04 PM 1
Share

You can find a good and official source of examples and documentaion right here: https://msdn.microsoft.com/en-us/library/7ay27kt9(v=vs.110).aspx
One of the most common classes used to serialize data is https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx. Be aware that using the BinaryFormatter will make your data version dependent but in this case, if you do not require your data to be human readable, the BinaryFormatter can be used without problems because the Dictionary data structure will not be changed any time soon, so you do not need to worry about versioning problems.

Hope I've been clear, let me konw :)

avatar image jpthek9 · Mar 01, 2015 at 05:46 PM 0
Share

Awesome! Thanks for leading me to the right place!

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

20 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

Related Questions

Are OnGUI calculations cached? 1 Answer

Fastest way to get all components on GameObject? 2 Answers

Export objects to a .3DS file at runtime 1 Answer

Using raycast to sonify point cloud data and geometry of shapes 0 Answers

Serializing Dictionary with Vector3 keys 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