Advertisement
zORg_alex

Assembly Reload safe initialization

Apr 20th, 2024
728
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.54 KB | Source Code | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public static class MonobehaviourExtensions
  5. {
  6.     /// <summary>
  7.     /// Use <code>
  8.     /// private void Start() => Initialize();
  9.     /// private void OnEnable() => this.OnAssemblyReload(Initialize);</code>
  10.     /// </summary>
  11.     /// <param name="mb"></param>
  12.     /// <param name="action"></param>
  13.     public static void OnAssemblyReload(this MonoBehaviour mb, Action action)
  14.     {
  15. #if UNITY_EDITOR
  16.         UnityEditor.AssemblyReloadEvents.afterAssemblyReload += Handler;
  17. #endif
  18.         void Handler() => action();
  19.     }
  20. }
  21.  
Advertisement
Comments
  • zORg_alex
    22 hours
    # XML 0.58 KB | 0 0
    1. Visual Studio snippet:
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    4.   <CodeSnippet Format="1.0.0">
    5.     <Header>
    6.       <Title>Initialize OnAssemblyReload</Title>
    7.       <Shortcut>onassemblyreload</Shortcut>
    8.     </Header>
    9.     <Snippet>
    10.       <Code Language="csharp" Delimiter="$">
    11.           <![CDATA[private void Start() => Initialize();
    12. private void OnEnable() => this.OnAssemblyReload(Initialize);
    13.  
    14. private void Initialize()
    15. {
    16.     $selected$$end$
    17. }$end$]]></Code>
    18.     </Snippet>
    19.   </CodeSnippet>
    20. </CodeSnippets>
Add Comment
Please, Sign In to add comment
Advertisement