Search Unity

Serialization - Dictionaries and Decouple of references

Discussion in 'Scripting' started by nettomb, Jun 19, 2017.

  1. nettomb

    nettomb

    Joined:
    Mar 24, 2017
    Posts:
    14
    I'm using Unity 5.5. I have implemented (in fact, I have copied an implementation) a Clone method using serialization. The fact is that I was expecting to have problems with dictionaries and decoupled references, but I'm not experiencing it.

    In relation to dictionaries, what kind of problem will show up when trying to serialize them? My cloned objects are bringing dictionaries with exactly the same key/value relations.

    In relation to references, I've read that serialization (I'm not using ScriptableObjects) would copy data by value, decoupling the references. I have:

    class Obj1 {
    }
    class Obj2 {
    public Obj1Ref;
    }

    Test:
    Obj1 testObject1 = new Obj1();
    Obj2 testObject2 = new Obj2();
    Obj2.Obj1Ref = Obj1;

    Obj2 clonedTestObject2 = testObject2.Clone();

    Now, if I check with ReferenceEquals, testObject2.Obj1Ref and clonedTestObject2.Obj1Ref are the same. Both refer to testObject1. If I change testObject1, the change is reflected on both. This is exactly what I want, but I was expecting the references to be decoupled. I just want to understand the underlying logic of it. After all, will the serialization make a copy by value, decoupling reference? If so, what are the conditions for which this won't happen? Isn't it happening because I'm doing runtime serialization?
     
  2. CraigGraff

    CraigGraff

    Joined:
    May 7, 2013
    Posts:
    44
    @nettomb Sorry that this isn't an answer to your question, but are you using a serializable generic dictionary? If so, could you please paste an example of your code? I'm having trouble getting a serializable class that extends dictionary to display in the editor.