Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is it possible to inherit from FormerlySerializedAsAttribute?

Discussion in 'Scripting' started by eliashodel, Aug 6, 2023.

  1. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    Hi there

    I was experimenting with creating my own FomerlySerializedAsAttribute to rename AutoProperties without having to write
    FormerlySerializedAs("<MyOldName>k__BackingField")


    This is what i came up with:
    Code (CSharp):
    1. public class FormerlySerializedAsAutoProperty : FormerlySerializedAsAttribute
    2. {
    3.     public FormerlySerializedAsAutoProperty(string oldName) : base($"<{oldName}>k__BackingField")
    4.     {
    5.     }
    6. }
    Which then should be used like this:

    Code (CSharp):
    1. [field: SerializeField]
    2. [field: FormerlySerializedAsAutoProperty("OldName")]
    3. public float Value {get; private set;}
    However that did not work, my attribute is just ignored. This left me wondering if this even is supposed to work? How is Unity calling and using the Built-in Attributes? Or am i missing something to make this work?

    Looking forward to your insights!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Even if you make your own attribute inherited from
    FormerlySerializedAsAttribute
    (which should be sealed, honestly), Unity is not looking for it at all in whatever internal part of the engine manages this and thus will do nothing.

    So, no this won't ever work.
     
  3. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    Yeah i was assuming this was the case, thanks for confirming!