Source Generator to support Domain Reload off in Unity (handle static fields and event handlers).
You have two ways to use this repository:
- Download the latest release from here.
- Extract the DLL file somewhere into your
Assetsfolder. - Continue below at the "Finishing" section
- Clone the repo
- Open up in your favorite IDE
- Build the solution
- Copy the
<repo path>\bin\Release\netstandard2.0\DomainReloadSG.dllsomewhere into yourAssetsfolder
- Follow the instructions here:
- Inside the Asset Browser, click on the .dll file to open the Plugin Inspector window.
- Go to Select platforms for plugin and disable Any Platform.
- Go to Include Platforms and disable Editor and Standalone.
- Go to Asset Labels and open the Asset Labels sub-menu.
- Create and assign a new label called RoslynAnalyzer. To do this, enter “RoslynAnalyzer” into the text input window in the Asset Labels sub-menu. This label must match exactly and is case sensitive. After you create the label for the first analyzer, The label appears in the Asset Labels sub-menu. You can click on the name of the label in the menu to assign it to other analyzers.
- The class you're augmenting has to be decorated with the
partialkeyword. - Currently there is no way of excluding classes, static fields or event handlers from this service.
- If you're using extra namespaces currently you need to explicitly write it in the code as opposed of in a
usingstatement.
using UnityEngine;
public partial class TestStaticOnNoDomainReload : MonoBehaviour
{
private static int _number;
private void Start()
{
Debug.Log($"Started with {_number}");
_number += 10;
Application.quitting += OnQuit;
Debug.Log($"Ended with {_number}");
}
private static void OnQuit() => Debug.Log("Exiting");
}The result after entering play mode twice:
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 System;
using UnityEngine;
public partial class TestStaticOnNoDomainReload
{
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
static void ApplyStaticFieldsAndEventHandlers()
{
_number = default;
Application.quitting -= OnQuit;
}
}