Saving assets created in Unity as Prefabs

You can easily create primitive geometry with Unity. In this recipe, we will create a game object from Unity's resources and keep it in our project as a Prefab.

How to do it...

To create a Prefab, follow these steps:

  1. Inside the Unity editor, navigate to GameObject | Create Other | Sphere.
  2. In the Hierarchy view, right-click Sphere and choose the appropriate option from the context menu to rename it to Cue Ball.
  3. Now, in the Project view, click on the Create button and choose the Material option. Then, rename the new material to Cue Ball Material.
  4. In the Project view, select Cue Ball Material. Then, in the Inspector view, change its Shader value to Specular.
  5. Also, set Specular Color to white and set its Shininess to the maximum, as shown in the following screenshot:
    How to do it...
  6. From the Project view, drag Cue Ball Material into the Cue Ball game object, in the Hierarchy view.
  7. Select Cue Ball. Then, access Component | Physics | Rigidbody. That should attach a Rigidbody component to that game object.
  8. Now that your game object is complete, click on the Create button in the Project view and choose the Prefab option. Then, rename it to Cue Ball Prefab.
  9. Drag the Cue Ball game object from the Hierarchy view into the Prefab in the Project view. Your game object is ready to be re-used in this project.
    How to do it...

How it works...

In Unity, game objects can be stored as Prefabs. This is very useful in case you want to re-use a game object in several levels or instantiate it through scripting. Adobe Flash users can think of it as the Unity equivalent of MovieClips.

There's more...

There are many other ways to use Unity's built-in resources. Here are some ideas:

Adding external files

In this recipe, we haven't used any external asset. However, there's no reason you could not have imported a texture and used it as the Cue Ball Material base map, for instance.

Taking your Prefabs to another project

Also, if you plan of re-using your Prefab in other projects, you can do it by exporting it as a custom package.

Creating other kinds of game objects

As you have probably noticed, spheres are not the only entities you can create directly with Unity. Other primitives are also available, as well as many other types of entities: lights, camera, GUI textures, and so on. Navigate to GameObject | Create Other and experiment with the options available.

See also

  • The Exporting Custom packages from your project recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.182.62