Unity3D Engine Architecture

2023-11-19

原文:http://www.umingo.de/doku.php?id=paper:mechs_and_tanks:section03

Architecture

To better understand the game's software architecture and object model it is useful to get a better look atthe Unity3D Game Engine and editor and it's key principles.

Unity3D Engine

Unity3D is an award winning tool for creating interactive 3D applications on multiple platforms.Unity3D consists of a game engine and an Editor. The engine contains software components for the most common and recurring tasks in game developement. The topics covered by the engine include sound, graphics, physics and network functions. The engine supports C-Sharp, Boo, and JavaScript for script programming.

The other component is the Unity Editor that serves as an integrated development environment with a project panel for scripts and other components, a hierarchical object inspector containing the game scene setup and a game preview window (see figure 4).It also comes with several multi-language script editors and a unique prefab system that will be explained later.

Figure 4: The Unity Editor

There are several licenses available for Unity. Unity Basic with limited features is free for PC, MAC and Web development.Other platforms or full feature set require an additional license purchase [15].

Although there are many free and proprietary alternative game engines like the Unreal Engine™ or the C4™ engine the choice fell on Unity for the following reasons:

* It is possible to deploy to Windows, Mac OSX, Web Browser, Wii, iPhone, iPad, Android, XBox 360 and Playstation 3. It is even planned to add Flash and linux deployment in the future. The deployment possbilities offer many possibilities to use the game engine or the games created with the engine for monetizing or further studies. * The Unity community is very supportive and the engine as well as the editor are well documented. * The engine is relatively easy to learn and to work with and supports the idea of rapid software development by providing all tools for quick prototyping and iterating as well as fast script compilation. * The iOS Basic license with possibility to deploy for iPhone, iPad and iPod touch comes at a relatively low price compared to other vendors. Mechs and Tanks was created with Unity 3.0, C-Sharp scripting and the MonoDevelop IDE for development. You can find a Unity Tutorial in Appendix.

Brief History of Unity3D

The following dates illustrate the evolution of the Unity Engine between 2001 and 2011 [16].

  • 2001 Unity Technologies started to develop their game engine in 2001. The main incentive at the time was to create games and to create a good tool as a foundation for these games [1].
  • 2003 In 2003 the company noticed that the resulting engine would be a great product on its own.
  • 2005 In 2005 Unity 1 launched onstage at Apple's WWDC.
  • 2007 Unity 2.0 launched in 2007 and added a terrain engine, real-time dynamic shadows and video playback among others.
  • 2008 In 2008 Unity iPhone launched and the Cartoon Network introduced FusionFall, a game that has been played by more than 8 million people.
  • 2010 In 2010 Unity 3.0 with dozens of new features like asset management and beast lightmapping was released.
  • 2011 Unity surpassed 500 000 developers and 60 million web-player installations.

Game Architecture

Mechs and Tanks' architecture consists of the module- and the Unity scene-architecture.

Main Modules

This section describes the most important modules and their relations.The game's architectural style on a subsystem level is an object network with data capsules.The following UML component diagram illustrates the subsystems and their relations.

Game logic

This module manages the current player and AI configuration the countdown timerand the current game state (paused, waiting for network reply..).

AI

The AI module contains the logic behind unit-, group- and player-AI.The unit AI makes use of diverse steering behaviours for pathfinding or obstacle avoidance and controls a unit's state.The group AI manages a group's behaviour and things like group pathfinding. On a higher level all the player's groups are managed by a player module.

The machine learning AI saves and loads it's data using the interface of the persistant data module.

Persistant data

This module is responsible for saving and loading data that should be available between different game sessions.Among others, it stores lookup tables and graphs for the pathfinding module and manages the machine learning AI's accumulateddata.

Game actors

Game actors are terrains, units or buildings in the game. Their 3D models get passed to the Unity3D rendering pipeline for visualization. Every game actor owns references to the AI modules that control it's behaviour.

Steering behaviours

Steering behaviours calculate forces that influence how and how fast an autonomous game agent should move.They can be used for obstacle avoidance, crowd movement or simple seeking tasks [8].

Pathfinding

This module is responsible for the creation of a pathgrid, gathering of obstacle information and for providing aninterface for various pathfinding requests. For better performance some information is saved to and loaded from the disk.

