# Reusable Components (Prefabs)

## Overview

Prefabs are powerful assets that allow developers to create, store, and reuse preconfigured game objects and their components.

When creating optimal prefabs for your project, keep the following best practices in mind:

1. **Simplify GameObject Structure**\
   Ensure the hierarchy is as simple as possible. Avoid unnecessary nesting or components to improve performance and maintainability. The realvirtual tool "[Clean Up Hierarchy](/components-and-scripts/performance-tools/combine-meshes-pro.md#hierarchy-clean-up)" supports this.
2. **Use Instantiated Materials**\
   All materials should be instantiated. This helps reduce memory usage and allows efficient batching during rendering. You can use the option "Set and Create Materials" in the [CAD link](/basics/cad-import/cadlink.md#step-import-settings) component and create instantiated materials when importing your data.
3. **Use Instantiated Meshes**\
   Ensure that meshes are unique instances to avoid conflicts and improve compatibility when creating prefabs.
4. **Combine Meshes**\
   If components consists of several meshes use the [**Mesh Combine**](/components-and-scripts/performance-tools/combine-meshes-pro.md) to merge multiple meshes into a single mesh.
5. **Share Materials Where Possible**\
   Use shared materials across multiple objects to minimize the number of draw calls. The fewer unique materials, the better the performance.

By adhering to these principles, you can create prefabs that are efficient, scalable, and ready for integration into complex projects.

## Creating Prefabs in realvirtual (Pro)

The `PrefabGenerator` is a utility designed to simplify the creation of prefabs. It operates in the Unity Editor and automates the process of generating prefabs, including handling sub-assets such as dynamically created meshes within child objects. It also considers Drives, Kinematics, and Group definitions, preserving their structure when creating the prefab. The PrefabGenerator also includes instantiated meshes in the scene (e.g., from previous combining), resulting in lighter scenes.

{% hint style="warning" %}
Combine Meshs and Create Prefab are only included in realvirtual.io Professional version

Please also check detailled documentation here:

[Combine Meshes](/components-and-scripts/performance-tools/combine-meshes-pro.md)

[Create Prefab](/components-and-scripts/performance-tools/create-prefab-pro.md)
{% endhint %}

### Usage

* Select the GameObject in the Scene Hierarchy that you want to convert into a prefab.
* Right-Click in the hierarchy area and select realvirtual/Create Prefab
* A prefab will be created in the `Assets/` folder with the same name as the selected GameObject.

### Example

For the example, we start with a CNC machine where components like "Drives" are already defined, along with some group definitions.

<figure><img src="/files/pEGG3cG03wLfsVHlU6lk" alt=""><figcaption><p>Game Object structure after import: starting point</p></figcaption></figure>

To prepare this GameObject as a prefab, follow these steps:

* Clean up the hierarchy: realvirtual/Clean up Hierarchy
* using realvirtual/Combine Meshes for every main Game Object

After these steps the structure look like this:

<figure><img src="/files/wOOiRbPmhKh1G7yTivWb" alt=""><figcaption></figcaption></figure>

Now your Game Object is prepared and ready to create a prefab:

<figure><img src="/files/f68hYLN4jjJhEnosPUCA" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.realvirtual.io/basics/reusable-components-prefabs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
