构建发布与综合实战

20.1 构建发布基础

Build Settings

File → Build Settings(Ctrl+Shift+B)

设置项 说明
Scenes in Build 构建包含的场景列表(顺序决定 Build Index)
Platform 目标平台
Player Settings 玩家设置(公司名、图标、分辨率等)
Development Build 开发构建(包含 Profiler、Debug 等)
Autoconnect Profiler 自动连接 Profiler
Compression Method 压缩方式(Default/LZ4/LZ4HC)

Player Settings 核心配置

分类 配置项
图标 不同平台的默认图标
分辨率 默认全屏、可调节分辨率
其他设置 渲染管线、颜色空间、脚本后端(Mono/IL2CPP)
发布设置 包名(Bundle Identifier)、版本号

20.2 多平台发布要点

Windows 发布

  • Build Settings → PC, Mac & Linux Standalone
  • 导出为 EXE + 数据文件夹
  • 可选 x86 或 x64 架构
  • Fullscreen Mode:ExclusiveFullScreen(独占全屏)/ FullScreenWindow(窗口全屏)

Android 发布

  1. 安装 Android Build Support 模块
  2. 配置 JDK / SDK / NDK 路径(或使用 Unity Hub 自动安装)
  3. Player Settings:
    • Package Namecom.公司名.产品名
    • Minimum API Level:建议 API Level 24+
    • Texture Compression:ETC2(默认)或 ASTC
  4. Build 生成 APK 或 App Bundle(AAB)

iOS 发布

  1. 需要 macOS 环境和 Xcode
  2. 安装 iOS Build Support 模块
  3. 生成 Xcode 工程后在 Xcode 中签名和发布
  4. 需要 Apple Developer 账号

WebGL 发布

  • 导出为 HTML5 + JavaScript
  • 注意:不支持多线程、文件系统受限
  • 适用于网页游戏展示(如 itch.io)

20.3 综合实战:2D 平台跳跃小游戏

将前面所学的知识整合,完成一个完整的 2D 平台跳跃小游戏。

游戏设计

1
玩法:左右移动 + 跳跃 + 收集金币 + 到达终点

项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Assets/
├── Scenes/
│ └── GameScene
├── Scripts/
│ ├── PlayerController.cs
│ ├── Coin.cs
│ ├── GameManager.cs
│ ├── UIManager.cs
│ └── CameraFollow.cs
├── Prefabs/
│ ├── Player.prefab
│ ├── Coin.prefab
│ └── Platform.prefab
├── Sprites/
├── Audio/
└── Materials/

玩家控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class PlayerController : MonoBehaviour
{
[Header("移动参数")]
[SerializeField] private float moveSpeed = 8f;
[SerializeField] private float jumpForce = 12f;

[Header("碰撞检测")]
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform groundCheck;
[SerializeField] private float checkRadius = 0.2f;

private Rigidbody2D rb;
private SpriteRenderer sprite;
private float moveInput;
private bool isGrounded;

void Start()
{
rb = GetComponent<Rigidbody2D>();
sprite = GetComponent<SpriteRenderer>();
}

void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if (moveInput > 0) sprite.flipX = false;
else if (moveInput < 0) sprite.flipX = true;

if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}

void FixedUpdate()
{
rb.velocity = new Vector2(
moveInput * moveSpeed,
rb.velocity.y
);

isGrounded = Physics2D.OverlapCircle(
groundCheck.position,
checkRadius,
groundLayer
);
}

void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, checkRadius);
}
}
}

金币收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Coin : MonoBehaviour
{
[SerializeField] private int scoreValue = 10;
[SerializeField] private AudioClip collectSound;

void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
GameManager.Instance.AddScore(scoreValue);
AudioSource.PlayClipAtPoint(collectSound, transform.position);

// 播放收集特效
// Instantiate(collectEffect, transform.position, Quaternion.identity);

Destroy(gameObject);
}
}
}

游戏管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }

[SerializeField] private int totalCoins;
private int currentScore;

void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}

public void AddScore(int points)
{
currentScore += points;
UIManager.Instance.UpdateScoreText(currentScore, totalCoins);
}

public void GameOver()
{
Time.timeScale = 0f;
UIManager.Instance.ShowGameOverPanel(currentScore);
}

public void RestartGame()
{
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}

完整的开发流程

  1. 搭建场景:用 Tilemap 绘制地面和平台,放置玩家出生点、金币和终点
  2. 创建预制体:玩家、金币、平台分别制作预制体
  3. 编写脚本:玩家控制 → 金币收集 → UI 管理 → 游戏管理 → 摄像机跟随
  4. 添加音效:跳跃、收集金币、游戏结束音效
  5. 测试调整:调整移动速度、跳跃力度、关卡设计
  6. 性能检查:使用 Profiler 检查 Draw Calls 和 GC
  7. 构建发布:选择目标平台,配置 Player Settings,Build

20.4 教程系列总结

知识图谱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
第一阶:入门筑基
├── 01 Unity 简介与环境搭建
├── 02 编辑器界面详解
├── 03 GameObject 与组件系统
├── 04 脚本基础与生命周期
└── 05 场景管理与坐标系统

第二阶:核心机制
├── 06 输入控制与玩家交互
├── 07 物理引擎与刚体组件
├── 08 碰撞检测与触发事件
├── 09 预制体与动态实例化
└── 10 摄像机控制与视野

第三阶:系统扩展
├── 11 UI 系统入门
├── 12 动画系统基础
├── 13 协程与异步编程
├── 14 音频系统
└── 15 数据持久化

第四阶:进阶实战
├── 16 对象池技术
├── 17 导航寻路系统
├── 18 粒子特效系统
├── 19 性能优化入门
└── 20 构建发布与综合实战

推荐学习路径

  • 初学者:按 01→20 顺序逐章学习,完成每章练习题
  • 有基础读者:跳过基础章节,重点关注异步编程、对象池、优化等进阶章节
  • 实战导向:先看第 20 章的实战项目,遇到不懂的回头查找对应章节

下一步方向

本系列之后可以继续探索的方向:

  • URP/HDRP 渲染管线:实现更精美的画面效果
  • Addressables 资源管理:现代资源加载方案
  • DOTS(Entities + Job System + Burst):高性能数据导向架构
  • Shader 编程:自定义着色器效果
  • 网络编程:Mirror / FishNet 多人联机
  • 编辑器工具开发:自定义窗口和编辑器扩展

20.5 练习题

  1. 完成本章的 2D 平台跳跃游戏,确保所有功能正常工作
  2. 添加新的关卡和陷阱(尖刺、移动平台)
  3. 添加 Game Over 后显示分数的排行榜(PlayerPrefs 或 JSON)
  4. 为游戏添加粒子效果(金币收集特效、死亡特效)
  5. 将游戏发布到 WebGL 并在浏览器中测试
ByteFisher
分享编程技术 · 记录钓鱼乐趣
扫码关注
▸ 扫码关注 ◂
Unity3D开发基础教程 系列
第 20/20 篇
分享: