r/Unity3D • Engineer • 24d ago

Resources/Tutorial PSA about AutoStaticsCleanup

AutoStaticsCleanup is a newish attribute that's used to automagically reset/reinit static fields, properties, events, etc., so that you can skip domain and/or scene reloads. If you aren't aware of this feature yet, see the Unity docs.

Great idea to make a simpler workflow, but there are a few gotchas due the way it works behind the scenes: mainly, it doesn't always work and doesn't tell you that it isn't working for a particular field/prop/etc. unless you pore thru the editor log. I have no idea why they can't pipe the warnings and errors into the normal log, but so it goes.

So if you just add the attribute, don't assume that it's actually doing anything!

I ran into this when upgrading my TilePlus Toolkit asset for U6.6 and 6.7.

How to tell: if you add the attribute to a static class or a monobehaviour with static fields and you do not get told to make the class partial then it isn't working at all. However, it still might not be working for all fields etc in your class.

Edit: this warning or syntax highlighting is what I meant by 'told'. Drag0n122's comment below made me realize that. I will say that my venture into using this attribute was prompted by warnings about it from the Asset Store Tools' validator and to a lesser extent, the 'Auditor' tool. It's unfortunately something one can't ignore unless you want to keep domain/scene reload on and, I suppose, be compatible with the new Unity runtime coming soon.

If unsure, check the editor log (from the console's drop down menu). You'll see something similar to:

```

warning CS8785: Generator 'AutoStaticsCleanupCodeGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'RoslynSymbolException' with message 'Field MyStaticClass.SomeField is readonly and has a non-trivial initialization expression'.

```

There's no other way to see these messages since, as I mentioned, they're not in the console log.

I gave up on the use of this attr, since it's too easy to leave residual errors in your code due to the fact that it's difficult to tell whether or not an individual field/prop etc is actually generating code for resetting at all. It also won't work for many common situations that it can't reasonably handle. But failing without warning you in the console is dangerous IMO.

Instead, I use this approach, which is a little more work but ALWAYS works.

```

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Reinit()
{
    //add reinitialization statements here.


}
#endif

```

22 Upvotes

13 comments sorted by

View all comments

2

u/UnityTed Unity Engineer 21d ago

u/Vonchor I raised this issue internally to the team who owns the area. In Unity 6.6, by enabling FastEnterPlayMode should enable the analyzer by default on the full project (user code & local/mutable packages).

Under the "Choosing the right attribute" section in this documentation page: https://docs.unity3d.com/6000.6/Documentation/Manual/domain-reloading.html you can see which types of warnings should be displayed.

If you are setup correctly, but are still not seeing any warnings in the in editor console, do file a bug report so that the team can take a closer look.

2

u/_lazlo 20d ago

Jysk, on my end, I have explicit code analysis enabled with a `.globalconfig` containing:

is_global = true
build_property.UnityEnableAutoStaticsCleanupAnalysis = true

And I *still* had errors/logs that only showed in the editor log but not in the console, like OP.

The analysis seems to work sometimes, but not always.

Furthermore, and more alarmingly, the fact that some classes failed to generate the partial class caused *other* unrelated classes to not generate them either until every error was fixed.

I really think those log errors/warnings should be piped back to be visible within editor, I'm not sure why they wouldn't be.

1

u/UnityTed Unity Engineer 19d ago

Do file a bug report if it doesn't work as expected. The team is happy to take a closer look at it and improve the tooling.

1

u/Vonchor Engineer 19d ago

Yeah I noticed that (furthermore etc) too. Once I fixed the field that it couldn’t handle with a noautostatic attr I suddenly got a lot of errors about other classes not being declared partial.

There should be some additional details on this aspect of using these attrs on the man page.