The Unity manual explains how to run editor script code on launch. The example shows the InitializeOnLoad attribute being used on the class itself in combination with a static constructor. The docs explain, that Unity calls the static constructor of the decorated class as soon as the editor starts.
However, there is also the InitializeOnLoadMethod attribute, which seems to do the same thing, although it directly decorates a static method.
Is there any functional difference between the two? Any benefit of using one over the other?
At first, I thought they were basically the same, because both are static, however, after thinking about it, I realize that InitializeOnLoad and the static constructor is more flexible, because I can assign to static readonly fields in this case, but not when using the static method. I just wonder, why we have both since I can always call any static methods myself from the static constructor anyway.
The use cases for the two are largely the same, but with slightly different connotations. If someone unfamiliar with your code (or yourself months down the line when you have forgotten about it) sees InitializeOnLoad, you have to look in more places before your understand what that attribute means for a given class. InitializeOnLoadMethod is more self-contained, generally.
I would prefer to use InitializeOnLoadMethod because I feel it is simpler to understand with a cursory glance. It decorates exactly the thing I need to look at where as InitializeOnLoad requires me to find the static constructor and look at that. Realistically though, the difference are very minor. If you need to setup static class data, the static constructor is the best place to do that, but if you are just running an essentially independent function, InitializeOnLoadMethod seems to be clearer in intent.