Git Product home page Git Product logo

domainreloadsg's Introduction

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

  • Download the latest release from here.
  • Extract the DLL file somewhere into your Assets folder.
  • Continue below at the "Finishing" section

Build from source

  • Clone the repo
  • Open up in your favorite IDE
  • Build the solution
  • Copy the <repo path>\bin\Release\netstandard2.0\DomainReloadSupport.dll somewhere into your Assets folder

Finishing

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.

Limitations

  • The class you're augmenting has to be decorated with the partial keyword.
  • Currently there is no way of excluding static fields or methods from this service.

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
  • 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

Previous changes >

domainreloadsg's People

Contributors

lurkingninjadev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.