AutoStaticsCleanupAttribute
Resets static variables automatically on entering or exiting Play mode with domain reload disabled.
Read time 5 minutesLast updated 6 days ago
Definition
- Type: Class
- Namespace: Unity.Scripting.LifecycleManagement
- Assembly: Unity.Scripting
- Inherits from: Attribute
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event, AllowMultiple = true)]public sealed class AutoStaticsCleanupAttribute : Attribute
Remarks
This attribute can be applied to classes, structs, fields, properties, and events. Applying it to a class or struct ensures all static members of the class or struct are automatically reset. Any class or struct that has this attribute, or that has a member with this attribute, must be declared , along with all its containing types, to allow the source generator to inject clean-up code. Otherwise the analyzer reports and the source generator generates no clean-up code for that type. If the analyzer is disabled, the compiler reports instead.
partialUAL0012CS0260[AutoStaticsCleanup]- Static fields are restored by reapplying their field initializer. If no initializer is present, the field is reset to its C# default value. For example, resets to
static int myInt = 5;.5 - For reference-type fields initialized with , the initializer is evaluated again, so a new instance is created. For example,
newresets tostatic MyObj obj = null;andnullcreates a freshstatic MyObj obj = new();when Play mode starts.MyObj - static collection fields (such as
readonly,List<T>,Dictionary<TKey,TValue>, and any type with a parameterlessHashSet<T>method) are a special case: instead of replacing the instance, the existing collection is preserved andClearis called. Non-readonly static collections are reset normally by reapplying their initializer. ForClearfields, the initializer must either be omitted, usereadonly, or use the exact declared type — for example,new().= new List<string>() - Fields whose type has an accessible parameterless method are disposed before they're reset. The generator calls
Disposeon the current value, then applies the reset described previously. Only non-Disposefields are disposed, becausereadonlycollection fields usereadonlyinstead. AClearmethod counts as accessible when the class that declares the field can call it, so aDisposeprivateon that same class is used, and so is aDisposeprotectedinherited from a base class. ADisposeorprivateprotectedon an unrelated type is ignored, and the field is only reset. A struct field is disposed even when it still holds its default value, so write itsDisposemethod to be safe to call on a default instance.Dispose
For more information, refer to Enter Play mode with domain reload disabled in the manual.
Examples
using Unity.Scripting.LifecycleManagement;using UnityEngine;public partial class MyCounterClass : MonoBehaviour{ [AutoStaticsCleanup] public static int cleanedUpCounter = 0; void Start() { Debug.Log(cleanedUpCounter); // Counter value is reset each time entering Play Mode cleanedUpCounter++; }}