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
1
Question by cashtotalszero · Jun 02, 2014 at 10:39 AM · c#memorygame objects

High numbers of unique game objects - best practice?

I am quite new to Unity and C# and am looking for some general advice on how to deal with large numbers of game objects.

What my software needs to do:

  • Track 1000s+ unique game objects which represent real life (live) satellites in space.

  • Users need to be able to click into a graphical representation of each satellite to access data about them. The scene itself will be a 3D representation of space.

What is the best Unity way to store the data in about each satellite in memory which enables quick lookup access and can handle the potential high number of satellites without memory bottlenecks? Which data structure(s) would be most effective in this scenario when using Unity?

Please note that the number of satellites being tracked may vary dramatically from moment to moment at the data to be displayed will depend on live data and very high numbers may need to be processed at any given moment. Perhaps I have missed the appropriate tutorial/thread. Can anyone help point me in the right direction?

Comment
Add comment · Show 3
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 vexe · Jun 02, 2014 at 11:25 AM 0
Share

So you have... a bunch of stuff to track? how about you simply store them in an array? - if you need to dynamically add/remove satellites, consider generic lists. You can't go wrong with O(1).

avatar image CHPedersen · Jun 02, 2014 at 11:42 AM 0
Share

Generic lists do not have O(1) complexity for addition and removal. They move elements around to preserve contiguousness. But you're right that it's either that or a static array, depending on whether or not he knows how many satellites he must store beforehand. Data structures that don't bother with keeping elements contiguous, such as LinkedList, are bothersome to work with when locating single elements.

avatar image vexe · Jun 02, 2014 at 06:44 PM 0
Share

I meant O(1) for accessing elements (lookup). $$anonymous$$y bad should have said so.

1 Reply

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

Answer by CHPedersen · Jun 02, 2014 at 12:19 PM

There is no appropriate toturial/thread for something like this that I know of. :)

You should choose a native array for this, I think. But it requires that you take a decision on the upper bound of the number of elements to be stored. Even if you don't know how many satellites will be tracked at a given time, do you know what the worst case scenario is? If you know that your program will never track more than, say, 5000 satellites at a time, it will likely be more efficient to just declare an array that big to begin with. This Wikipedia-article, for example, states that about 3600 (man-made) satellites are currently in orbit, including non-functional ones. Maybe that number would be a good starting point?

It might seem a little silly at first to just declare an array big enough for all of them all at once, instead of going with a List of maybe 100 and then expand it as your need grows. But there is a very specific reason for this suggestion: Unity's garbage collector. Since we're still on Mono 2.6, Unity's Garbage Collector is unable to compact and defragment de-allocated memory so it can be returned to the OS. It will remain allocated to the process as your program runs, and just have huge holes in the process's heap memory. Therefore, Unity developers should be very careful not to constantly re-allocate large amounts of memory, because doing so will cause Unity to ask the OS for more memory for its heap as it won't be able to fit the new requirement for contiguous memory into any of the holes in its heap. Therefore, Generic Lists that contain 1000's of elements run the risk of fragmenting the heap and continually increase memory usage whenever they re-allocate their internal array to change size, because Unity's garbage collector is unable to return the previously used memory to OS.

A native array does not have this serious drawback. It is allocated once, and then just sits there for the entire lifetime of the object. You may then clear the array and re-fill it with new objects as much as you please; it will never be re-allocated unless you yourself do so with the 'new' operator.

In addition to this advantage, native arrays do not concern themselves with keeping elements sequential like Lists do. So, as long as you can keep track of the indices of your individual satellites, a native array will offer you both O(1) lookup and O(1) addition/removal, which you will definitely need if you're going to be editing this array a lot based on incoming data.

So, go with an array. :)

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 cashtotalszero · Jun 02, 2014 at 02:37 PM 0
Share

Thank you! Very helpful :)

avatar image vexe · Jun 02, 2014 at 06:47 PM 0
Share
  • great answer. @cashtotalszero if this solves it please tick it.

avatar image vexe · Jun 02, 2014 at 06:48 PM 0
Share

@CHPedersen:

So, go with an array. :)

Tell this to @Fattie LoL!

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Does variable order affect class size due to byte alignment in C#/Unity 1 Answer

How do you profile memory usage during compilation? 0 Answers

Memory game logic 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