Create 2D Sprite Prefab: Rock
Last updated
Last updated
In this section, we'll create a 2D sprite gameObject that will have 2D physics configured to act on the object. Once the gameObject has been configured, then we'll create a Prefab from the gameObject, this allows us to store the custom gameObject as a named asset which is stored independently from a specific scene. One use of prefabs is that they can be used for dynamically generating gameObjects at run-time via code.
In the project / asset panel, right click and select: Create > Sprite > Select the sprite shape, such as 'diamond'. This sprite can be used as the Sprite for a SpriteRenderer component on a 2D Sprite gameObject. The color can be set using the colorSelector once the sprite has been added to a 2D Sprite gameObject. This color can also be used to tint an image, however, remember to select white as the color to show an un-tinted image used as a sprite.
Open a finder / explorer window on your system, browse to the folder that contains your image file.
Drag the image file directly into the Project / Asset panel at the bottom of your Unity window.
If you have configured your game as a 2D game, then the sprite should be ready to use. Otherwise, with the image selected in your project panel: in the Inspector panel, select the options shown in the image below:
The animation below shows how to create a place-holder sprite, and how to import an external image, after locating the image file in an explorer or finder window. The place-holder sprite is set as the sprite for the basket gameObject.
In the animation below, we change the sprite used for the basket from the square placeholder sprite, so that it's now using a .png sprite file that was dragged into the asset folder. After making the change, we reset the color so it's not tinting the image.
Select from the top menu: GameObject > 2D > Sprite
Select the gameObject in the Hierarchy Panel, change the name, in this case: Rock.
With the Rock gameObject still selected in the hierarchy, do the following steps:
Check that the gameObject's transform.position.z value = 0;
Select a Sprite for the SpriteRenderer Component, do this by selecting the small circle icon in the SpriteRenderer component, this brings up the Asset / Scene pop-up....Make sure to select a sprite from the Asset tab (not the Scene tab). This sprite can either be a placeholder sprite, or an imported sprite.
Add the following Components in the Inspector Panel, configure as detailed below:
Rigidbody2d - dynamic type selected
Collider2D - Select CircleCollider2D, BoxCollider2D, or PolygonCollider2D, edit points if necessary.
Add a Script component - add either Rock.cs or PickUp.cs script
Set the Script component's pointValue to some negative value in the inspector
Add Tag: "Rock" to the gameObject (top of inspector panel). (If the tag doesn't exist, select 'addTag', and create a new tag: "Rock", then add to your gameObject
Once the gameObject has been configured as shown in the animation below, drag it to your Project / Assets panel to turn it into a prefab. The text should show as blue once it is a prefab.
The animation clip below shows the above steps to create a 2D sprite gameObject, Rock, that can be used as a dropped object in our game.