Need insight into how object references are deserialized

Given the following simple script:

using UnityEngine;

public class MyScript : MonoBehaviour {
  public Texture2D Texture;
}

If I create a GameObject, attach said script, and drag a texture asset onto the inspected field. How does Unity restore that texture reference at runtime in the player? I’ve searched over the forums and the wiki, but haven’t been able to find any reliable identifier for assets that is accessible both in the editor and at runtime.

I’ve got an editor integrated tool that I’m working on that generates a custom asset type. That type has references to Texture assets in the project. The assets I generate are using custom serialization and then storing the results as binary data that is handled directly by Unity. All this is working quite well, with the exception being that I can’t find a reliable way to serialize a reference to a Texture or Material and then restore it via the player at runtime.

If this isn’t clear feel free to PM me.

Hello!

What you can do is to use the WWW class: Unity - Scripting API: WWW

And load your assets in run time given an URL (In case you are using a web player).

Hope it helps you out :wink:

Unfortunately this is not related to what I’m doing.

If you are familiar with the Behave library, (and Path library for that matter), you’ll see that the “custom” serialized data for those tools is stored in byte array that is managed by Unity, using a simple ScriptableObject with a byte[ ] field. The library itself them works with the contents of that byte array.

I’ve followed the same approach (as nothing else seems remotely possible, and I spent a good month pursuing more favorable designs); but where that approach falls apart is if you try and serialize a reference to a Texture2D object (or any other UnityEngine.Object derived class) where you expect the serialization graph to remain intact across persistance boundaries.

From a custom serialization perspective, this is not possible from any public API that I’ve been able to locate, as you do not have access to the methods found in the AssetDatabase class at runtime, which would be required to make this work, assuming you serialized your references by asset path. Given that AssetDatabase is closed to runtime code, I would assume there must be some way to reconstitute an object graph given persisted ID’s in a reliable manner, but if it exists, I’ve not been able to find it.

In the interim while waiting on a reply back here, I’ve made some headway on a workaround that will suffice, but I’d be thrilled if someone has a more direct solution.