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

SerializeField auto converting

Discussion in 'Scripting' started by LSPredator, Apr 24, 2021.

  1. LSPredator

    LSPredator

    Joined:
    Jun 23, 2014
    Posts:
    35
    If I create int field
    [SerializeField] private int temp;

    and at the Inspector set 6, than change at code type to float
    [SerializeField] private float temp;

    I see at the inspector 6 is stays.

    How can I make the same behavior with my class/struct?

    I need, when I change int
    [SerializeField] private int temp;

    to my LoggableInt
    [SerializeField] private LoggableInt temp;

    the value not to be lost.

    Code (CSharp):
    1. [Serializable]
    2. public class LoggableInt
    3. {
    4.     public int Value
    5.     {
    6.         get
    7.         {
    8.             return _value;
    9.         }
    10.         set
    11.         {
    12.             Debug.Log($"New value: {value}");
    13.             _value = value;
    14.         }
    15.     }
    16.  
    17.     [SerializeField] private int _value;
    18.  
    19.     public LoggableInt()
    20.     {
    21.         _value = default;
    22.         Debug.Log($"New value: {_value}");
    23.     }
    24.  
    25.     public LoggableInt(int value)
    26.     {
    27.         _value = value;
    28.         Debug.Log($"New value: {_value}");
    29.     }
    30.  
    31.     public static implicit operator int(LoggableInt loggableInt)
    32.     {
    33.         return loggableInt.Value;
    34.     }
    35.  
    36.     public static implicit operator LoggableInt(int intValue)
    37.     {
    38.         return new LoggableInt(intValue);
    39.     }
    40. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    No because the format of an int and a float in yaml is basically unchanged, but in your case you have a whole object which is another layer of complexity and indirection.

    If this is possible IDK how to do it.
     
    LSPredator likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Yeah, as far as YAML is concerned, a lone int or float can both be represented as:
    Code (csharp):
    1. temp: 6
    But a value that is inside a class/struct will be represented as:
    Code (csharp):
    1. temp:
    2.   value: 6
     
    LSPredator and PraetorBlue like this.
  4. LSPredator

    LSPredator

    Joined:
    Jun 23, 2014
    Posts:
    35
    If use float, set 6, and change to int, the value changed to 0.
    So It's not only YAML, it's also some logic here.
    So may be it's possible to convert int to struct.
     
  5. LSPredator

    LSPredator

    Joined:
    Jun 23, 2014
    Posts:
    35
    Although you are probably right, and this is impossible.
     
  6. LSPredator

    LSPredator

    Joined:
    Jun 23, 2014
    Posts:
    35
    Only if it would be possible to store struct with one field in the same way as stored int.