-
Notifications
You must be signed in to change notification settings - Fork 134
Avoid using string based Method Invocation
Certain Unity APIs take a string value that is the name of a method to invoke. This can be very inefficient, and it is recommended to avoid these methods inside a performance critical context. For example, MonoBehaviour.Invoke
works very much like reflection, having to look up the method by name on the current behaviour. The SendMessage
functions are even more expensive, looking for the named method on each MonoBehaviour
attached to the game object.
Each usage inside a performance critical context will be highlighted. Context actions that move the invocation to Start
or Awake
are available in Alt+Enter, although be careful, as this does change the semantics of your code - the named method is invoked at a different time. Other ways to improve performance may require more general rewriting to avoid these methods.
The methods highlighted include:
MonoBehaviour.Invoke
MonoBehaviour.InvokeRepeating
MonoBehaviour.CancelInvoke
MonoBehaviour.IsInvoking
-
GameObject.SendMessage
+Component.SendMessage
-
GameObject.SendMessageUpwards
+Component.SendMessageUpwards
-
GameObject.BroadcastMessage
+Component.BroadcastMessage
This inspection was first added in Rider/ReSharper 2018.3