Search Unity

Serialize Vector3

Discussion in 'Editor & General Support' started by WaxerPL, Dec 31, 2019.

  1. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    Hey there!

    I am working on my saving system and I can't deal with a serialization problem.

    I'm trying to save an array of Vector3 and array of quaternions and array of Colors.
    They are one dimmensional arrays.

    Color[] colors;
    Vector3 vectors;
    Etc.


    Unity Log says I cant because Vector3 isn't marked as serializable.
    But Unity Documentation says it is.

    When I remove those arrays my whole script works. It saves and loades all ints, strings, floats etc.

    What's the problem?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Does it work in earlier versions than Unity 2019.3? Serialzation was worked on in 2019.3, perhaps you found a regression, therefore please check if this problem is new to 2019.3.
     
  3. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    I'm using 2019.3.11b
     
  4. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    I've downloaded 2019.3.0f3. The problem still exists
     
  5. asadmuzammil77

    asadmuzammil77

    Joined:
    May 31, 2017
    Posts:
    23
    You can't serialize Vector3 or any Unity Classes in a Save File.You can only serialize datatypes such as float,int,byte etc.You can also serialize their arrays.
    You will have to create your own custom classes that inherits from the UnityClass you want to serialize and Add Attribute on it [System.Serializeable].
    For more Info You can watch this Video.



    I hope this will help!
     
  6. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    Simple field types that can be serialized
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Vector3
    ,
    Vector3[]
    and
    List<Vector3>
    can all be serialized, I never had a problem with that.

    Please create an example script that shows what you're doing. Please check if this works in earlier Unity versions than 2019.3, because you post this in the beta forum and I'm trying to figure out whether this is a new issue in 2019.3.

    Also clarify what "serialization" means in your specific case. Do you use Unity's serialization system or a custom solution?
     
  8. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    The class:
    Code (CSharp):
    1. [System.Serializable]
    2. public class FragmentMeshData
    3. {
    4.     public int verticesPerLine = 0;
    5.     public Vector3[] vertices = null;
    6.     public int[] triangles = null;
    7.     public Color[] colors = null;
    8.     public int triangleIndex = 0;
    9.     public int VectorIndex = 0;
    10.     public int ColorIndex = 0;
    11. }
    My serialize method:
    Code (CSharp):
    1. public static void SerializeFile<_T>(string FilePath, _T save)
    2.     {
    3.         BinaryFormatter bf = new BinaryFormatter();
    4.         FileStream file = File.Create(FilePath);
    5.  
    6.         bf.Serialize(file, save);
    7.         file.Close();
    8.  
    9.     }
    I want to highlight that every other class without Vector3, Color etc. works well.
    If I remove those things:
    Code (CSharp):
    1. [System.Serializable]
    2. public class FragmentMeshData
    3. {
    4.     public int verticesPerLine = 0;
    5.     //public Vector3[] vertices = null;
    6.     public int[] triangles = null;
    7.     //public Color[] colors = null;
    8.     public int triangleIndex = 0;
    9.     public int VectorIndex = 0;
    10.     public int ColorIndex = 0;
    11. }
    Code works so well
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    OK, so you're not using Unity's serialization system.

    Now for the third time: Since you posted this in the beta testing forum, please figure out if this issue is related to the beta version. Does it work in Unity 2019.2 for example? If it's not related to the beta, you have better chances to get an answer in a different sub-forum, then we can ask a moderator to move the thread, so you get help faster.
     
  10. asadmuzammil77

    asadmuzammil77

    Joined:
    May 31, 2017
    Posts:
    23
    Like I said In Serialization (only) use array of float of length 3 for x,y,z instead of Vector 3
    as was in the above video
    Because The System doesn't recognize Unity datatypes such as Vector 3 etc
     
  11. asadmuzammil77

    asadmuzammil77

    Joined:
    May 31, 2017
    Posts:
    23
    If you don't understand The serialization you should see this video as well

     
  12. asadmuzammil77

    asadmuzammil77

    Joined:
    May 31, 2017
    Posts:
    23
  13. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    Thanks for those links, but I understand how it works. I've used it many times but always I've been writing my own "Vector" classes like this one:

    Code (CSharp):
    1. [Serializable]
    2. public struct Vector2S
    3. {
    4.     public float x;
    5.     public float y;
    6.  
    7.     public Vector2S(float x, float y)
    8.     {
    9.         this.x = x;
    10.         this.y = y;
    11.     }
    12.  
    13.     public override bool Equals(object obj)
    14.     {
    15.         if (!(obj is Vector2S))
    16.         {
    17.             return false;
    18.         }
    19.  
    20.         var s = (Vector2S)obj;
    21.         return x == s.x &&
    22.                y == s.y;
    23.     }
    24.  
    25.  
    26.     public Vector2 ToVector2()
    27.     {
    28.         return new Vector2(x, y);
    29.     }
    30.  
    31.     public static bool operator ==(Vector2S a, Vector2S b)
    32.     {
    33.         return a.x == b.x && a.y == b.y;
    34.     }
    35.  
    36.     public static bool operator !=(Vector2S a, Vector2S b)
    37.     {
    38.         return a.x != b.x && a.y != b.y;
    39.     }
    40.  
    41.     public static implicit operator Vector2(Vector2S x)
    42.     {
    43.         return new Vector2(x.x, x.y);
    44.     }
    45.  
    46.     public static implicit operator Vector2S(Vector2 x)
    47.     {
    48.         return new Vector2S(x.x, x.y);
    49.     }
    50. }
    And when I use my Vector2S or similar Vector3S everything works well.

    I always did it this way, but a few days ago I read that Unity allows to serialize built-in Vector3, but it seems it doesn't.

    I tried to serialize Vector3 in
    2019.3.0b12
    2019.3.0f1
    2020.1.0a17

    and it always says Vector3 isn't marked as serializable
     
  14. asadmuzammil77

    asadmuzammil77

    Joined:
    May 31, 2017
    Posts:
    23
    Where did you read that? Can Send me The Link?
     
    bugfinders likes this.
  15. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    It does, but it's slightly misleading as its referring to the built-in Unity serializer which supports Vector3 automagically.

    This has existed in Unity since the beginning and is not specified to 2019.3.

    Your options are;
    • Create a serializable Vector3 type (as you've already done)
    • Implement a serialization surrogate (as explain here)
    • Stick with the Unity serializer to store your data (ScriptableObject or JsonUtility)
     
  16. Deozaan

    Deozaan

    Joined:
    Oct 27, 2010
    Posts:
    707
    This is somewhat tangential, but I think your Vector2S != operator has a bug.

    Your code says they're not equal if both x and y values are different. But only one of the values needs to be different for them to not be equal.
     
  17. WaxerPL

    WaxerPL

    Joined:
    May 26, 2015
    Posts:
    33
    @Deozann thank you for your reply! Now I see, just changed && to ||

    Thank you very much!
     
    Deozaan likes this.
  18. diliupg

    diliupg

    Joined:
    Jan 23, 2018
    Posts:
    45