Can we find out an object's prefab/asset in the Editor?

You can tell visually what prefab or asset an object is linked to, in the Editor. Is there any way to find that out programmatically?

If so, would there be a way to access that information, using the DragAndDrop class, when a certain script is attached?

Here is the chain of events I want to happen:

  • Select a script from the Project pane.
  • Drag it onto a Game Object.
  • If the Game Object is an instance of a prefab or asset, assign the link to that prefab or asset to an exposed variable on that Game Object.

This is an interesting question. A prefab after all is just an instance of a transform with a set of components attached, but the editor must somehow be maintaining a link in order to be able to do it’s “Apply” and “Revert” functions.

Are AssetDatabase.Contains and AssetDatabase.GetAssetPath of any use to you here?

1 Like

so if I am understanding you correctly, you want the script you are dropping onto an object to point to the object containing that object, if any.

If that is the case then there is a very simple way to do that, yes.

var myObject : Transform;

function Awake()
{
// see if this object's highest parent class has a parent
if (transform.parent.transform == transform.root.parent.transform)
   myObject = null; else
// if not, then assign the parent object to the var
   myObject = transform.root.transform;
}

I haven’t used this in a while and I am at work at the moment so I am not entirely clued up on the exact syntax here, but have a look at the transform.parent and transform.root. That should give you an indication of wether the object the script is dropped on is a root level object or an object contained by another object…

Possibly, if there is a way for an Editor Script containing them to be alerted when a certain type of script is attached.

No, I’m not talking about objects in the Hierarchy. Objects in the Hierarchy may be instances of assets or prefabs, allowing them to automatically update when those assets or prefabs are changed.

okay, sorry, forgive my stupidity, here. I think if I read your post with the other eye I think I can see where you are coming from. Allow me to re-word your initial post and just point out to me where I am falling of the train, please…

You have GameObject A that is in the scene
You drop script B onto GameObject A
If GameObject A is a prefab then add a link to the actual/ master/ head prefab into an exposed variable.

So basically you want to have the ability to create new instances from GameObject A without actually duplicating GameObject A. Is that correct?

The way I read it initially was:
You have GameObject A in the scene
If you drop script B onto GameObjct A then you want to get a reference to the object that contains GameObject A.

I was working on the concept that you would parent any instanced of prefabs so something instead of just laying loose in the scene. my bad.

Well, in the latter case, my code above should have worked but in the former situation, I am afraid I am not going to be of much use…

The only thing that comes to mind is Instantiate(gameObject);
but that would duplicate the entire object includigg all your settings (won’t it?)

Sorry. Next batter’s up…

Right.

No. When you add a script, it will break the link. I don’t have any use for the original instance. I just want this reference set up.

This is not hard to do at all, manually. It’s just two drag and drop operations. In fact, I’m still going to be performing the first drag and drop, but I want the second one to happen automatically. I don’t know how to script that, though. if it is even possible.

On answers, Lucas Meijer got something that worked for me:
http://answers.unity3d.com/questions/2759
Basically if you first use:

Object parentObject = EditorUtility.GetPrefabParent(gameObject);
string path = AssetDatabase.GetAssetPath(parentObject);

I do get the paths! Without the GetPrefabParent it doesn’t work even if I go over all the GameObjects in the scene.

This actually won’t anymore. As far as I know the class “PrefabUtility” takes over this work:

GameObject prefab = PrefabUtility.GetPrefabParent (objectInstance) as GameObject;