Does annotating Unity lifecycle methods with the UsedImplicitly
attribute improve the readability of your code?
For example:
public class Example : MonoBehaviour
{
[UsedImplicitly]
private void Awake()
{
DoSomething();
}
private void DoSomething()
{
// ...
}
}
This blog post on "Structuring Your Unity MonoBehaviours" suggests this approach as a useful convention.
- Annotate any Unity lifecycle methods (Start, Awake, Update, OnDestroy etc.), event methods, and other functions that are implicitly (or automatically) invoked in your code with the [UsedImplicitly] attribute. This attribute, although included in the UnityEngine.dll, is used by ReSharper to disable code cleanup suggestions for methods that are seemingly unreferenced. It also helps to make the code easier to read for people who are newer to Unity and your codebase, especially for events that are referenced via the inspector.
(Note: I don't think ReSharper shows code-cleanup suggestions for seemingly unreferenced Unity lifecycle methods, so that might be outdated advice.)
I agree with the above post that it could be helpful for those newer to Unity. I also think it could be useful to label those Unity lifecycle functions that are not used as frequently as Awake
, Start
, and Update
. But I am wondering if this is actually a good convention for "clean code", or if it's just noise that reduces readability.