Code Monkey home page Code Monkey logo

scenestack's Introduction

SceneStack

SceneStack is a multi-scene management system with URP CameraStack support.

Features

  • Multi-scene management
  • URP Camera Stacking support
    • Find and add all overlay cameras across scene to the base camera.
    • Removes annoying cross-Scene reference warnings by referencing overlay cameras from another scene.

Usage

UI Scene

Using SceneStack, UI camera & canvas can be managed in separate scenes.
image

You can create a UI scene by these steps.

  1. Create a Scene.
  2. Add a UI camera with Render Type - Overlay and Culling Mask - UI.
  3. Add a canvas with Screen Space - Camera and set Render Camera to your UI camera.

For more information of this usage, please refer to the UISceneSample sample project in the package’s details panel.

Guide

Getting started

0. Prerequisites

SceneStack requires Unity 2022.x+ and URP with camera stack support.

1. Install from a Git URL

Add the following line to the dependencies section of your project's manifest.json file.

"com.malcha.scenestack": "git+https://github.com/GeonhanLee/SceneStack.git?path=/Packages/SceneStack"

2. Namespace

Add using Malcha.SceneStack;

SceneStackSO

You can create your own SceneStack with SceneStackSO in the editor.

Create SceneStackSO using the menu Assets > Create > SceneStack > Create SceneStack.
image
Assign your scene to the base scene field in the inspector.
You can also add overlay scenes as a stack.
image

SceneStackSO gives you warning in the inspector if base scene is missing or some scenes in the SceneStack are not in build.
image

Configure SceneStack in runtime

You can clone SceneStack from your SceneStackSO.

public SceneStack ExampleCloneSceneStack(SceneStackSO so)
{
  return so.CloneSceneStack();
}

or you can construct your own SceneStack without SceneStackSO.

SceneStack stack = new SceneStack("BaseScene");
// stack.baseScene = new SceneData("BaseScene"); is also ok.
stack.overlayScenes = new List<SceneData>
{
  new SceneData("Assets/SampleScenes/UIOverlaySceneA.unity"),
  new SceneData("UIOverlaySceneB"),
  new SceneData("SampleScenes/UIOverlaySceneC")
};

You can fill the class SceneData's constructor with the name or path of the scene as SceneManager.LoadScene.

CameraStack

Overlay cameras in scenes included in the SceneStack will be added to base camera when SceneStack is loaded.
image
The order of cameras in camera stack is equivalent to the order of belonged scene in the SceneStack.
image

Open SceneStack in editor mode

You can open your SceneStack in editor mode by clicking the button on SceneStackSO,
image

or right click the SceneStackSO in the project window and select Open Scene Stack menu.
image

Load SceneStack in runtime

You can load SceneStack using a static class, SceneStackLoader in runtime.

public void ExampleLoadSceneStackSO(SceneStackSO so)
{
  SceneStackLoader.LoadSceneStack(so);
}
public void ExampleLoadSceneStack(SceneStack ss)
{
  SceneStackLoader.LoadSceneStack(ss);
}

SceneStackCanvasSorter

The raycast order of canvas across multiple scenes with same sorting order is not guranteed.
To fix this, SceneStackCanvasSorter sets the sortingOrder of a canvas to the index of a scene which belongs to.

Add SceneStackCanvasSorter component to your canvas object.
image

SceneStackCameraSorter

Camera is sorted in the camera stack with an index of its belonged scene.
If you want to sort the cameras in the same scene, use the SceneStackCameraSorter component.

Add SceneStackCameraSorter component to your Camera object and modify Sorting Order in the inspector window. The lower sorting order is rendered first.
image

Internal Operations

SceneStackSOManager

SceneStackSOManager Reserializes all SceneStackSO by ReserializeAllSceneStackSO().

The method is called when below occurs.

  1. ExitingEditMode
  2. OnPreprocessBuild
  3. OnPostprocessAllAssets : when SceneAsset is imported / deleted / moved

ReserializeAllSceneStackSO() automatically saves dirtied SceneAssetSO when called.
This is because AssetDatabase.ForceReserializeAssets() serializes before the modification is applied.

SceneStackWarningSuppressor

SceneStackWarningSuppressor removes annoying warnings on the editor by removing cross scene references when entering/exiting playmode.

It also selects and deselects camera when entering/exiting playmode to remove the error message below.
https://forum.unity.com/threads/indexoutofrangeexception-in-urp.1306230

Limits & To-Do

The list below is not currently supported.

  • Multiple base camera (e.g. split screen, render texture)
  • Async scene loading

Credits & Similar Projects

SceneStack is heavily inspired by these projects.

  • Eflatun.SceneReference
    • Unlike Eflatun.SceneReference, SceneStack doesn't generate a Scene GUID to Path Map. Instead, SceneStack reserializes all SceneStackSO.
    • SceneStack doesn't provide SceneReference API to user.
  • CarterGames/MultiScene

License

This project is licensed under the terms of the MIT License. Refer to the LICENSE.md file.

scenestack's People

Contributors

geonhanlee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.