Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is there any way to know if a type can be serialized by Inspector?

Discussion in 'Scripting' started by akeit, Dec 3, 2023.

  1. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
    ValueTuples have [Serializable] attributes and Public fields, but they can not be serialized.
    Vector3 and some built-in types don't have [Serializable] attributes, but they can be serialized.
    Is there any way to know if a type can be serialized by Inspector from C#?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,236
    Lurking-Ninja likes this.
  3. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
  4. Why? I think
    ValueTuple
    is exactly what they say: it is not serializable because it doesn't fulfill the rules they laid out on the link I provided above.
    What is the exact exception?

    It doesn't dependent where it is declared, it depends on the rules you can find on the link I provided above.
    Especially these, but the rest also apply:
    ValueTuple
    currently doesn't fulfill these rules.

    Wrap in a
    Serializable
    class using this:
     
    Kurt-Dekker likes this.
  5. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
    As an example, this is part of the code for ValueTuple<T1, T2>.
    Code (CSharp):
    1. [Serializable]
    2.   [StructLayout(LayoutKind.Auto)]
    3.   public struct ValueTuple<T1, T2> :
    4.     IEquatable<(T1, T2)>,
    5.     IStructuralEquatable,
    6.     IStructuralComparable,
    7.     IComparable,
    8.     IComparable<(T1, T2)>,
    9.     IValueTupleInternal,
    10.     ITuple
    11.   {
    12.     public T1 Item1;
    13.     public T2 Item2;
    When I copy and paste this code and remove IValueTupleInternal interface, it can be serialized.
     
    Last edited: Dec 3, 2023
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,961
    If I'm not mistaken Unity cannot serialize a generic type T with no restrictions (aka "where T: System.Object"). This means it could literally be *anything*.

    It might work with [SerializeReference] though.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,236
    What part of that code changes what has already been noted above?
     
  8. Well, of course, custom structs (when fulfill the rules I even pasted for you above) can be serialized. And when you do that, it becomes a custom struct called "
    ValueTuple
    " and it won't be the C# type
    ValueTuple


    But as @Kurt-Dekker asked above, what's your point?
     
  9. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
    I can't find what rule is not fulfilled.
    The TupleTest below has been serialized.
    Code (CSharp):
    1.  
    2. public ValueTuple<int, int> TupleTest;
    3. [Serializable]
    4. [StructLayout(LayoutKind.Auto)]
    5. public struct ValueTuple<T1, T2> {
    6.     public T1 Item1;
    7.     public T2 Item2;
    8.  
    9.     public ValueTuple(T1 item1, T2 item2) {
    10.         this.Item1 = item1;
    11.         this.Item2 = item2;
    12.     }
    13. }
     
  10. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
    I copied from mscorlib.dll.
     
  11. Yeah, that's only .NetFramework, in .NetStandard (netstandard.dll) it looks like this:
    Code (CSharp):
    1. public partial struct ValueTuple<T1, T2> : System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.IComparable, System.IComparable<System.ValueTuple<T1, T2>>, System.IEquatable<System.ValueTuple<T1, T2>>, System.Runtime.CompilerServices.ITuple
    2. {
    3.      public T1 Item1;
    4.      public T2 Item2;
    5.  
    6.      [...]
    7. }
    Unity Serializer probably implements the lowest common denominator here. I'm obviously not sure because I don't work at Unity.
     
  12. akeit

    akeit

    Joined:
    Apr 28, 2021
    Posts:
    9
    Ah, I understand, UnityEngine does not seem to look for Serializable types in mscorlib.dll or UnityEngine.dll.