기술 자료

Unity Engine

Download Unity

Unity Engine

AutoStaticsCleanupAttribute

Resets static variables automatically on entering or exiting Play mode with domain reload disabled.
읽는 시간 5분최근 업데이트: 7일 전

Definition

[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 with this attribute must be declared
partial
to allow the source generator to inject clean-up code.
[AutoStaticsCleanup]
resets state as follows:
  • 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,
    static int myInt = 5;
    resets to
    5
    .
  • For reference-type fields initialized with
    new
    , the initializer is evaluated again, so a new instance is created. For example,
    static MyObj obj = null;
    resets to
    null
    and
    static MyObj obj = new();
    creates a fresh
    MyObj
    when Play mode starts.
  • readonly
    static collection fields (such as
    List<T>
    ,
    Dictionary<TKey,TValue>
    ,
    HashSet<T>
    , and any type with a parameterless
    Clear
    method) are a special case: instead of replacing the instance, the existing collection is preserved and
    Clear
    is called. Non-readonly static collections are reset normally by reapplying their initializer. For
    readonly
    fields, the initializer must either be omitted, use
    new()
    , or use the exact declared type — for example,
    = new List<string>()
    .
  • Fields whose type has an accessible parameterless
    Dispose
    method are disposed before they're reset. The generator calls
    Dispose
    on the current value, then applies the reset described previously. Only non-
    readonly
    fields are disposed, because
    readonly
    collection fields use
    Clear
    instead. A
    Dispose
    method counts as accessible when the class that declares the field can call it, so a
    private
    Dispose
    on that same class is used, and so is a
    protected
    Dispose
    inherited from a base class. A
    private
    or
    protected
    Dispose
    on 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 its
    Dispose
    method to be safe to call on a default instance.
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++; }}

Inheritance

Inherited Members