Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Attribute to clear static fields on play start

Discussion in '2019.3 Beta' started by joshcamas, Dec 8, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,273
    Hello!

    I made a lil tool that makes disabling domain reload easier. It's essentially two attributes.

    ClearOnReloadAttribute
    Use on static fields, properties or events that you wish to reset on playmode. You can either "clear" the field (set the value to default), set it to a specified value, or make it assign itself a new instance of its type

    ExecuteOnReloadAttribute
    Use on static methods that you want to execute during a domain reload. Basically shorthand for [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)], since that is obnoxiously long.

    Here's a lil example. In this case, the instance field would be cleared, the event would be cleared, and the RunThis function would be invoked.

    Code (CSharp):
    1.  
    2. public class CharacterManager : MonoBehaviour
    3. {
    4.   //Will set value to default (null)
    5.   [ClearOnReload]
    6.   static CharacterManager instance;
    7.  
    8.   //Works on events!
    9.   [ClearOnReload]
    10.   static event Action onDoSomething;
    11.  
    12.   //Will set value to defined value (10)
    13.   [ClearOnReload(valueToAssign=10)]
    14.   static int startsAsTen;
    15.  
    16.   //Will reset value, but make it not null (creates a new instance)
    17.   [ClearOnReload(assignNewTypeInstance=true)]
    18.   static CharacterManager myNeverNullManager;
    19.  
    20.   [ExecuteOnReloadAttribute]
    21.   static void RunThis()
    22.   {
    23.       Debug.Log("Clean up here or something")
    24.   }
    25. }
    26.  
    Here's the code! I sort of feel like something along these lines should be built into the engine, but it is what it is.

    https://github.com/joshcamas/unity-domain-reload-helper

    Thanks to @JGroxz for contributing some nice features!
     
    Last edited: Aug 23, 2020
  2. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    Thanks!
     
    joshcamas likes this.
  3. alexeyzakharov

    alexeyzakharov

    Joined:
    Jul 2, 2014
    Posts:
    507
    JoNax97, _met44, kreso and 7 others like this.
  4. JGroxz

    JGroxz

    Joined:
    Dec 12, 2017
    Posts:
    12
    Hey! First of all thank you for sharing this code - you have potentially saved thousands of seconds of play mode waits for me and other people who gonna use this :)

    While adding it to my project, I have made some additions to the code:
    - ClearOnReload can now take a default value as a parameter. The value will be assigned to the field/property on reload. Works for all built-in value types and strings (anything which can be expressed as a constant).
    - Same attribute now supports initializing fields with default instance of that field type. Very useful for static lists and arrays, as those will turn null otherwise and throw NullReferences if the code expects them to be initialized (which was the case for some of my scripts).
    These two things should leave even less reasons to write separate static methods just for the sake of variables initialization.

    I created a pull request to your repo at GitHub. Please check it out and merge if you consider those changes helpful.

    Peace!
     
    SugoiDev and joshcamas like this.
  5. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,273
    Amazing additions, merged!
     
  6. StCost

    StCost

    Joined:
    Mar 1, 2017
    Posts:
    30
    I've been struggling to make it work on Singleton<T>.
    Turns out, I had to make variable/method public, to reset it
    I guess, that's the way how it supposed to work in inherited classes

    Thanks, it's quite helpful
     
  7. shanecelis

    shanecelis

    Joined:
    Mar 26, 2014
    Posts:
    22
    Cool project, Josh! I took Alexey's suggestion and forked the project to use TypeCache, and it improved performance significantly. Although it did restrict the clearable types to fields only. No properties or events. Pull request submitted.
     
    joshcamas and SugoiDev like this.
  8. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,273
    Update merged! This does mean properties and events are no longer supported, but the huge jump in speed is super worth it in my eyes. If users do want to wipe properties, events, and other values, they can use the ExecuteOnReload attribute on a method that does so.
     
    shanecelis likes this.
  9. unity_QJ7RazXzghZCzA

    unity_QJ7RazXzghZCzA

    Joined:
    Jul 9, 2021
    Posts:
    104
    Hello!
    We need to make this project Greate Again!

    Please, update it to make support of generic types like Singleton<T> : Monobehaviour. Also can it clear not only Monobehaviours but ScriptableObjects fields or even simple classes(not monobehaviours)?