Input

This module keeps track of the user's input, processes it and generates the feedback.

Network

The network module is responsible for managing all game actor's states in a network game.Another responsibility is to keep the game state consistent on both machines and to avoid jittering in network unit movement.

GUI

The graphical user interface (GUI) displays all buttons, menus, the minimap and the countdown timer. It is also responsible for these element's functionality and interacts heavily with the game logic module for this purpose.

3D rendering

This module is mainly managed by Unity3D. The scene's main camera determines the objects that need to be rendered and sends them through the rendering pipeline. Unity3D encapsulates most rendering details but also offers access through pixel and vertex shaders.

Unity Scene Setup

Every map in the game is represented by a Unity3D scene. Here is what a typical scene setup in the Unity Hierarchy (a) and in the scene window (b) looks like:

A map in the Unity Hierarchy

A map in the Scene View

Now all game objects from the top to the bottom of the panel will be described.

CWalls

This object contains custom drawn wall nodes and wall edges. The alternative to using custom drawn walls is to calculatethem depending on the map's geometry.

Directional light

This light is only used to calculate the terrain lightmap. It is turned off afterwards due to performance reasons.

Game Music

Holds the game's main music and plays it on scene start.

GameController

The GameController GameObject holds and administers all GameObjects that manage the game's logic. It consists of the following objects:

CursorController

Manages the look and the logic behind the cursor.

GameInstantiator

This important GameObject is responsible for instantiating other objects that need to be created in aspecific order.

GameInstantiator in the InspectorGameInstantiator in the Inspector

GameController in the HierarchyGameController in the Hierarchy

The GameInstantiator holds referenes to the buildings on the map, the PathCreator for path creation and obstacle management, the Player GameObject that manages player-configuration and -settings, the InputControl GameObject that is used to process user input, and a reference to the PlayArea GameObject that defines the playable area of a map.

It also contains references to the player's spawn points and custom paths and walls.

GUI

Holds all the map's GUI objects.

MachineLearningController

This GameObject controls all functionality that is needed for machine learning.

spawn1, spawn2

The actual player spawn points. Spawn points are marked by green cubes in the Scene View.

HPaths

Custom path nodes and edges of the map. Custom nodes are labeled in the scene editor and outlined with red lines.

Main Camera

The scene's main camera and audio listener. All 3D sounds are observed from the camera's perspective.

PlayArea

Defines the actual playable area of a map.

Base Prefabs

The buildings that are scattered on the map.

Terrain

The Unity terrain object.

TestRunner

A GameObject that is used to run unit tests made with the Unity3D unit testingframework SharpUnit [17].

MVC Pattern

The design of the Unity Engine encourages MVC (Model View Controller) oriented engineering.In my case the structure looks like this:

 
Figure 5: MVC pattern representation of the architecture

The model contains all GameObjects, their components and data files. It has access to the GameObject's renderers and the camera object.

The view renders the models and is mainly managed by the Unity3D Engine's renderers. It needs to accessthe 3D models, textures, materials and effects that are held by the model. It also has influence on what input options are available.

The controller receives user input and reacts by calling methods on model objects. It is represented by the Input subsystem in my game. The user can influence the view with his input.

Multiplatform Development

Unity allows to deploy a project on different platforms with minor changes.The presentation and functionality is largely maintained depending on the platform's capabilities.However, there are major differences in some areas like the input mechanisms on different devices.

The abstract factory design pattern was applied to design these components.

The Abstract Factory Pattern

Figure 6: Abstract factory pattern

The abstract factory pattern (see figure 6) protects a client from different platforms that implement the same concept in different ways.A platform is a set of AbstractProduct classes. These classes represent a concept that is supported on all platforms.An AbstractFactory class declares operations for creating single products, ConcreteFactory classes represent a specific platform.

The client only uses abstract factory and abstract product methods and is therefore protected from a platform's concrete implementation.

Input and Persistant Data

The two main aspects of the game that ask for different implementation are the input-mechanisms and the usageof persistant file data. Unity3D does not support cross-plattform databases. That is why Mechs and Tanks uses persistant data as binary files on the disk. All platforms got their own implementation in respect to their capabilities and supported file formats.

