Skip to content

Implement ResetStaticFields method for Unity instance cleanup#5554

Open
ImDreamerDev wants to merge 10 commits into
clockworklabs:masterfrom
ImDreamerDev:master
Open

Implement ResetStaticFields method for Unity instance cleanup#5554
ImDreamerDev wants to merge 10 commits into
clockworklabs:masterfrom
ImDreamerDev:master

Conversation

@ImDreamerDev

@ImDreamerDev ImDreamerDev commented Jul 15, 2026

Copy link
Copy Markdown

This PR adds support for Unity projects that have Domain Reloading disabled in Enter Play Mode Options.

Normally, Unity resets static fields when entering Play Mode, but when Domain Reloading is disabled, static data can remain from previous play sessions. This can cause unexpected behavior, such as old singleton instances, cached data, or test states carrying over.

To prevent this, static reset methods have been added to a few core SDK classes. These methods are called automatically by Unity when the subsystem is initialized, ensuring the SDK starts from a clean state each time.

Changes made:

  • Added a reset method to Log to clear the current logger instance.
  • Added a reset method to SpacetimeDBNetworkManager to clear the singleton instance.
  • Added a reset method to RemoteTableHandleBase to clear the cached serializer instance.
  • Added a reset method to DbConnectionBase to reset the testing flag.
  • Added the required Unity imports for using RuntimeInitializeOnLoadMethod.

These changes should make the SDK more reliable when used in Unity projects that disable Domain Reloading, without affecting normal Unity projects that keep it enabled.

API and ABI breaking changes

No API or ABI breaking changes.

This only changes internal static state handling and does not modify existing public APIs or behavior.

Expected complexity level and risk

Complexity: 2/5

This is a relatively small change focused on improving Unity compatibility.

The main consideration is making sure the reset methods run at the correct time during Unity initialization and that clearing static fields does not interfere with normal SDK usage. The changes are isolated to a few classes that manage static state.

Testing

Tested the changes in Unity with Domain Reloading disabled to confirm that static data is properly reset between play sessions.

Verified that:

  • The SDK initializes correctly after entering Play Mode multiple times.

  • Singleton instances do not persist unexpectedly.

  • Cached serializers and test state are cleared correctly.

  • Existing behavior remains unchanged when Domain Reloading is enabled.

  • Test normal Unity Play Mode behavior with Domain Reloading enabled.

  • Test with Unity Enter Play Mode Options with Domain Reloading disabled.

@CLAassistant

CLAassistant commented Jul 15, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Added a method to reset the static instance to prevent data persistence when entering Play Mode with Domain Reloading disabled.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Added ResetStaticFields method to Log class for static instance reset.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Added a method to reset static fields to prevent data persistence in Unity.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Added ResetStaticFields method to prevent data persistence in Unity.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>

@rekhoff rekhoff left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! A few things I noted when looking over your changes.

Comment thread sdks/csharp/src/ISpacetimeDBLogger.cs
Comment thread sdks/csharp/src/ISpacetimeDBLogger.cs Outdated
ImDreamerDev and others added 5 commits July 18, 2026 01:44
Co-authored-by: Ryan <r.ekhoff@clockworklabs.io>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants