How do I serialise GameObject references between editor and runtime?

I'm trying to do a Potencial Visibility Set system and I have some problems about the runtime. In the editor I have a button in a editor class to calculate which transforms are potencialy visible. This metod is very expensive and I save to disk this informations in a XML file.

The XML is like this:

..
<cell i="0" j="0" k="3">
    <transform>17638</transform>
    <transform>17636</transform>
    <transform>17642</transform>
</cell>
..

Every number is a transform ID that I get it with metod GetInstanceID.

I want to transform this number to a transform again in the start metod of the camera but the only metod that i know is EditorUtility.InstanceIDToObject but it's for the editor only.

Any ideas?

Thx

Instance IDs are only valid within the same session. In stead I'd suggest saving the path of the GameObject.

using UnityEngine;

public class GameObjectUtility
{
    public static string GetPath (GameObject gameObject)
    {
    	string path = "/" + gameObject.name;
    	Transform transform = gameObject.transform;

    	while (transform.parent != null)
    	{
    		transform = transform.parent;
    		path = "/" + transform.gameObject.name + path;
    	}

    	return path;
    }

    public static GameObject GetGameObject (string path)
    {
    	return GameObject.Find (path);
    }
}

Edit: Since you're working with GameObjects with non-unique names, you could alternatively set up a custom asset to store serialized references to your GameObjects.

Custom asset handling:

  1. Create a ScriptableObject derived class which stores your data (groups and GameObject references in your case).
  2. To create a new asset of your type, use AssetDatabase.CreateAsset (new YourAssetType (), "Assets/Path/To.asset");.
  3. To load your asset at editor time, use myAsset = AssetDatabase.LoadAssetAtPath ("Assets/Path/To.asset", typeof (YourAssetType)) as YourAssetType;
  4. When you have made a change to your asset, use EditorUtility.SetDirty (myAsset); to serialise them to disk.
  5. To access the asset data at runtime, make a public variable in the script needing access to the type of your asset and drag-drop the asset from the project view to that property in the inspector.