### Build and Test Commands Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Commands to build the project, run the test suite, and execute benchmarks. Use the filter flag to target specific tests or benchmark cases. ```bash # Release build — produces the DLL you drop into Unity dotnet build AutoStaticsCleanup/AutoStaticsCleanup.csproj -c Release # Build the whole solution (generator + tests + benchmarks) dotnet build AutoStaticsCleanup.sln # Run the test suite dotnet test AutoStaticsCleanup.Tests/AutoStaticsCleanup.Tests.csproj # Run a single test dotnet test AutoStaticsCleanup.Tests/AutoStaticsCleanup.Tests.csproj \ --filter "FullyQualifiedName~TestMethodName" # Generator benchmarks (cold + incremental cases, allocation breakdown) dotnet run -c Release --project AutoStaticsCleanup.Benchmarks # Filter to one case: dotnet run -c Release --project AutoStaticsCleanup.Benchmarks -- --filter "*ColdRun*" # Quick smoke job (fewer iterations): dotnet run -c Release --project AutoStaticsCleanup.Benchmarks -- --job short ``` -------------------------------- ### Verify DLL attestation with GitHub CLI Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Use the GitHub CLI to verify that the downloaded DLL was built by the repository's CI workflow. A successful verification confirms the asset's provenance. ```bash gh attestation verify AutoStaticsCleanup.dll \ --repo alexisstrat/unity-auto-statics-cleanup-generator ``` -------------------------------- ### Define DelegateAutoCleanup.cs Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Provides a registration wrapper for cleanup methods. The constructor self-registers into a static list, and cleanup methods are registered via CreateForPlayMode. ```csharp #if !UNITY_6000_5_OR_NEWER using System; using System.Collections.Generic; namespace UnityEngine { public sealed class DelegateAutoCleanup { private static readonly List Instances = new(); private readonly Action _cleanup; private readonly string _ownerDescription; private DelegateAutoCleanup(Action cleanup, string ownerDescription) { _cleanup = cleanup ?? throw new ArgumentNullException(nameof(cleanup)); _ownerDescription = ownerDescription; Instances.Add(this); } public static DelegateAutoCleanup CreateForPlayMode(Action cleanup, string ownerDescription = "") => new(cleanup, ownerDescription); public static IEnumerable RegisteredInstances => Instances; public void Cleanup() => _cleanup(); public override string ToString() => _ownerDescription; } } #endif ``` -------------------------------- ### Editor Hook for Cleanup Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md A central editor hook using InitializeOnLoad triggers cleanup on play-mode transitions. ```csharp [InitializeOnLoad] ``` -------------------------------- ### Type-level opt-in with selective opt-out Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Apply AutoStaticsCleanup to the entire class to include all static members, and use NoAutoStaticsCleanup to exclude specific fields. ```csharp [AutoStaticsCleanup] public static partial class GameCache { public static int FrameCount; public static Dictionary Counters = new(); [NoAutoStaticsCleanup] public static int PersistAcrossPlay; } ``` -------------------------------- ### Generated File Conditional Compilation Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Generated files are wrapped in a preprocessor directive to ensure compatibility with Unity 6.5+. ```csharp #if !UNITY_6000_5_OR_NEWER ``` -------------------------------- ### Member-level opt-in for static state Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Use the AutoStaticsCleanup attribute on individual static members within a partial class to include them in the cleanup process. ```csharp using Unity.Scripting.LifecycleManagement; using System.Collections.Generic; public static partial class GameCache { [AutoStaticsCleanup] public static int FrameCount = 0; [AutoStaticsCleanup] public static List Loaded = new(); [AutoStaticsCleanup] public static event System.Action OnReset; } ``` -------------------------------- ### Generic base class cleanup registration Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Apply AutoStaticsCleanup to static members in a generic base class. Each closed instantiation registers automatically when its statics are first initialized. ```csharp public partial class Singleton : MonoBehaviour where T : MonoBehaviour { [AutoStaticsCleanup] private static T _instance; public static T Instance => _instance ??= FindFirstObjectByType(); } public class PlayerManager : Singleton { } public class EnemyManager : Singleton { } ``` -------------------------------- ### Define AutoStaticsCleanupRegistrar.cs Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md An editor-only hook that listens for play mode state changes to trigger cleanup. This file must be placed inside an Editor folder. ```csharp #if UNITY_EDITOR && !UNITY_6000_5_OR_NEWER using System; using System.Linq; using UnityEditor; using UnityEngine; namespace AutoStaticsCleanup { [InitializeOnLoad] internal static class AutoStaticsCleanupRegistrar { static AutoStaticsCleanupRegistrar() { EditorApplication.playModeStateChanged -= OnChange; EditorApplication.playModeStateChanged += OnChange; } private static void OnChange(PlayModeStateChange change) { if (change != PlayModeStateChange.ExitingEditMode && change != PlayModeStateChange.EnteredEditMode) return; var snapshot = DelegateAutoCleanup.RegisteredInstances.ToArray(); foreach (var c in snapshot) { try { c.Cleanup(); } catch (Exception e) { Debug.LogError("Failed to cleanup " + c + ": " + e.Message); } } } } } #endif ``` -------------------------------- ### Per-type Emission Registration Field Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md The generator emits a static readonly field to register the cleanup delegate for a specific type. ```csharp static readonly UnityEngine.DelegateAutoCleanup __autoCleanup_UnityEngine_PlayModeScope_Both = UnityEngine.DelegateAutoCleanup.CreateForPlayMode(__AutoStaticsCleanup_…, "Namespace.TypeName"); ``` -------------------------------- ### Readonly collection cleanup with Clear() Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md The generator handles static readonly collections by calling Clear() and re-adding initializer elements to maintain reference stability. This applies to standard collections like Dictionary and List, as well as user-defined wrappers with a parameterless Clear() method. ```csharp [AutoStaticsCleanup] public static readonly Dictionary Weights = new() { { "a", 1 } }; // generated cleanup: // Weights.Clear(); // Weights["a"] = 1; ``` -------------------------------- ### Attribute Detection via IIncrementalGenerator Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md The generator uses ForAttributeWithMetadataName to identify types marked with the AutoStaticsCleanupAttribute. ```csharp IIncrementalGenerator.ForAttributeWithMetadataName("Unity.Scripting.LifecycleManagement.AutoStaticsCleanupAttribute", …) ``` -------------------------------- ### Define NoAutoStaticsCleanupAttribute.cs Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Defines the attribute used to explicitly exclude members from automatic cleanup. ```csharp #if !UNITY_6000_5_OR_NEWER using System; namespace Unity.Scripting.LifecycleManagement { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, AllowMultiple = false)] public class NoAutoStaticsCleanupAttribute : Attribute { } } #endif ``` -------------------------------- ### Define AutoStaticsCleanupAttribute.cs Source: https://github.com/alexisstrat/unity-auto-statics-cleanup-generator/blob/main/README.md Defines the attribute used to mark classes, structs, fields, properties, or events for automatic cleanup. This must be placed in the specified namespace. ```csharp #if !UNITY_6000_5_OR_NEWER using System; namespace Unity.Scripting.LifecycleManagement { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, AllowMultiple = true)] public class AutoStaticsCleanupAttribute : Attribute { } } #endif ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.