Once you’ve cut the material, you can use the image resource. Dragging an image into the hierarchy window to edit the map in the scene is the basic method. The obvious problem is that creating a level requires repeatedly copying the image resources to adjust the position, which is extremely inefficient. Especially on complex maps, even more levels can take a lot of time. So use the TileMap system provided by Unity.
1. Basic use method of Tile Map, import Sprite group and draw scene Map
1.1 Hierarchical Windows Add TileMap
After the TileMap is created, Grid will appear in the scene. Rename it to Background in the drop-down menu to make it the Background and save the current scene.
1.2 Start tile Drawing palette to create a Map
The Window – 2 d – Tile Palette
Drop-down menuCreate New Palette
Create a new Palette in theMap
, and save it in the project folder TileMap – TilePalette.
1.3 Put tile information into the scene
Drag the cut atlas to the Map and save it in the destination folderTileMap--Tiles
In the.
If you need to modify the atlas information here, you can click the Edit button and use the upper toolbar to operate the current atlas. IfTo make no changes, close the Edit button
.
⚠️, in the Palette menu, has a columnActive TileMap
, it refers to the currently activated TileMap, that is, the map on which TileMap, you can select the corresponding platform to draw from the drop-down menu.You need to leave a message before each operation. Who is the active TileMap?
1.4 Drawing Background Background
Start by changing the size of the MainCamera to 7
In the hierarchy windowGrid--Background
, select the tiles you need in Paletee and use the square tool to draw map information in a large area of the scene.
⚠️ When removing tile information, the background will be revealed, and the subsequent loading of the player will require it to be placed on the platform without colliding with the background, so you need to create a new TileMap storage platform under the Grid, etc.
2. Install the 2D-Extra extension package
Use unity’s official 2D-Extra extension pack to add additional features to the TileMap system. Official address: github.com/Unity-Techn…
2.1 Downloading the ZIP file directly
Download the zip package and decompress it in the project folder
2.2 Network Installation
If network conditions are good, copy the Git address bar.
https://github.com/Unity-Technologies/2d-extras.git
Copy the code
In the menu barWindow -- Package Manager -- Add package from git URL
Add the address
Once installed, in the New folder right-click menu you can see the added tiles in the extension pack.
3. Create Rule tiles
3.1 Adding Rules
Create a RuleTile named Ground, which generates a tile according to the rules, and add the rules for the tile in the Inspector on the right:
In the nine cells of the picture, the center represents the position of the picture, and the periphery represents the 8 directions around the picture. The green arrow indicates that the arrow direction of the currently selected image can survive the image, whereas the Red Cross indicates that the specified direction cannot be generated.
Drag the Ground defined by the rules into Tile Paletee, and then draw a grid on the Platform as shown on the left. The resulting grid is a walled grid with the center empty according to the rules.
3.2 Supplement for missing inner corners
In the actual drawing process, there may be the following situation, in the corner place present right Angle, resulting in the loss of inner Angle.
Add four interior corners to fill and fill all eight directions of the space. As shown below:
3.3 Adding backgrounds using Rules
Adding regular tiles to the background in the same way is more realistic than adding rules around the background in 1.4. As shown below:
4. Modify the game rendering hierarchy
Modify theSorting Layer
To add a hierarchy to the game scene, you can add a hierarchy of Background, and whoever is underneath will be shown first.
Therefore, according to the first four steps using TileMap to draw their own game scene as shown below
5. Add a Collider to the scene
Although the scenes are drawn on different layers, they are actually pictures, and there is no real physical simulation between pictures. Characters to be created, environmental factors, or enemies to be placed on platforms. Add a TileMapCollider2D to Platform so that it can physically collide with other objects.
As you can see from the image above, there are many green squares on the platform, which actually adds a box collider for each tile on the platform. If the tiles on the drawing platform are not square, the surface of the platform will be uneven and the characters will have problems in the process of movement. Now we need to add an extra collider,Composite Collider2D
A rigid body will also be added.
The rigid bodyChange Body Type to Static
, fixed to ensure that the game running scene does not fall off the screen. Note that when the BodyType is Dynamic and the Gravity Scale is set to 0, the scene will not fall off the screen, but it will be affected by physical collisions, such as when the character jumps to the platform and falls with the platform.
In this case, the scene is a piece of collider, and the TileMapCollider2D needs to be addedUsed By Composite check
Turn the scene into a single collider.
6. Create other objects in the scene
6.1 Modifying parameters of objects to be added to the scene
Create interactive objects that form the background elements of the scene to add to the fun of the game. Find some scene elements provided in the material and drag them into the hierarchy directory.
Before using these scene elements, you need to change their pixel units to 32Pixels Per Unit -- 32
, filter mode is changed to point sampling modeFilter Mode -- Point
The screen pixel will find the nearest tile pixel as output, which is rough, but good performance, not anti-aliasing. And compress to NoneCompression -- None
.
6.2 Add gravity effects to scene objects
Take the chair for example, and drag it down to the hierarchy,Change Order in Layer to 1
Placed at the top of the scene. And for itAdd Rigidbody2D component
, mainly considering the Mass and GravityScale gravity ratio. The default setting for gravity is -9.8, which simulates downward gravity. Mass does not affect the speed of downward gravity.
If you add a collider BoxCollider2D, it will use the empty part of the box as the collider. If the chair is rotated sideways, it will not be a real landing effect. In order to simulate the bomb explosion in the game can blow up all the objects around, so needAdd a PolygonCollider2D
. Use EditCollider to drag and drop the shape point to edit the Angle to make it more like a real chair Angle, as shown below.
Follow the above method to add polygon colliders for other scene objects, mainly divided into three steps:
- Change Order in Layer to 1
- Add Rigidbody2D
- Add PolygonCollider2D and modify shape points
When the game starts, all the environment objects fall to the bottom of the platform under the influence of gravity.
7.2 Relationship between physical environment collisions in D
7.1 Adding the Layer Layer
When objects in the scene are stacked on top of each other, the game starts bumping into each other. As shown in the picture below, the bottles are stacked on top of the table and collide with it.
The Layer option in Unity’s hierarchy menu sets layers by default that collide with each other.
Open up the 2D collision environment,Menu -- Edit -- Project Settings -- Physics 2D
The areas selected by the red box are the system’s default layers. Check box indicates that the current game object layer can collide with other layers.
Characters in the game do not collide with the Beijing in the game when they move, but only collide with these objects when attacking. So you need to put different layers on the game objects so that they have different collision relationships.
- Add two new layers, Ground and Enviroment
- Set the background platform to Ground and change all game scene objects to Enviroment.
- Turn off collisions between game objects for testing
7.2 Set different qualities of game objects
Game objects of different mass react differently when they are stressed. For example, if you change the table’s gravity mass to 3, it will be stronger for a bottle with a specific gravity of 1.
7.3 Adjusting Other Parameters
Rigibody2D Collision Detection checks the collision relationship. Let it continuously detect the collision environment and avoid passing through the wall during the subsequent rapid movement.
8. Generate preforms
Create folder Prefabs under Assets and save the prefab.