Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding scenes in Unity3d

I have some confusion with scenes in Unity3d and I was not able to find any resources about them.

  1. When should scenes be used? For example in a platformer would every level have to be a different scene? Would the main menu be a scene?
  2. Can one overlay scenes?
  3. How do assets work between scenes? Are they attached to each individual scene and have to be reloaded every time. Can one specify when an asset is no longer needed?
  4. How does one send data between scenes/interface between scenes? I understand that this is a broad topic, but I didn't want to spam with multiple questions.
like image 347
user2853108 Avatar asked Jan 04 '14 04:01

user2853108


People also ask

How do scenes work in Unity?

Scenes contain the objects of your game. They can be used to create a main menu, individual levels, and anything else. Think of each unique Scene file as a unique level. In each Scene, you will place your environments, obstacles, and decorations, essentially designing and building your game in pieces.

How do I navigate scenes in Unity?

Use Flythrough mode to navigate the Scene View by flying around in first-person, similar to how you would navigate in many games. Click and hold the right mouse button. Move the view around using the mouse, the WASD keys to move left/right/forward/backward, and the Q and E keys to move up and down.

Are scenes levels in Unity?

Scenes in Unity can be thought of as unique levels or even separate screens like a main menu. In the previous article, we created a Game Over screen by simply displaying text over the existing Game Scene. However, to restart a game from a game over state, using Unity's Scene Management is necessary.

How do I view a scene in Unity?

Scene View NavigationHold the right mouse button to enter Flythrough mode. This turns your mouse and WASD keys (plus Q and E for up and down) into quick first-person view navigation. Select any GameObject and press the F key. This will center the Scene View and pivot point on the selection.


1 Answers

When should scenes be used? For example in a platformer would every level have to be a different scene? Would the main menu be a scene?

There are no general rules about that. In theory you may have just one scene for the whole game. How you organize your scenes is entirely up to you and often depends on the type of game you are creating.

I think that there are at least 3 features to be considered of using scenes:

  • they are a logical container for all pre-instantiated objects that might be useful to divide your game into multiple levels/sections.
  • You can serialize cross references between GameObjects and Components inside a scene (if GO A needs a ref to GO B, and they belong to the same scene, the reference can be serialized and you no longer need to find the referenced object at runtime)
  • When you load (not in an additive way) another scene, the resources already loaded into memory are automatically released

Can one overlay scenes?

Yes you can using LoadAdditive. Unfortunately, once 2 scenes are both loaded into memory there is no automatic way of distinguish objects belonging to one or the other. So if you load additive a second level environment, it's up to you to keep track of the previous environment and explicitly destroy it if you need to.

How do assets work between scenes? Are they attached to each individual scene and have to be reloaded every time. Can one specify when an asset is no longer needed?

As defaults every GameObject of a scene will be destroyed once the new scene is loaded (unless you use an additive scene loading). A way to make a GameObject survive across scenes is to mark it using DontDestroyOnLoad.

If you need to share a particular "configuration" of a GameObject, you can store it as a prefab, and reference it across scenes (but remember that once in a scene it's a prefab instance, so the GO shares with the prefab the initial serialized and not overriden properties, but 2 instances of the same prefab are different objects).

How does one send data between scenes/interface between scenes?

Several ways, depending on what kind of persistent data you want to share.

  • For a particular GameObject instance let the object survive using DontDestroyOnLoad.
  • If you have some configuration data that doesn't need to be attached to a specific GameObject you can consider storing a ScriptableObject inside the AssetDatabase and reference it.
  • If you have data that must persist across different game sessions you can consider storing them into PlayerPrefs.

There are 2 other ways that I don't like, but just to cite them:

  • Using a static field can sometimes help you in doing that, but it has several problems from my point of view
  • Save and load from disk (could be useful in several situations, but often it's a platform dependent way and you can have some trouble especially on different mobile platforms)

This is a broad topic btw, I hope this answer can be a quite decent overview.

like image 112
Heisenbug Avatar answered Sep 28 '22 17:09

Heisenbug