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 cregox · Nov 06, 2013 at 06:04 PM · save scene

Optimal way to save minimal serialized scene status, at runtime

My usage case is this: a home scene with menus and submenus. After leaving it to a brand new scene, everything gets destroyed. When getting back to the home scene, everything is recreated. I wanted to return to the same submenu, thus I need to save its status some how.

The status isn't all that complex in the end. There are a few (NGUI) panels and only one is active at a time. I just need, basically, to remember the last one. Basically. So this doesn't need to be a complex scene serialization. I think. And it is in runtime, nothing to do with the editor!

I'm currently resolving this, using the gameobject's names. But they need to be unique and I use a *GameObject HardFind* (I hate how unity answers can't properly link to an answer!), which will search through all objects including disabled ones. So, that's all far from optimal.

Any better ideas on how to approach the issue? Any better solutions to this approach?

Comment
Add comment · Show 4
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 TheValar · Nov 06, 2013 at 06:34 PM 0
Share

would it be possible to just deactivate the etire menu ins$$anonymous$$d of destroying it? If you put all of your NGUI panels under a single parent object then ins$$anonymous$$d of destroying the menu you could use gameObject.setActive(false) on the parent menu object. Then when you reactivate it it should be in the exact same state/location/everything that it was before.

I don't use NGUI so I'm not sure if this method would screw something up.

avatar image TheValar · Nov 06, 2013 at 06:36 PM 0
Share

oops just re-read the question and realized that you are going to a new scene. So I guess you could stilldo what I just said but make the menu persistent across scenes. I'll admit this seems like a hacked solution (hence the comment vs. answer lol)

avatar image cregox · Nov 06, 2013 at 06:50 PM 0
Share

@TheValar yeah, but that's far from $$anonymous$$imal, isn't it? :P

avatar image TheValar · Nov 06, 2013 at 06:57 PM 0
Share

well it will add a bit of clutter to your object hierarchy and the menu will remain in memory so yes not the most elegant solution. It does however have the advantage of being extremely simple to implement :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Nov 06, 2013 at 06:36 PM

Since unity doesn't have simple built in functionality to save (serialize) a scene, what you need to do is save all the data that you need to reproduce the current state in persistent storage (in our case persistent means something that is kept between scenes, unless you want it to remain between games and then you'd need to store it in the disk).

The easiest way in unity to do that, is store all the data you need inside a script attached to a GameObject, and use DontDestroyOnLoad to make sure that game object persists between loading scenes.

Then whenever the scene is loaded, you need to reproduce the last state using the information in that GameObject. In your case you might need to keep in the GameObject which submenu was open, and maybe any values that the user set in the menus. Then when the scene is loaded, read which submenu should be displayed from the object, and display it. Make sure that the same object puts in default values (the home menu I guess) in it's Start(), which will only be called the first time you load the menu, because in subsequent times it will not be re-created.

Comment
Add comment · Show 7 · 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 cregox · Nov 06, 2013 at 06:47 PM 0
Share

what's the advantage of using a hidden indestructible gameobject over creating a non-monobehaviour serialized variable (i.e. my current approach)?

avatar image Jamora · Nov 06, 2013 at 06:48 PM 1
Share

Ins$$anonymous$$d of a GameObject set to DontDestroyOnLoad, an easier way (at least causing less clutter in the hierarchy) would be to use a static class to remember which menu window was open and on reloading the menu scene, set the active menu to that.

EDIT: Any GameObject takes up memory, not only is it an object itself, but has a Transform class as well (plus any other memory it might reserve internally). While not a whole lot of memory, it's still more than a single static class.

The obvious advantage is that a GameObject can receive the Unity callbacks (Start, Update etc.)

avatar image Tomer-Barkan · Nov 06, 2013 at 06:51 PM 0
Share

Static class works as well, and so does a serializable variable, but then you'll have to serialize it and keep it somewhere (disk?). The DontDestroyOnLoad is just a way to keep the data in memory, ins$$anonymous$$d of writing to disk. Static class is a bit cleaner, as @Jamora said (thumbed up), though a bit less 'Unity' way of doing things. Your choice, there are many options.

avatar image cregox · Nov 06, 2013 at 06:57 PM 0
Share

I think static classes are a very bad choice. I'm currently using singletons but I'll soon change to a **Singleton Toolbox** ins$$anonymous$$d.

avatar image TheValar · Nov 06, 2013 at 06:59 PM 0
Share

it seems the question has been moved (sort of) to here http://answers.unity3d.com/questions/570319/dynamically-keep-game-object-reference-after-destr.html

Should this question still be considered open since it has been reiterated?

Show more comments

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

18 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

Related Questions

Unity keeps crashing when I try to save a scene 0 Answers

How to specify something not to be saved in a scene? 1 Answer

Unity export as OpenDrive 0 Answers

iPhone saving form data - how and where? 1 Answer

How to automatically edit and build a project by script? 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