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 jedd.ahyoung · Aug 13, 2014 at 03:04 AM · best practices

Best practices for Unity GameManager object?

I'm working on some solutions and I'm attempting to have a GameManager class for all of my games. This should:

  1. Persist across levels/scenes.

  2. Be serializable and visible in the Unity Editor.

  3. Be scene-agnostic - meaning, I can start the game at any scene for debugging purposes.

I've been able to accomplish #1 with DontDestroyOnLoad. However, I'm having conflicts between #2 and #3.

If I want #2 - "the object should be serializable and visible in the Unity Editor" - I have to derive from MonoBehavior, or ScriptableObject. The issue with Monobehavior is that it requires a gameObject, meaning that there must be an instance in a scene somewhere. ScriptableObjects, unfortunately, are similar; while they don't have to be attached to a gameobject, they must be referenced from an active object (as a property) in order to be used.

If I want #3 - starting the game from any scene - I can make the class a singleton, or a static class; this means I can instantiate the object in code without having to have a gameObject in the scene, but then I run into configuration issues. Creating the class from code means that Unity has no way to serialize data in the editor (unless I'm missing something fundamental).

I attempted to use AssetDatabase.LoadAssetAtPath, thinking that I could generate my Singleton by loading my GameManager scriptableObject from disk, but AssetDatabase is in the UnityEditor namespace. It won't work in a build.

 public class _GameManager : ScriptableObject {

 private static _GameManager _instance;
 public static _GameManager getInstance() {
     if (_instance == null) {
         Debug.Log ("GAMEMANAGER INSTANCE NULL");
         // _instance = ScriptableObject.CreateInstance<_GameManager>();
         _instance = AssetDatabase.LoadAssetAtPath("path/to/gamemanager");
     }
     return _instance;
 }

What's best practice for this? Do I need to create a GameManager prefab and instantiate that when the game runs? That would allow me to satisfy #2, and maybe #3, but it seems....incorrect, somehow, since a prefab is supposed to be a template.

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 rutter · Aug 13, 2014 at 03:25 AM 0
Share

Given the needs you're describing, you might try the prefab solution and see how it goes. You can make that prefab a "resource" and load it with Resources.Load, the first time it's needed.

One handy thing with $$anonymous$$onoBehaviour is that you'll get Update calls, if those are useful for your manager.

I agree it's not a perfect solution, but it seems to be the best I've seen.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brunoleos · Dec 02, 2016 at 06:30 PM

Now with multi-scene editing being better supported, I'm using a main scene that is always loaded and have the persistent managers, and the other scenes are loaded addictively.

Comment
Add comment · 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

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

Best pracices: Object communication without references 2 Answers

Best Practices: Compiling for iOS 0 Answers

How to Make Things Happen the Right Way 1 Answer

Best Practice: 3D GUI or Built-in uGUI? 2 Answers

Best practices for editing large levels 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