This Unity base class provides a robust implementation of the Singleton pattern for MonoBehaviour-derived components. It ensures that only one instance of the component exists in the scene and that it persists across scene loads.
- Singleton Pattern: Ensures that only one instance of the component exists in the scene.
- Auto-Initialization: Automatically creates an instance if none exists.
- DontDestroyOnLoad: Keeps the instance alive across scene loads.
-
Open Unity and go to
Window>Package Manager. -
Click the
+button in the top-left corner. -
Select
Add package from git URL.... -
Enter the following URL:
https://github.com/RimuruDev/MonoSingleton.git -
Click
Addto install the package.
- Download the latest
.unitypackagefile from the Releases page. - In Unity, go to
Assets>Import Package>Custom Package.... - Select the downloaded
.unitypackagefile and import it into your project.
- Create a new class that inherits from
MonoSingleton<T>, whereTis the type of the inheriting class. - Implement your custom logic in this class.
Example:
using UnityEngine;
namespace YourNamespace
{
public class GameManager : MonoSingleton<GameManager>
{
protected override void Awake()
{
base.Awake();
// Your initialization code here :D
}
// Your custom methods here ;3
}
}This will ensure that GameManager behaves as a singleton within your project, with a single instance that persists
across scene loads.
- Email: rimuru.dev@gmail.com
- GitHub: RimuruDev
- LinkedIn: rimuru
This project is licensed under the MIT License - see the LICENSE file for details.