Giter Site home page Giter Site logo

davidhenshaw / unity-scene-reference Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johannesmp/unity-scene-reference

0.0 0.0 0.0 41 KB

A SceneReference wrapper class that uses ISerializationCallbackReceiver and a custom PropertyDrawer to provide safe, user-friendly scene references in scripts.

Home Page: https://gist.github.com/JohannesMP/ec7d3f0bcf167dab3d0d3bb480e0e07b

License: MIT License

C# 100.00%

unity-scene-reference's Introduction

Disclaimer (2022)

This project is no longer being actively maintained, and should be considered archived.

If you do find it useful you are absolutely encouraged to fork it and feel free to reach out so your work can be linked to from here.

Here are some relevant projects to check out:



openupm

This repository has UPM support. You can install this plugin to your project using OpenUPM:

npm install -g openupm-cli
openupm add com.johannesmp.unityscenereference

What is this?

A SceneReference wrapper class that uses ISerializationCallbackReceiver and a custom PropertyDrawerto provide safe, user-friendly scene references in scripts.

alt text

Why is this needed?

Sooner or later Unity Developers will want to reference a Scene from script, so they can load it at runtime using the SceneManager.

We can store a string of the scene's path (as the SceneAsset documentation suggests), but that is really not ideal for production: If the scene asset is ever moved or renamed, then our stored path is broken.

Unity has already solved this problem for assets using Object references. Using AssetDatabasse you can find the path for a given asset. The problem there unfortunately is that we don't have access to AssetDatabase at runtime, since it is part of the UnityEditor Assembly.

So to be able to reliably use a scene both in editor and at runtime we need two pieces of serialized information: A reference to the SceneAsset object, and a string path that can be passed into SceneManager at runtime.

We can create our own Wrapper class that uses ISerializationCallbackReceiver to ensure that the stored path is always valid based on the specified SceneAsset Object.

This SceneReference class is an example implementation of this idea.

Key features

  • Custom PropertyDrawer that displays the current Build Settings status, including BuildIndex and convenient buttons for managing it with destructive action confirmation dialogues.
  • If (and only if) the serialized Object reference is invalid but the path is still valid (for example if someone merged incorrectly) will recover object using path.
  • Buttons collapse to smaller text if full text cannot be displayed.
  • Include detailed tooltips and respects Version Control if build settings are not checked out (tested with Perforce).
  • It's a single drop-in script. You're welcome to split the Editor-only PropertyDrawer and helpers into their own Editor scripts if you'd like, just for convenience I've put it all in one self-contained file here.

For easy runtime verification I've also provided a testing Monobehaviour that lets you view and load scenes via buttons when in playmode:


Acknowledgements

unity-scene-reference's People

Contributors

starikcetin avatar johannesmp avatar hbollon avatar der-hugo 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.