In order to maintain readability and flexibility the following adaption of the abstract factory pattern for cross-platform input processing was used:

Figure 7: Excerpt of the Input subsystem structure

This is only a little excerpt of all the involved platforms and commands, chosen for demonstration purposes.

The InputManager gets attached to the InputControl GameObject in the scene. It calls the CommandImplementor's Execute method every frame inside the Update function. The Execute method of a CommandImplementor iterates through all added commands, checks if their execute condition is satisfied and calls their Execute method, if so.

These are the components of the Input subsystems's diagram in respect to the abstract factory pattern:

  Component in the input subsystem
Client InputManager
AbstractFactory CommandImplementor
ConcreteFactory CommandImplementorWindows, CommandImplementorIphone
AbstractProduct Command, InputMoveCamera, InputHover, InputMultiselect
ConcreteProduct InputWindowsMoveCamera, InputIphoneMoveCamera InputWindowsHover, InputIphoneHover,InputWindowsMultiSelect, InputIphoneMultiSelect

iOS+Unity

Mechs and Tanks was created with a process of iterative development, which is the reason why the game was already playable before any adjustments for the iPad™ were made.

This chapter describes and analyses the challenges and solutions of porting Mechs and Tanks to the iPad™ .

Maximum Polygon Count

One of the most obvious restrictions for iPad™ games is the number of polygons the graphics chip is able to render each frame.It turned out that more than 30 000 polygons per frame started to drop the framerate below 25 FPS on the iPad™ . After reducing the polygon count inside a 3D modeling software by manually removing edges and applying pre-defined algorithms for polygon reduction and even rebuiilding some models from the start, the current polygon counts on all platforms are:

  Mech Tank Runner Building
Polygon count 350 226 356 385

The average polygon count is about 300 per unit. If all 24 enemy and player units are captured by the camera in one frame the resulting polygon count would be 7200 on average and about 8500 in the worst case of 24 runners.Adding the maximum 4000 terrain polygons (the rest of the terrain gets frustum culled), the heaviest frame would give the GPU about 12 500 polygons to render.

Draw Call Reduction and Lights

Even more important for decent performance on mobile devices is the number of draw calls per frame. A draw call is issued to the GPU every time a model is drawn. If the model has n submeshes it will cause at least n draw calls.Every GUI texture, selection plane and health bar adds one draw call to the scene.

Another source for additional draw calls is per pixel lighting that causes an additional draw call for every light pass.That is why all submeshes in a model were combined into one and models don't use dynamic lighting at all. The lights for all model and terrain textures are baked in. Light and shadow baking calculates the brightness for each texel that gets illuminated by static light sources and lays a lightmap over the texture. The models look as if they are effected by lights although no calculations are made. Figure 8 shows a few models without lightmaps or light effects.

Figure 8: Screenshot with Unlit Texture material shader and no light baking for models

Figure 9: Game view with expanded stats window in the top right corner

It is to say that Unity can combine a number of objects, sharing the same material, at run-time and draw them together in a single draw call. This method is called dynamic batching [18].Figure 9 shows a scene with 66 draw calls that are caused by the following sources:

GUI

8 draw calls for buttons, minmap, countdown and countdown text + 24 unit map spots + 8 building map spots + 1 camera map spot. Sums up to 41 GUI caused draw calls.

Terrain

4 draw calls. One for each texture.

Visible Models

14 draw calls. Each model would cause 3 draw calls on its own. One for the unit mesh, one for the healthbar mesh and one for the selection plane mesh. The low number can be explained with Unity's dynamic batching.

Terrains

Unity3D did not support terrains for iOS until Unity 3.4 was released in July 2011 [19].In order to find the best solution for Mechs and Tanks, two alternative ways for terrain implementation have been tested: * Modelling terrains in a 3D program: Creating a terrain in a 3D modeling program and segmenting it into different parts (see figure 10).The partitioning has the effect that most segments are frustum culled by Unity. That meansthat only the segments that are at least partly visible get rendered.

Figure 10: Modelled terrain with highlighted parts

This technique was discarded because the process of creating new terrains gets very complicatedcompared to the Unity terrain system and the texture size is either very large or the quality suffers. * Terrain for mobile systems: Terrain for mobile systems is a solution that is available on the Unity Asset store and can convert Unity terrains to T4M terrains whichcan be used on mobile devices. It turned out that the performance of this solution was not sufficient for the game.

