Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


OnCodeUnloadingAttribute

Marks a static method as a callback to be invoked before code is unloaded during a code reload.
Read time 4 minutesLast updated 5 days ago

Definition

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]public sealed class OnCodeUnloadingAttribute : LifecycleAttributeBase

Remarks

Methods marked with this attribute are called before assemblies are unloaded during a code reload. This is the counterpart to OnCodeLoadedAttribute.
Use this callback to:
  • Release native plugin handles.
  • Gracefully disconnect network sessions.
  • Save editor-time state before reload.
This is a method attribute that can only be applied to
static
methods with no parameters and return type
void
.

Examples

using Unity.Scripting.LifecycleManagement;using UnityEngine;using System;using System.Runtime.InteropServices;public static partial class NativeAudioPlugin{ private static IntPtr s_PluginHandle; [DllImport("AudioPlugin")] private static extern IntPtr AudioPlugin_Initialize(); [DllImport("AudioPlugin")] private static extern void AudioPlugin_Shutdown(IntPtr handle); [OnCodeLoaded] static void Initialize() { s_PluginHandle = AudioPlugin_Initialize(); } [OnCodeUnloading] static void Shutdown() { if (s_PluginHandle != IntPtr.Zero) { AudioPlugin_Shutdown(s_PluginHandle); s_PluginHandle = IntPtr.Zero; } }}

Constructors

Constructor

Description

OnCodeUnloadingAttribute()Marks the decorated static method to be invoked before assemblies are unloaded during a code reload. Methods are called in reverse registration order.

Inheritance

Inherited Members