This package (com.orbitalnine.core) provides foundational building blocks for common Unity development patterns:
- Singleton: Create a
MonoBehaviour-based singleton by inheriting fromSingletonMonoBehaviour<T>. - Persistence: The
DontDestroyOnLoadcomponent provides a way to keep any GameObject alive across scene loads.
This package has no external dependencies and relies only on the core Unity Engine libraries.
Important: This package has only been tested on Unity version 6.1 for Editor, iOS and Android. Compatibility with other Unity versions and platforms is not guaranteed.
You can install this package in Unity's Package Manager using the "Add package from git URL" option.
- Open the Package Manager (
Window > Package Manager). - Click the
+button in the top-left corner. - Select "Add package from git URL...".
- Enter the repository's HTTPS URL (
https://github.com/shirleyman/unity-core.git) and click "Add".
- Clone or download this repository.
- Copy the
RuntimeandTests(optional) folders into your Unity project'sAssetsorPackagesdirectory.
To create your own singleton manager (e.g., a GameManager), create a new C# script and have it inherit from SingletonMonoBehaviour<T>, where T is your new class.
Example: Creating a GameManager
// Create a new file: GameManager.cs
using UnityEngine;
// Inherit from SingletonMonoBehaviour, passing your class as the type.
public class GameManager : SingletonMonoBehaviour<GameManager>
{
public void DoSomething()
{
Debug.Log("GameManager is doing something!");
}
}Example: Accessing the Singleton
You can now access the GameManager instance from any other script in your project.
using UnityEngine;
public class MyOtherScript : MonoBehaviour
{
void Start()
{
// Access the single instance of GameManager and call a public method.
GameManager.Instance.DoSomething();
}
}To make a GameObject persist across different scenes:
- Select the GameObject you want to keep in your scene hierarchy.
- In the Inspector, click "Add Component".
- Search for
DontDestroyOnLoadand add it.
This GameObject will now not be destroyed when a new scene is loaded.
This package includes a suite of tests for the SingletonMonoBehaviour and DontDestroyOnLoad components. You can run these tests using the Unity Test Runner window (Window > General > Test Runner).
This project is licensed under the terms of the license specified in the LICENSE file.
- Used in Brain It On! (iOS, Android) by Orbital Nine Games for singleton managers, as well as dependencies for other Orbital Nine packages.