Skip to content

Avoid using string based names for setting and getting properties on Animators, Shaders and Materials

Matt Ellis edited this page Feb 19, 2019 · 4 revisions

The setter and getter methods on Animator, Material and Shader can take a string parameter to identify the property to return or set. For example, Animator.GetBool, Material.SetTexture and Shader.GetGlobalValue, amongst others. Each time the method is called, Unity will convert the string name into an integer ID to identify the property. This integer ID is a stable value and can be calculated in advance and used in overloads to the setters and getters to reduce the overhead of calculating the ID on each method call.

This inspection will highlight any usage of a name-based Set or Get function for Animator, Shader or Material. It also provides an Alt+Enter quick fix which will introduce or reuse a static read-only integer field to calculate and cache the integer ID of the name and use it in place of the string parameter in the setter or getter method. For Animator methods, the ID is calculated with Animator.StringToHash, and Shader.PropertyToID is used for both Shader and Material.

This inspection was first added in Rider/ReSharper 2018.3

Clone this wiki locally