Search Unity

Feature Request Inject tuned values in inspector as field default values in C# script

Discussion in 'Editor & General Support' started by huulong, Aug 16, 2022.

  1. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    Hello, I was tuning values of MonoBehaviour / ScriptableObject in the inspector, and realized that the default values I set in my C# script were very bad. So, I'd like to reinject the values tuned in the inspector (or at least some value in the same range) into my original C# script.

    Usually, I just copy paste the values I like. But I realize that this could be automated: find the name of the property, open the C# script, find the line that defines that property and edit the default value besides it to the one set in the inspector.

    Of course, this requires to touch a script file which is not trivial in some cases:
    - if the field is defined with preprocessor conditionals, e.g. to have different default values based on platforms, the change is not trivial
    - if the field comes from a custom editor that is not directly connected to a property, using custom getter and setter, finding a matching field is not trivial (e.g. if the getter/setter conversion means that the displated value is multiplied by 10 from the internal field, we should set the field default value to the value set in inspector / 10 ...)

    But I don't have any of these edge cases, just plain fields, in my project. So I could benefit from a mere regex replacement, even if it's not reliable in all cases.

    To other developers: are you interested by such an automation tool, and have you worked on something similar?

    To Unity devs: is there a chance that this becomes a native feature despite the complexity of editing script files? And if not, what is the first step to write my custom tool? (I need a way to get the original script asset from the Component/ScriptableObject being edited, but also to hijack the right-click to add some "reinject as default value" menu item... Maybe I need a custom property drawer?)

    If this is not something we want in the editor natively, I'll switch the label from Feature Request to Question/Discussion, and maybe Showcase if I (or someone else) manage to write said tool.
     
  2. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    I've realized Rider can find values used in all assets, so this allows us to visualize good new default values!
    Below: 0.2 instead of 1

    upload_2022-8-16_13-42-40.png

    Unfortunately clicking on said value doesn't replace the default value nor copy it to clipboard. But I can at least suggest this to Rider now!
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,910
    So elucidate me, are you looking to change the default value written out in the script asset in some form, or the value present across any number of serialised assets?