resharper-unity icon indicating copy to clipboard operation
resharper-unity copied to clipboard

Improve quick fix for introducing a cached property ID

Open citizenmatt opened this issue 5 years ago • 3 comments

The Material.SetFloat method can take a string name, that is immediately converted to a property ID via Shader.PropertyToID. We already have an inspection and quick fix to recognise this and introduce a field with a converted property ID. We can make some improvements:

  • When replacing one instance, check to see if there are any other instances in the same file and prompt to replace all.
  • Check to see if a field already exists and change text to say something like "Use existing property field 'Whatever'"
  • Provide name suggestions to choose from, based on the initial string literal value, but with additional suffixes, such as PropId and ShaderPropertyId

citizenmatt avatar Sep 01 '20 13:09 citizenmatt

Maybe generate something like the following?

public class MyComponent : MonoBehaviour
{
    private static class ShaderProperties
    {
        public static readonly int Tint = Shader.PropertyToID("_Tint");
    }
}

I think that would be cleaner if you have a couple of shader properties but might be a bit bulky for a single one.

vinhui avatar Sep 01 '20 13:09 vinhui

Yeah, I think moving to a separate class should be a separate refactoring step.

citizenmatt avatar Sep 01 '20 13:09 citizenmatt

Am I tripping or has this feature been removed?

vanilla-plus avatar Sep 07 '24 11:09 vanilla-plus