Domain Reload Off Support w/ Source Generator
On GitHub
Source Generator to support Domain Reload off in Unity (handle static fields and event handlers).
Example:
using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
using UnityEngine;
public partial class TestStaticOnNoDomainReload : MonoBehaviour
{
private static int _number;
private void Start()
{
Debug.Log($"Started with {_number}");
_number += 10;
Application.quitting += OnQuit;
OtherTestEvent.OnChangeSomethingStatic += OnQuit;
Debug.Log($"Ended with {_number}");
}
private static void OnQuit() => Debug.Log("Exiting");
}
This will cause to generate this code:
using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
using UnityEngine;
using System;
public partial class TestStaticOnNoDomainReload
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnEnterPlayMode]
static void ApplyStaticFieldsAndEventHandlers(UnityEditor.EnterPlayModeOptions options)
{
if (!options.HasFlag(UnityEditor.EnterPlayModeOptions.DisableDomainReload)) return;
_number = default;
Application.quitting -= OnQuit;
OtherTestEvent.OnChangeSomethingStatic -= OnQuit;
}
#endif
}
So repeatedly entering play mode will not cause the static members to change over time:
I’m looking forward any and all feedback or if you have any idea for useful additions to this package, feel free to discuss it below, I will consider every ideas.
You can install through GitHub here: Lurking Ninja’s Domain Reload Support
Changelog
All notable changes to this project will be documented in this file.
[0.0.5] - 2024-01-12 - Released (Download)
Changed
- Moved into a real Unity project
- Unity Test added to detect if static variable resets upon entering play mode while the Domain Reload is off
[0.0.4] - 2023-11-20
Changed
- Moved onto using [InitializeOnEnterPlayMode] in UnityEditor only
- Only execute if Domain Reload is OFF
[0.0.3] - 2023-11-20
Changed
- Limitation of missing using directives was removed
[0.0.2] - 2023-11-06
Added
- This CHANGELOG
- [NoDomainReloadSupport] added to exclude classes
- Namespace black list added
[0.0.1] - 2023-11-05
Added
- Initial release with existing code