Skip to content

Unity package for editor that loads main (boot, master, loader, etc.) scene before entering play mode.

License

Notifications You must be signed in to change notification settings

EmreDogann/Unity-Editor-Scene-Bootstrapper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Editor Scene Bootstrapper

Editor-only tool intended to streamline your workflow when using Persistent/initial/loader/boot/bootstrap (you name it) scenes.

Features:

  • Load specificed scenes before any other when entering playmode.
  • Persist hierarchy state(selected and expanded objects) on entering/exiting playmode.
  • Convinient settings UI located in ProjectSettings window.
  • UPM support, simple install.
  • Highly extendable for your project.

Dependencies:

Notes:

  • Does not yet support the new UITK-based hierarchy introduced in Unity 6.3 LTS.
  • This is Editor-only, will not build into a runtime build.

image

load.single.scene.mp4
load.multiple.scenes.mp4

Installation

Install via git url by adding this entry into your manifest.json

"com.emreedev.scenebootstrapper": "https://github.com/EmreDogann/Unity-Editor-Scene-Bootstrapper.git#upm"

Or using the PackageManager window:

  1. copy this link https://github.com/EmreDogann/Unity-Editor-Scene-Bootstrapper.git#upm,
  2. open PackageManager window,
  3. click + button in top-left corner,
  4. select Add package from git URL,
  5. in appeared field paste the link you copied before (https://github.com/EmreDogann/Unity-Editor-Scene-Bootstrapper.git#upm).

How it works

After installing this package it will create Assets/SceneBootstrapperSettings.asset. Plugin settings can be changed by selecting this asset or by using ProjectSettings window.

Scene Bootstrapper works upon playmode changes only:

  • SceneProvider is the system that will determine which scene to return when the plugin asks for the "bootstrap" scene.
  • SceneLoadedHandler decides what to do after the bootstrap scene has loaded.
  • PlaymodeExitHandler decides what to do after exiting playmode.
  • On Playmode Enter: SceneProvider asks for the bootstrap scene and sets EditorSceneManager.playModeStartScene(docs) with specified scene. This will load the scene before any others.
    • When the bootstrap scene is loaded, SceneLoadedHandler is notified with additional information about preveiously opened scenes.
  • On Playmode Exit: SceneProvider notifies PlaymodeExitHandler.
  • You also have the option to save and restore hierarchy settings between playmode sessions.

Available Defaults

Scene Providers:

  • First Scene In Build Settings: Will always bootstrap the scene listed as first (index 0) in the build settings options.
  • Specified Scene Asset: Allows you to pick from the project browser any scene asset.
  • Or, a custom provider (see Extending section below).

Scene Loaded Handlers:

  • Load All Loaded Scenes (Additive): Loads all scenes that were loaded in the hierarchy before entering playmode.
  • Load All Loaded Scenes: Similar to Load All Loaded Scenes (Additive), but will load them non-additively. Loads the active scene first.
  • Load Active Scene: Loads only the active scene in the hierarchy.
  • Load Active Scene (Additive): Similar to Load Active Scene, but additively loads only the active scene in the hierarchy.
  • Delegate To In Scene Implementations: Will find and call all scene loaded handlers in the active scene.

Playmode Exit Handlers:

  • Restore Scene Manager Setup: Does nothing. Intended as a blank slate you can use to extend functionality from here.

Extending

For now, there are 3 ways to extend:

  • SceneProvider
  • SceneLoadedHandler
  • IPlaymodeExitHandler

Main thing to remember - all extended scripts should be in editor assemblies! e.g. under Editor folder or within editor .asmdef

ISceneProvider

Responsible for providing the bootstrap scene, that will be loaded first when entering playmode:

public interface ISceneProvider
{
    SceneAsset Get();
}

An example of implementing your own is shown in the sample code (Sample_SceneProvider.cs).

After that you could find your option in settings's dropdown: image

You could also write a custom property drawer if you'd like:

[CustomPropertyDrawer(typeof(Sample_SceneProvider))]
public class Sample_SceneProviderPropertyDrawer : PropertyDrawer
{
    private const int FieldsCount = 4;
    private const int FieldHeightSelf = 18;
    private const int FieldHeightTotal = 20;


    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return FieldHeightTotal * FieldsCount;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.indentLevel++;

        position.height = FieldHeightSelf;
        GUI.enabled = false;
        EditorGUI.LabelField(position, "Custom description");
        GUI.enabled = true;
        position.y += FieldHeightTotal;
        EditorGUI.PropertyField(position, property.FindPropertyRelative("_setting1"));
        position.y += FieldHeightTotal;
        EditorGUI.PropertyField(position, property.FindPropertyRelative("_setting2"));
        position.y += FieldHeightTotal;
        EditorGUI.PropertyField(position, property.FindPropertyRelative("_setting3"));

        EditorGUI.indentLevel--;
    }
}

image

ISceneLoadedHandler

The same as above, just implement this interface and you are good to go.

However, you could also create an in-scnene handler and combine with the Delegate To In Scene Implementations scene provider.

// This script should not be present in builds!
#if UNITY_EDITOR
using System.Collections;
using EmreeDev.SceneBootstrapper;
using EmreeDev.SceneBootstrapper.SceneLoadedHandlers;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

public class InGameSceneLoadedHandler : MonoBehaviour, ISceneLoadedHandler
{
    public void OnSceneLoaded(SceneBootstrapperData bootstrapperData)
    {
        Debug.Log("OnSceneLoaded! Now decide what to do with bootstrapperData.SceneSetups...");
        StartCoroutine(LoadDesiredScenes(bootstrapperData));
    }

    private IEnumerator LoadDesiredScenes(SceneBootstrapperData bootstrapperData)
    {
        yield return new WaitForSeconds(1f);
        foreach (SceneSetup sceneSetup in bootstrapperData.SceneSetups)
        {
            SceneManager.LoadScene(sceneSetup.path, LoadSceneMode.Additive);
        }
    }
}
#endif

IPlaymodeExitedHandler

Look at the default empty implementation RestoreSceneManagerSetup.

About

Unity package for editor that loads main (boot, master, loader, etc.) scene before entering play mode.

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • C# 100.0%