1. Register Unity ID
Click the avatar icon in the upper right corner of the Unity official website创建Unity ID
to register Unity ID, click to enter the registration content, pass the man-machine verification, click 创建Unity ID
If you register by email, open the email, click to activate the email
Bind mobile phone
You can also bind WeChat, after logging out (do not log out if the server is unstable), log in with WeChat and bind the registered account
2. Download and install Unity Hub
Visit Unity Official Download
Click Unity Hub
to Windows 下载
download Unity Hub
3. Open Unity Hub
Click to Sign in
log in. It is recommended to use the mobile phone number to accept the verification code to log in.
The Install Unity Editor window will pop up
. Click Install Unity Editor
to install Unity
. Click Agree and get personal edition license
to agree to the personal license (or click Choose another plan
to pay)
and wait for the installation to complete.
4. Set the language of Unity Hub
Click the gear icon to enter the preference setting
Click Appearance
to enter the appearance setting Language
, English
change from to 简体中文
, close the preference setting window
5. Set the Unity language and install VS2019
On the left column, install, select the installed Unity
, click the gear icon on the right, click 添加模块
, check 简体中文
, click 继续
, check the terms of agreement, click 安装
(at this time, Visual Studio Community 2019 will also be installed by the way, which will be used later)
After opening a project, change the menu bar, Edit, Preferences, Languages, Editor language to 简体中文 (Experimental)
reopen the project to take effect
6. Tutorial website learn.u3d.cn
learn.u3d.cn
learn.u3d.cn
can use WeChat to log in and bind the mobile phone number, and then bind the Unity ID after logging in. It is unity.cn
not an account system
1. Create a new project
Unity Hub, left column, project, upper right corner, click 新项目
left column, template selection 核心模板
, select 2D核心模板
(this is a 2D empty project)
On the right side, the project name is changed to Unity Project 推箱子
the lower right corner, click 创建项目
to prompt 安装PlasticSCM
, click 确认
to prompt that the PlasticSCM download is successful, click 安装
to wait for loading, wait a little longer
2. Add material
Drag sokoban
the folder into the Assets window,
select 4 pngs, change the number of pixels per unit to 128
, click another location to trigger saving
3. Game window
Switch from the scene window to the game window
Free Aspect
to WXGA (1366x768)
switch back to the scene window after modification
4. Add tile map
In the upper left corner of the hierarchy window, click on +
the 2D object, tile map, and
Tilemap
rename the rectangle towail
5. Tiled Palettes
Menu Bar, Window, 2D, Tile Palette
Create a new palette, rename it 0map
, click 创建
, click 选择文件夹
Drag an image wail
to the Tile Palette window, rename it 0wail.asset
, click保存
Select it in the tile palette window wail
and draw a wind map in the scene window
6. Add collision to the wall, static
Hierarchy window, select Grid
thewail
In the inspector on the right, click 添加组件
, in the Tilemap Collider 2D
(Tilemap classification) attribute, check the
Tilemap Collider 2D
由复合使用
Add another Composite Collider 2d
(Physics 2D)
Rigidbody 2D
body type toKinematic
7. Add characters
Drag worker
and drop the picture into the layer layer
, select worker
the inspector, position, X: -0.5, Y: 0.5
(if you want to drag manually, you can activate the four-way arrow button in the scene)
8. Add worker components
Layer, select worker
the inspector, add components, change the size of Box Collider 2D
(Physics 2D)
Box Collider 2D
to X 0.95, Y 0.95
inspector, add components, change the body type of Rigidbody 2D
(Physics 2D) to
Rigidbody 2D
Kinematic
9. Add boxes
Drag box
and drop the picture into the layer layer
, select box
the inspector, position, X: -1.5, Y: 0.5
10. Add box components
Layer, select box
the inspector, add components, change the size of Box Collider 2D
(Physics 2D)
Box Collider 2D
to X 0.95, Y 0.95
11. Duplicate boxes
Hierarchy, select box
Ctrl+C, Ctrl+V, copy 3, a total of 4 boxes
and rename box1
box2
box3
box4
them as
X:-1.5, Y: 0.5
X: 0.5, Y: 1.5
X: 1.5, Y: -0.5
X :-0.5, Y:-1.5
12. Add box storage
Drag box_place
and drop the picture into the layer layer
, select box_place
the inspector, position, X: -3.5, Y: 0.5
13. Add box storage component
Layer, check box_place
the inspector, add components, change the size of Box Collider 2D
(Physics 2D)
Box Collider 2D
to X 0.95, Y 0.95
check 是触发器
the inspector, add components, change the body type of Rigidbody 2D
(Physics 2D) to
Rigidbody 2D
Kinematic
14. Duplicate box depository
Hierarchy, select box_place
Ctrl+C, Ctrl+V, copy 3, a total of 4 box storage locations
and rename box_place1
box_place2
box_place3
box_place4
them as
X: -3.5, Y: 0.5
X: 0.5, Y: 3.5
X: 3.5, Y: - 0.5
X: -0.5, Y: -3.5
15. Display
Hierarchy
Scene
Tile Palette
Assets
16. Scripting settings
Unity, menu bar, edit, preferences, external tools
will be Open by file extention
changed to Virtual Studio Community 2019
this setting will be lost, and if Visual Studio can’t associate the code of the game engine, then reset it
17, screenplay
In the lower left corner, project, right click Assets
, create, folder, named as Scripts
Right click Scripts
, create, C# script, named asworker
Right-click worker
the script file and open it
(if there are the following contents, process according to the content: select an application to open this .cs file, double-click to select Microsoft Visual Studio 2019; log in to the Visual Studio interface, select, and then 以后再说
click 启动 Visual Studio(S)
)
18. Write mobile script worker
1. View the script content
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class worker : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Change to the following template
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class worker : MonoBehaviour
{
// 向量
// 图层遮罩1
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 移动
// 判断移动
}
Two, will
// 向量
changed to
// 向量
Vector2 moveVec;
Operation: if
press twice after input Tab
to insert "if" fragment
Three, will
// 移动
changed to
// 移动
public void move(Vector2 moveVec)
{
transform.Translate(moveVec);
}
Four, will
// Update is called once per frame
void Update()
{
}
changed to
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
moveVec = Vector2.up;
}
else if (Input.GetKeyDown(KeyCode.A))
{
moveVec = Vector2.left;
}
else if (Input.GetKeyDown(KeyCode.S))
{
moveVec = Vector2.down;
}
else if (Input.GetKeyDown(KeyCode.D))
{
moveVec = Vector2.right;
}
if (moveVec!=Vector2.zero)
{
move(moveVec);
}
moveVec = Vector2.zero;
}
Five, Ctrl+S to save
19. Mount script worker
Go back to the Unity
level, select worker
the item in the lower left corner, Assets, Scripts, and drag the script to the button in worker
the inspector window on the right添加组件
20. run
Click the triangle run button at the head-up position (or click the menu bar, edit, play (Ctrl+P))
to enter WASD, and find that the character can move freely on the map, and can overlap with boxes or walls (covered or covered)
Click the triangle run button again to end the run
21. The Chinese of the script is garbled in Unity's inspector
Unity, project, right click Assets
, display in the resource manager
to create a new file .editorconfig
, enter the following content to save
root = true # 所在目录是项目根目录,此目录及子目录下保存的文件都会生效
[*] # 对于所有文件
charset = utf-8 # 文件编码格式
insert_final_newline = true #文件结尾添加换行符,以防警告
Subsequent changes to the code trigger recompilation and it will take effect
22. Layers
Click worker
(or other objects), right inspector, layer, click named 添加图层
User Layer 6
aswail
User Layer 7
box
Press and hold Shift to select 4 boxes, change the layer to 7: box
Expand the wail under the Grid, change the layer to6: wail
23. Write ray detection
One, will
// 图层遮罩1
changed to
// 图层遮罩1
public LayerMask wailandbox;
2.
Save, go back to the Unity
level, select worker
, inspector, find the script, Wailandbox, check wail
andbox
3. Back to Visual Studio
will
// 判断移动
changed to
// 判断移动
public bool canMove(Vector2 moveVec)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, moveVec, 1f, wailandbox);
if (hit)
{
// 检测到墙或者箱子
}
else
{
return true;
}
return false;
}
Four, will Update
be
if (moveVec!=Vector2.zero)
{
move(moveVec);
}
changed to
if (moveVec!=Vector2.zero && canMove(moveVec))
{
move(moveVec);
}
5. When running at this time, the character can move, but cannot pass through walls or boxes
24. box script
1. In the lower left corner, project, right click Scripts
, create, C# script, name itbox
2. Double-click box
the script to open it
and replace it with the following template
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class box : MonoBehaviour
{
//图层遮罩2
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 移动
// 触发器
}
Three, will
// 图层遮罩2
changed to
// 图层遮罩2
public LayerMask wail;
Four, will
// 移动
changed to
// 移动
public bool moveToVec(Vector2 vec)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, vec, 1f, wail);
if (!hit)
{
transform.Translate(vec);
return true;
}
return false;
}
5. Save
6. Mount Go
back to the Unity
level, select 4 box
items in the lower left corner, Assets, Scripts, drag the script to the button on box
the right inspector window 7. Set the layer Go back to the Unity level, select 4 , Inspector, Find Script, Wail, Tick添加组件
box
wail
25, worker script
// 检测到墙或者箱子
changed to
// 检测到墙或者箱子
if (hit.collider.gameObject.GetComponent<box>() != null)
{
return hit.collider.GetComponent<box>().moveToVec(moveVec);
}
26. run
Run the game, the character can move, can push the box, can't go through the wall, can't go through the box, can't keep pushing when the box is blocked by the wall
27. Add Rigidbody 2D to the box
Layer, select 4 box
, inspector on the right, add component, Physics 2D, Rigidbody 2D
body type toKinematic
28. Modify the label of the box storage place
Level, select 4 box_place
,
inspector, label, add label, click +
, name the label as box_place
, clickSave
Select 4 again box_place
and change the label tobox_place
29. box trigger
// 触发器
changed to
// 触发器
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "box_place")
{
collision.gameObject.SetActive(false);
}
}
30. run
The character pushes the box to the target point, the target point disappears, and the box turns gray
31. How to print out
Use Unity's Debug.Log statement to print output
Modify the code of worker.cs
// Start is called before the first frame update
void Start()
{
}
changed to
// Start is called before the first frame update
void Start()
{
Debug.Log("绑定在" + this.ToString() + "上的脚本运行了");
}
Back in Unity, switch to the console window, run
32. Generate
Unity, Ctrl+S to save the project
Unity, menu bar, file, click Build Settings...(Ctrl+Shift+B) to open the Build Settings window,
select Windows
the target platform, click 生成
the button
path to select the desktop, create a new folder on the desktop 推箱子
, select the newly created folder, click选择文件夹
run Unity Project 推箱子.exe
, Alt+F4
end run