The final terrain solution was using the Unity terrain engine with very low quality settings. That was only possible after Unity 3.4 was released in July 2011 and added support for Unity terrains on mobile systems.

GUI-Optimization

The Unity engine offers two ways to implement the GUI. One way is to use Unity's GUI system UnityGUI that needs it's functions to be calledinside a special function called OnGUI(), that is executed twice every frame and onceevery event. This system is only used for the main menu and the pause menu, where allvalues needed are calculated outside the OnGUI function.

GUI Textures are used for all other GUI elements like GUI buttons and the minimap to sustain performance. GUI Textures are flat images that are displayed in 2D and only rendered once per frame.

Script-Optimizations

In order to grant high script performance the most expensive methods were tracked with a profiler.It turned out that the most expensive methods are called either by the pathfinding or the steering behaviour subsystem. Pathfinding has been optimized as described below in the AI section. It was also made sure that costly functions like steering behaviours and other AI routines don't get called every single frame.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity3D Engine Architecture 的相关文章

  • 在 Angular 中,如何动态地将某些单词包装在另一个 html 元素中?

    我有这个简单的角度组件 Component selector my component template p someString p export class MyComponent Input someString string som
  • HTML if 语句在 CDN 失败时加载本地 JS/CSS

    当从 CDN 或任何外部服务器加载 CSS JS 文件时 有可能 即使概率很低 由于外部故障而丢失该文件 在这种情况下 html 页面将因缺乏适当的 CSS 和 JS 而被损坏 有没有一种实用的方法可以在 CDN 故障时加载本地版本 IF
  • 更改二维数组元素的值会更改整个列

    当我打印我的arrvalue 我得到了 2D 数组的正确值 但是当我退出 while 循环时 我的值都是错误的 我不确定我做错了什么 num runs n 4 x np linspace 1 1 n y np linspace 1 1 n
  • gcc 不太可能使用宏

    我正在编写一段关键代码 其逻辑大致如下 if expression is true do something with extremely low latency before the nuke blows up This branch i
  • php字符串是值类型吗?

    为什么php的string是值类型 每次将参数传递给函数时 每次进行赋值时 每次连接都会导致字符串被复制时 它都会被复制到各处 我的 NET 经验告诉我 它似乎效率低下 迫使我几乎在任何地方都使用引用 考虑以下替代方案 替代方案1 This
  • 我有*很多*源文件要添加到 git 存储库,如何使其快速

    我在看here https git scm com docs git fast import寻找更快地将批量文件导入 git 存储库的灵感 但不确定是不是这样 基本上情况是 我有超过 1 亿个文件想要提交到 git 存储库 我已将它们分解为
  • 超慢的表格布局性能

    我遇到了糟糕的 TableLayout 性能 我在这里读过一些帖子 谈论同样的事情 Android 动态创建表 性能不佳 https stackoverflow com questions 9813427 android dynamical
  • 如何读取 GPU 负载?

    我正在编写一个程序 用于监控计算机的各种资源 例如CPU使用率等 我还想监控 GPU 使用情况 GPU 负载 而不是温度 using System using System Collections Generic using System
  • 动态 SQL 和 where case 哪个更好?

    我需要创建一个带有 12 个参数的存储过程 并使用这些参数的不同组合来过滤查询 所有 12 个参数都不是强制性的 就好像我传递 3 5 或 12 个参数取决于用户输入的搜索输入一样 我可以通过两种方式创建 即使用动态 SQL 查询或使用 C
  • 如何在 C++ 中对静态缓冲区执行字符串格式化?

    我正在处理一段对性能要求非常高的代码 我需要执行一些格式化的字符串操作 但我试图避免内存分配 甚至是内部库的内存分配 在过去 我会做类似以下的事情 假设是 C 11 constexpr int BUFFER SIZE 200 char bu
  • 是否存在比 SVN 更快的集中版本控制?

    我已经使用 SVN 很长时间了 现在我们正在尝试使用 Git 我在这里谈论的不是中心化 去中心化的争论 我唯一关心的是速度 后一个工具要快得多 但有时 我需要使用一种集中式方法 这种方法比分散式方法更简单 更简单 学习曲线非常快 这节省了大
  • 为单个方法引用大 DLL

    我想在 C 中使用大型类库 dll 中的单个方法 是否有性能或其他方面的缺点 我应该使用反射工具 读取 方法代码并将其复制粘贴到我的项目中吗 更新 硬盘空间不是问题 我的应用程序是网络应用程序 是否有性能或其他方面的缺点 唯一真正重要的是可
  • Array.indexOf 如何比 Array.some 更高效

    这个问题的灵感来自于这个问题的竞争答案 具有多个参数的indexOf https stackoverflow com questions 39000151 indexof with multiple arguments 用户想知道一种有效的
  • 为什么 pandas 在简单的数学运算上比 numpy 更快?

    最近 我观察到 pandas 的乘法速度更快 我在下面的例子中向您展示了这一点 如此简单的操作怎么可能做到这一点 这怎么可能呢 pandas 数据帧中的底层数据容器是 numpy 数组 测量 我使用形状为 10k 10k 的数组 数据框 i
  • SQL Server 不使用索引将日期时间与非空进行比较

    我有一个与其他任何表都不相关的简单表 它有一个非 PK 列 它是一个日期 我已经为该列创建了一个非聚集索引 如果我提出这个查询 select from table where datecolumn is not null 但如果我删除 no
  • 通过增加索引之和来生成排序组合的有效方法

    对于启发式算法 我需要一个接一个地评估特定集合的组合 直到达到停止标准 由于它们很多 目前我正在使用以下内存高效迭代器块生成它们 受到 python 的启发 itertools combinations http docs python o
  • 为什么 Java 11 中对于空白字符串 String.strip() 比 String.trim() 快 5 倍

    我遇到过一个有趣的场景 因为某些原因strip 针对空白字符串 仅包含空格 明显快于trim 在Java 11中 基准 public class Test public static final String TEST STRING 3 w
  • TColorProperty德尔福柏林10.1.2?

    我正在尝试将组件从 Delphi 7 转换为 Delphi Berlin 平面组件 https sourceforge net projects flatstyle https sourceforge net projects flatst
  • 为什么对于小数组,for-of 循​​环比标准 for 循环快,而对于大数组则慢?

    在 JavaScript 中 我注意到 ES6for of循环的性能与传统的有很大不同for start stop step loop 基准 const n 10000 const arr Array n fill map e i gt i
  • 大数组上的 SSE 性能较慢

    我是 SSE 编程新手 所以我希望有人可以帮助我 我最近使用 GCC SSE 内在函数实现了一个函数来计算 32 位整数数组的总和 下面给出了我的实现代码 int ssum const int d unsigned int len stat

