Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

OnCodeUnloadingAttribute

class in Unity.Scripting.LifecycleManagement

/

Inherits from:Unity.Scripting.LifecycleManagement.LifecycleAttributeBase

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Marks a static method as a callback to be invoked before code is unloaded during a code reload.

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.

Additional resources: OnCodeLoadedAttribute, OnCodeDeinitializingAttribute

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; } } }

Inherited Members