# Unity-Programming-Patterns-Chinese **Repository Path**: muzan/unity-programming-patterns-chinese ## Basic Information - **Project Name**: Unity-Programming-Patterns-Chinese - **Description**: 在 Unity 中实现编程设计模式的示例(使用 C#),以及何时使用它们。 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-03 - **Last Updated**: 2025-08-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Unity中的游戏编程模式 在这里,你可以找到一系列适用于Unity的编程(设计)模式,这些模式主要来源于书籍 [Game Programming Patterns](http://gameprogrammingpatterns.com)。随着你的Unity项目不断扩展,这些模式可以帮助你更好地组织代码,因为它们总结了最佳实践,并提供了解决常见问题的方法。 你并不一定要使用这些模式,而应将它们视为工具箱中的工具。你还可以根据具体需求,对这些模式的实现方式进行调整和优化。一些模式,例如 **Update**、**Game Loop** 和 **Component**,已经被Unity内置,因此你在开发中其实已经在使用它们了! ## **编程模式的分类** 1. **架构模式(Architectural Patterns)** 例如 MVC(模型-视图-控制器)。 2. **设计模式(Design Patterns)** 比架构模式更具体,例如单例模式(Singleton)。 3. **反模式(Anti-Patterns)** 这些模式虽然被程序员用来解决问题,但实际上并不是高效的解决方案。例如“上帝对象(God Object)”,通常以 `GameController` 命名,并负责管理游戏中的所有内容。这种类的缺点是随着代码量的增长,维护和调试将变得极其困难。 ## **《Game Programming Patterns》书中的模式** 1. [命令模式(Command)](_text/01-command.md) 2. [享元模式(Flyweight)](_text/02-flyweight.md) 3. [观察者模式(Observer)](_text/03-observer.md) 4. [原型模式(Prototype)](_text/04-prototype.md) 5. [单例模式(Singleton)](_text/05-singleton.md) 6. [状态模式(State)](_text/06-state.md) 7. [双缓冲模式(Double Buffer)](_text/07-double-buffer.md) 8. [游戏循环(Game Loop)](_text/08-game-loop.md) 9. [更新方法(Update Method)](_text/09-update-method.md) 10. [字节码模式(Bytecode)](_text/10-bytecode.md) 11. [子类沙箱模式(Subclass Sandbox)](_text/11-subclass-sandbox.md) 12. [类型对象模式(Type Object)](_text/12-type-object.md) 13. [组件模式(Component)](_text/13-component.md) 14. [事件队列模式(Event Queue)](_text/14-event-queue.md) 15. [服务定位器模式(Service Locator)](_text/15-service-locator.md) 16. [数据局部性模式(Data Locality)](_text/16-data-locality.md) 17. [脏标记模式(Dirty Flag)](_text/17-dirty-flag.md) 18. [对象池模式(Object Pool)](_text/18-object-pool.md) 19. [空间分区模式(Spatial Partition)](_text/19-spatial-partition.md) ## **其他设计模式** 20. [装饰器模式(Decorator)](_text/20-decorator.md) 21. [工厂模式(Factory)](_text/21-factory.md) 22. [外观模式(Facade)](_text/22-facade.md) 23. [模板模式(Template)](_text/23-template.md) 需要注意的是,这些模式并非全部的设计模式。例如,我最近阅读了一本名为《Machine Learning Design Patterns》的书,其中包含了更多专注于机器学习问题的设计模式。随着学习的深入,我会不断添加与游戏开发相关的设计模式。 --- ## **来源与推荐阅读** - [Game Programming Patterns](http://gameprogrammingpatterns.com) - [Game Development Patterns with Unity 2021](https://www.amazon.com/Game-Development-Patterns-Unity-2021/dp/1800200811) - [Head First Design Patterns](https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124) - [Game Programming Gems](https://www.amazon.com/Game-Programming-Gems-CD/dp/1584500492) - [Game Programming Gems 2](https://www.amazon.com/Game-Programming-Gems-GAME-PROGRAMMING/dp/1584500549) - [Refactoring Guru](https://refactoring.guru/design-patterns) - [C# 设计模式与实时案例](https://dotnettutorials.net/course/dot-net-design-patterns/) - [Unity 官方资源:提升你的游戏编程模式](https://resources.unity.com/games/level-up-your-code-with-game-programming-patterns) --- ## **特别感谢** - **[masoudarvishian](https://github.com/masoudarvishian)** 贡献了事件队列模式、服务定位器模式的实现,并修复了相关错误。 - **[VladimirMirMir](https://github.com/VladimirMirMir)** 参与了错误修复。 - **[JayadevHaddadi](https://github.com/JayadevHaddadi)** 修正了事件代码。