Home

Awesome

Domain Reload support w/ Source Generator

Source Generator to support Domain Reload off in Unity (handle static fields and event handlers).

Installation

You can choose manually installing the package or from GitHub source.

Manual install

Build from source

Finishing

Follow the instructions here:

Limitations

Exclusion

Decorate your partial class with [NoDomainReloadSupport] attribute if you want to exclude it completely.

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");
}

The result after entering play mode twice:

Console screenshot showing resetting happening.

Obviously the Edit > Project Settings > Editor > Enter Play Mode is set and the Reload Domain is not set.

The generated source code: TestStaticOnNoDomainReload_codegen.cs

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
    }

Changelog

[0.0.5] - 2024-01-12

Changed

Previous changes >