x


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

more ▼

asked Dec 11 '09 at 08:56 AM

limdor gravatar image

limdor
55 3 3 8

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

1 answer: sort voted first

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.
more ▼

answered Dec 11 '09 at 10:46 AM

AngryAnt gravatar image

AngryAnt ♦♦
4k 14 19 52

I will try it and I will tell you something

Thx

Dec 11 '09 at 01:08 PM limdor

Hey Emil, do you know is the method you described with a custom asset going to work even after switching to another scene and then back to the original, or at least after reopening the project in the Editor? Seems like that failed according to this: http://answers.unity3d.com/questions/188059/how-to-reference-a-scene-gameobject-from-a-custom.html

Nov 23 '12 at 05:07 AM Flipbookee
(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:

x5099
x349

asked: Dec 11 '09 at 08:56 AM

Seen: 1973 times

Last Updated: Nov 23 '12 at 05:07 AM