How does a non-instance of loaded Prefabs compare to instances?

Example

In Unity5, assuming a GameObject named "SomeObject" has been saved as a prefab in Assets/Resources/SomeObject.prefab

, I know that I can instantiate the collection like this:

GameObject prefab   = Resources.Load<GameObject>("SomeObject");
GameObject instance = GameObject.Instantiate(prefab);

      

Once executed, the instance

GameObject will be a copy of the original "SomeObject" and will be placed in the current active scene named "SomeObject (Clone)".


As far as I understand, this GameObject prefab

is an actual prefab resource and any changes made to it (setting name, adding components, etc.) are written to the original prefab asset, and these changes persist even after exiting the editor play mode.

Questions

  • Since all GameObjects are usually stored in the scene, and the property scene

    in GameObject prefab

    the above example seems to be Unloaded / Invalid / Unnamed, where exactly should I consider this special GameObject? It seems I can do everything with this, which I can do with regular GameObjects without being visible in the hierarchy / scene view.

    Is it in some kind of state of limbo or in a special PseudoScene? It looks like it persists across scene changes LoadSceneMode.Single

    , but doesn't look like a special scene DontDestroyOnLoad

    that objects are moved to when passed to GameObject.DontDestroyOnLoad(...)

    .

  • Are there any other notable differences between prefab

    and instance

    in the above example, other than life changes, invalid scene, and different objects from each other (having different GetInstanceID()

    , etc.)?

  • Since the call GameObject.Instantiate(...)

    to prefab

    give GameObject, which is permissible scene, is there a way to manually create GameObjects, who are in a similar condition "no scene"? I know that Unity intends for all GameObjects to be in scenes and this is purely an academic question.

I tried to look at the documentation for the function , but nobody goes into the technical details of what exactly happens when you call Resources.Load

the prefab resource.

+3


source to share


1 answer


1. How should I treat this special GameObject like?

It is in a separate file and is not mentioned in the scene. When called Resources.Load<GameObject>("SomeObject");

, it is loaded into memory awaiting use, when called GameObject.Instantiate

.

If it is declared as public GameObject prefab;

and assigned from the editor instead GameObject prefab Resources.Load<GameObject>("SomeObject");

, it will be automatically loaded into memory when the scene is loaded.

2. Are there any other notable differences between a prefab and an object?

Yes.

Collections cannot be destroyed using the Destroy

or function DestroyObject

. It needs to be destroyed using a function DestroyImmediate

passing the true

second argument:DestroyImmediate(prefab, true);

3. Is there a way to manually create GameObjects that are in a similar "no scene" state?

No, there is no way to manually create GameObjects that are in a similar no scene state.

You can fake it though.



The closest thing to making a GameObject invisible on the Editor Hierarchy and Inspector tabs is by changing the GameObjects parameter hideFlags

, then deactivate that GameObject.

GameObject obj = new GameObject("SomeObject");
obj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;

      

After that, deactivate the GameObject:

obj.SetActive(false);

      

The GameObject is now invisible in the editor, and it is also invisible in the Scene and Game view.


There really isn't much of a difference between prefabs and a typical GameObject. A Prefab is just a container that contains a GameObject so that it can be reused and shared between scenes. Changing one collection will update any copy of it.

Imagine when you have hundreds of GameObjects of the same type in your scene and need to change all of them. With a prefab, you only need to change one of them, or just a prefab, and then click Apply to update other instances. This is the only thing that you have to think about national teams.

+3


source







All Articles