Code Monkey home page Code Monkey logo

unity-scene-handling's Introduction

Unity Scene Handling

This package contains a static utility class that provides extra functionality for the loading and unloading of scenes. It is designed to provide asynchronous, additive by default scene loading, the capability to safely load the same scene multiple times, the capability to insert dependencies in the loaded scenes and allow for explicit control over when scenes are loaded or unloaded.

Reasoning

The default behavior for loading scenes in the Unity game engine is to unload all currently loaded scenes and load the requested scene synchronously. This behavior may lead to dips in frame rate and game interactivity, having scenes that are larger than necessary, having to duplicate functionality across scenes and other issues.

Installation

Add this package to your Unity project using the "Add package from git URL..." option inside the package manager. "package manager installation" Or download a copy and extract it inside your project's Assets folder.

Usage

Add a SceneRoot component to a GameObject in the root of your scene hierarchy. "example scene"

Add the namespace.

using VV.SceneHandling;

In this example, a scene is loaded by using the SceneHandler.Load method and passing in the scene name and an object containing puzzle data as parameters.

The method returns a SceneLoadHandle object that encapsulates some of the UnityEngine.AsyncOperation class functionality and allows the class responsible for loading the puzzle to keep a reference of it. SceneLoadHandle.IsDone will be true after the scene finishes loading.

public class GameManager : MonoBehaviour
{
    IEnumerator Start()
    {
        var puzzle = levels[current];
        SceneLoadHandle handle = SceneHandler.Load("PuzzleScene", puzzle);
        yield return new WaitUntil(() => handle.IsDone);
        [...]

The SceneRoot object at the root of the newly loaded scene hierarchy can access the injected object through the SceneRoot.Payload property. It is possible to extend the SceneRoot class to implement scene specific functionality.

public class PuzzleScene : SceneRoot
{
    public bool Finished { get; private set; } = false;

    IEnumerator Start()
    {
        var puzzleData = (PuzzleData)Payload;
        InstantiatePuzzle(puzzleData);
    }
    [...]
}

After the puzzle is finished, the handle can be used to explicitly unload the puzzle scene using the SceneHandler.Unload method.

public class GameManager : MonoBehaviour
{
    IEnumerator Start()
    {
        [...]
        PuzzleScene sceneRoot = (PuzzleScene)handle.Root;
        yield return new WaitUntil(() => sceneRoot.Finished);
        SceneHandler.Unload(handle);
    }
}

unity-scene-handling's People

Contributors

vimsos avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.