Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Discussion [Open Source] Lurking Ninja's developer's tools series: Domain Reload Off Support

Discussion in 'Assets and Asset Store' started by Lurking-Ninja, Jan 13, 2024.

  1. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    557
    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:
    Code (CSharp):
    1. using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
    2. using UnityEngine;
    3.  
    4. public partial class TestStaticOnNoDomainReload : MonoBehaviour
    5. {
    6.     private static int _number;
    7.  
    8.     private void Start()
    9.     {
    10.         Debug.Log($"Started with {_number}");
    11.         _number += 10;
    12.         Application.quitting += OnQuit;
    13.         OtherTestEvent.OnChangeSomethingStatic += OnQuit;
    14.         Debug.Log($"Ended with {_number}");
    15.     }
    16.  
    17.     private static void OnQuit() => Debug.Log("Exiting");
    18. }
    This will cause to generate this code:
    Code (CSharp):
    1. using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
    2. using UnityEngine;
    3. using System;
    4.  
    5.     public partial class TestStaticOnNoDomainReload
    6.     {
    7. #if UNITY_EDITOR
    8.         [UnityEditor.InitializeOnEnterPlayMode]
    9.         static void ApplyStaticFieldsAndEventHandlers(UnityEditor.EnterPlayModeOptions options)
    10.         {
    11.             if (!options.HasFlag(UnityEditor.EnterPlayModeOptions.DisableDomainReload)) return;
    12.             _number = default;
    13.             Application.quitting -= OnQuit;
    14.             OtherTestEvent.OnChangeSomethingStatic -= OnQuit;
    15.  
    16.         }
    17. #endif
    18.     }
    So repeatedly entering play mode will not cause the static members to change over time:

    Console.png

    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
     
    Last edited: Jan 17, 2024
    Ryiah and LooperVFX like this.
  2. LooperVFX

    LooperVFX

    Joined:
    Dec 3, 2018
    Posts:
    182
    Very cool, thanks for sharing this!
     
    Lurking-Ninja likes this.