x


OnSerialize event

Is possible to be told after serialization occurs for objects, in the Editor?


I know that Unity3D serializes some variables (as described in the SerializeField manual page).

After importing a script something in my project, clicking play, or going back from play mode, Unity3D recreates all the objects in memory, and reset all serializable fields. That's why when I Debug.Log() a public variable (like int), it says the default value, but when I access it with other script it says the previous stored (serialized) value.

What I want to know is when serialization takes place, to sew my variables in the editor to a more proper object. The manual refers to this process of sewing:

Hint: Unity won't serialize Dictionary, however you could store a List<> for keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn't solve the problem of when you want to modify the dictionary and have it "saved" back, but it is a handy trick in a lot of other cases.

Thing is, in the Editor I can't use the Awake function. I've tried OnEnable, OnBecameVisible, Start, Awake and Reset with no help: none of these functions are called after I make a change in the scripts. The constructor is called, but as I mentioned earlier, it contains the constructor values.

Any help would be much appreciated.

more ▼

asked Feb 15 '12 at 05:35 AM

Veehmot gravatar image

Veehmot
378 14 18 25

Same problem here.

Mar 16 '12 at 11:07 PM hvilela
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You might be able to use a twist on lazy initialization:

//by default, we have a 'null' dictionary
//the first time we access it, create and sew
private Dictionary<Foo> _d;
public Dictionary<foo> d
{
    get
    {
       if (_d == null)
       {
         _d = new Dictionary<foo>();
         Sew();
       }
       return _d;
    }
    set
    {
       _d = value;
    }
}

public void Sew()
{
    //do stuff
}

This seems like it should re-create and re-sew the dictionary each time you get a new object.

more ▼

answered Mar 17 '12 at 12:03 AM

rutter gravatar image

rutter
5.1k 2 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1663
x347
x175
x169

asked: Feb 15 '12 at 05:35 AM

Seen: 728 times

Last Updated: Mar 17 '12 at 12:03 AM