示例 Unity 项目结构(Playable Game Template)

一、整体设计原则(先说清楚,博客加分)

这套结构遵循 4 个原则:

  1. 按职责分层,而不是按类型乱放

  2. 脚本 = 行为,而不是"万能控制器"

  3. 数据与逻辑分离

  4. 允许项目在小规模下保持简单


二、推荐目录结构(Assets)

复制代码
Assets/
├── Scenes/
│   ├── Bootstrap.unity
│   └── Game.unity
│
├── Scripts/
│   ├── Core/
│   │   ├── GameManager.cs
│   │   └── GameState.cs
│   │
│   ├── Player/
│   │   ├── PlayerMove.cs
│   │   ├── PlayerJump.cs
│   │   └── PlayerHealth.cs
│   │
│   ├── Systems/
│   │   ├── InputSystem/
│   │   │   └── PlayerInput.cs
│   │   ├── AudioSystem/
│   │   │   └── AudioManager.cs
│   │   └── UISystem/
│   │       └── UIManager.cs
│   │
│   ├── Gameplay/
│   │   ├── Goal.cs
│   │   └── DeadZone.cs
│   │
│   └── Utils/
│       └── Timer.cs
│
├── ScriptableObjects/
│   ├── PlayerConfig.asset
│   └── LevelConfig.asset
│
├── Prefabs/
│   ├── Player.prefab
│   └── UI.prefab
│
├── Art/
│   ├── Sprites/
│   └── Animations/
│
├── Audio/
│   ├── BGM/
│   └── SFX/
│
└── Settings/
    └── InputActions.inputactions

三、各目录的「工程意义」(博客重点)

1️⃣ Scenes ------ 游戏流程层

复制代码
Scenes/
 ├── Bootstrap.unity
 └── Game.unity
  • Bootstrap:初始化全局系统(音频、配置)

  • Game:真正的游戏内容

📌 工程思想:

把"启动逻辑"和"玩法逻辑"分开

后期加菜单 / 多关卡会非常舒服


2️⃣ Scripts/Core ------ 游戏生命周期控制

复制代码
Scripts/Core/
 ├── GameManager.cs
 └── GameState.cs
  • GameManager:控制流程

  • GameState:枚举状态

    public enum GameState
    {
    Ready,
    Playing,
    Win,
    Lose
    }

📌 好处:

  • 不用到处写 bool isGameOver

  • 博客里很好解释「状态驱动」


3️⃣ Scripts/Player ------ 行为拆分示例

复制代码
Player/
 ├── PlayerMove.cs
 ├── PlayerJump.cs
 └── PlayerHealth.cs

不要这样做:

复制代码
PlayerController.cs(1000 行)

📌 博客可强调:

Unity 鼓励用 组合,而不是继承


4️⃣ Scripts/Systems ------ 可复用系统层

复制代码
Systems/
 ├── InputSystem/
 ├── AudioSystem/
 └── UISystem/

这些系统特点:

  • 不关心具体关卡

  • 不绑定具体角色

  • 可以跨 Scene 存活

例如:

  • AudioManager 使用 DontDestroyOnLoad

  • UIManager 管界面切换


5️⃣ Scripts/Gameplay ------ 关卡内规则

复制代码
Gameplay/
 ├── Goal.cs
 └── DeadZone.cs

职责:

  • 胜利条件

  • 失败触发

  • 与 GameManager 通信

📌 关键点:

Gameplay 只描述规则,不控制流程


6️⃣ ScriptableObjects ------ 数据驱动核心

复制代码
ScriptableObjects/
 ├── PlayerConfig.asset
 └── LevelConfig.asset

用途:

  • 移动速度

  • 跳跃高度

  • 关卡参数

📌 博客亮点:

把「调参」从代码中解放出来


7️⃣ Prefabs ------ 组合结果

复制代码
复制代码
Prefabs/
 ├── Player.prefab
 └── UI.prefab
  • Player.prefab = Move + Jump + Health

  • UI.prefab = Canvas + UIManager

📌 Prefab 是组合的最终形态


四、典型对象关系图(文字版)

复制代码
GameManager
 ├── 控制 GameState
 ├── 接收 Goal / DeadZone 事件
 └── 通知 UIManager 切换界面

Player
 ├── PlayerMove
 ├── PlayerJump
 └── PlayerHealth

五、这套结构适合哪些项目?

✅ Unity 新手练手

✅ 技术博客示例

✅ 课程 / 毕设

✅ 独立游戏原型

❌ 超大型 RPG(需要更复杂架构)


六、博客写作建议(你可以直接用)

在博客中可以这样总结这一节:

这套项目结构并不是"唯一正确",

但它刻意避免了早期 Unity 项目中常见的混乱问题:

  • 脚本职责不清

  • 逻辑高度耦合

  • 后期难以扩展

对于一个「可以玩的游戏原型」,
清晰 > 完美架构

相关推荐
淡海水9 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
在路上看风景9 小时前
4.6 显存和缓存
unity
Zik----11 小时前
简单的Unity漫游场景搭建
unity·游戏引擎
在路上看风景1 天前
4.5 顶点和片元
unity
在路上看风景1 天前
31. Unity 异步加载的底层细节
unity
天人合一peng1 天前
Unity中做表头时像work中整个调整宽窄
unity
小李也疯狂2 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的2 天前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y2 天前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表