随机推荐

  • Hive练习题

    文章目录 Hive练习题 题目一 题目二 题目三 Hive练习题 题目一 学生表 STUDENT 的字段含义 SNO 代表学号 SNAME 代表学生姓名 SAGE 代表学生年龄 SSEX 代表学生性别 课程表 COURSE 的字段含义 CN
  • 4Sum

    Given an array S of n integers are there elements a b c and d in S such that a b c d target Find all unique quadruplets
  • 【Python爬虫开发实战①】使用urllib以及XPath爬取可爱小猫图片

    个人主页 为梦而生 关注我一起学习吧 专栏 python网络爬虫从基础到实战 欢迎订阅 后面的内容会越来越有意思 往期推荐 Python爬虫开发基础 urllib库的基本使用 Python爬虫开发基础 XPath库及其基本用法 我们在之前已
  • 策略模式-

    定义 定义一系列的算法 把它们一个个封装起来 目的就是将算法的使用与算法的实现分离开来 从而算法的变化不会影响到使用算法的用户 适用场景 1 假如系统中有很多类 而他们的区别仅仅在于他们的行为不同 2 一个系统需要动态地在几种算法中选择一种
  • python pipline_python中sklearn的pipeline模块实例详解

    最近在看 深度学习 基于Keras的Python实践 魏贞原 这本书 书中8 3创建了一个Scikit Learn的Pipeline 首先标准化数据集 然后创建和评估基线神经网络模型 代码如下 数据正态化 改进算法 steps steps
  • web前端技术笔记(十六)bootstrap、表单正则和前端优化

    bootstrap bootstrap bootstrap 容器 bootstrap 栅格系统 栅格响应式布局案例 列偏移 bootstrap 隐藏类 bootstrap 按钮 bootstrap 表单 bootstrap 导航条 导航条案
  • 如何在Redis中实现事务

    事务介绍 事务 Transaction 是指作为单个逻辑工作单元执行的一系列操作 事务必须满足ACID原则 原子性 一致性 隔离性和持久性 简单来说 事务可能包括1 N条命令 当这些命令被作为事务处理时 将会顺序执行这些命令直到完成 并返回
  • 1.1【Mask-RCNN训练自己的数据集】---- Part One:制作数据集(全部流程总结+部分释义)

    写在前面 该实现基于Tensorflow Keras框架 在Ubuntu16 04下运行 官方github MaskRCNN demo 一 制作数据集 1 1 下载安装Labelme Labelme用来制作图像分割数据集 在安装Labelm
  • Python安装scikit-learn后import sklearn失败/不存在该模块

    作者在学习机器学习时 需要安装堪称ML入门必备的sklearn sklearn由3个部分组成 numpy scipy scikit learn 环境 python 3 6 8 基于Anaconda 编译命令 conda install nu
  • java 字符串拼接

    最常用的有4种方法 运算符 如果拼接的都是字符串直接量 则适合使用 运算符实现拼接 public static void main String args method1 字符串拼接 public static void method1 S
  • Android MediaPlayer+Stagefright框架(音频)图解

    在android原生的媒体播放器中是由mediaplayerservice来控制媒体播放器的 在mediaplayerservice中创建了mediaplayer 在mediaplayer java的native方法通过jni调用andro
  • java使用jdbc连接hive2出现Required field 'client_protocol' is unset! Struct:TOpenSessionReq

    java sql SQLException Could not establish connection to jdbc hive2 Required field client protocol is unset Struct TOpenS
  • 欢迎进入嵌入式羊圈博客导航一站式搜索(所有博客的汇总帖)

    目录 一 Linux 服务器相关 二 折腾系列 三 日常学习笔记 四 硬件工程师之路 五 T5L迪文屏的开发与应用 六 ZigBee的开发与应用 七 C语言高阶学习笔记 八 嵌入式通信 九 单线通讯 十 单片机 嵌入式 十一 STM8S学习
  • vue常见面试题:computed和watch的区别是什么?

    根据vue官方文档可以看到 computed叫做计算属性 而watch叫做侦听器 顾名思义 计算属性 是依赖其他变量计算出来得到的一个变量 也就是它受别的变量的影响 别的变量一变他就会变 比如 计算属性是基于它们的响应式依
  • 微信公众号服务器配置验证服务器地址token php代码

    做记录
  • 三个小朋友分糖果

    题目描述 有甲 乙 丙三个小朋友 甲有x粒糖果 乙有y粒糖果 丙有z粒糖果 现在他们做一个游戏 从甲开始 将自己的糖平均分三份 自己留一份 其余两份分别给乙与丙 多余的糖果自己吃掉 然后乙与丙也依次这样做 问最后甲 乙 丙三人各有多少粒糖果
  • jquery attr()方法 添加,修改,获取对象的属性值

    转自 http hi baidu com 0701u item b8ec62c1daba973eef466524 jquery中用attr 方法来获取和设置元素属性 attr是attribute 属性 的缩写 在jQuery DOM操作中会
  • 区块链程序要怎么去测试?

    区块链程序开发越来越多 区块链怎么做测试 让大家很好奇 与传统测试不同的是区块链系统边界模糊 不管是是独立的应用程序 还是客户端 服务器模式的应用程序 传统软件都具备明显的系统边界 可以通过UI用户界面或者客户端去进行测试 区块链底层则不同
  • 空间解析几何

    解析几何是用代数方法研究几何对象之间的关系和性质的一门几何学分支 通俗讲就是通过建立坐标系来用方程描述几何图形 在解析几何创立以前 几何与代数是彼此独立的两个分支 而它的出现使形与数统一起来 这是数学发展史上的一次重大突破 在平面解析几何中
  • Unity3D Engine Architecture

    原文 http www umingo de doku php id paper mechs and tanks section03 Architecture To better understand the game s software