x


How are unity references serialized? (instanceID related)

Hi

I'm building some serializing logic, and I'm trying to serialize a reference to a unityObject. What I have come across is AssetDatabase.GetAssetPath --> AssetPathToGUID. Which works quite well, but not for assets which aren't the "main" asset. And object.GetInstanceID --> EditorUtility.InstanceIDToObject which works even better.

But I wonder how references are serialized in the project by Unity, is it using InstanceIDs?

And is there a risk with using instanceIDs? Are they changed by Unity sometimes?

Lastly, is there a better way to serialize references?

Thanks, Aron

more ▼

asked May 12 '11 at 04:30 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This is not a definitive answer since I'm not an expert, but I don't see anything that changes InstanceIDs, and I see some infrastructure that saves them (I think). So it seems like you can use InstanceIDToObject and GetInstanceID.

more ▼

answered May 12 '11 at 05:30 PM

almo gravatar image

almo
1.7k 2 6 18

That appears to be the case. One thing to check would be to make sure that the instance ID of a particular game object or component is the same from one run of Unity to the next. The docs do say that the instance ID is unique. Note that instance ID's are 32-bit, while asset GUIDs are 128-bit, so obviously they aren't equivalent.

May 12 '11 at 08:41 PM yoyo

I have more info on this now. It seems the InstanceIDs can change from load to load. Though they do not always do so. They do seem to remain static once the objects are loaded, though.

Jun 02 '11 at 07:24 PM almo

Yes it seems so, I have experimented and found that they (at least some) change when changing scenes, so if you saved IDs in one scene, they only remain the same as long as you don't change to another scene and back again (usually).

Jun 09 '11 at 05:20 PM TowerOfBricks

Unity seems to use an object called ReferenceData which contains an instanceID and a variable called "cachedPtr" (int) I'm not sure what it is for, perhaps a pointer to the memory address or something.

Jun 09 '11 at 05:45 PM TowerOfBricks
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Feb 03 '12 at 04:45 AM

sigs gravatar image

sigs
16 3 4 6

This project seems redundant now that UT is using text-based yaml to serialize scenes and prefabs.

Apr 24 '12 at 04:36 PM yoyo
(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:

x436
x179
x133
x75

asked: May 12 '11 at 04:30 PM

Seen: 2452 times

Last Updated: Apr 24 '12 at 04:36 PM