Entities介绍

Entities是Unity2018新引入的一个全新游戏设计框架,我们称之为实体组件系统(Entity Component System,简称ECS),它的出现使我们能够集中精力解决实际问题:构成游戏的数据和行为。它利用了C# Job System和Burst,使我们能够充分利用当今的多核处理器。通过从面向对象的设计转向面向数据的设计,我们将更容易重用代码,也更容易让其他人理解和使用ECS。

在Unity2018引入Entities之前,其实ECS框架早已经出现,但是ECS还是因为守望先锋采用了这个框架才被我们所熟知,后来Git上面出现了一个Entitas的插件可以直接用在Unity上面,我在以前的公司参与过一个项目就是用的Entitas插件。

实体Entity:一个ID,它可以用于间接的组件查找,用于在容器里面遍历。

组件数据ComponentData:存储在实体中的数据。

组件系统ComponentSystem:处理游戏,系统逻辑和行为的地方。

组Group:组件系统运行所需的ComponentData列表,相对于判断条件。

Entities和传统的ECS名称对比
Entities名称 传统ECS名称
Entity Entity
ComponentData Component
ComponentSystem System
Entities和MonoBehaviour功能对比
Entities MonoBehaviour
Entity GameObject
ComponentData Component的字段变量
ComponentSystem Component的Update方法

Entities的安装

  1. 点击菜单Window > Packages Manager > All,选择Entities,然后点击Install按钮安装。
  2. 打开PlayerSettings>Other Settings>Scripting Runtime Version,我们选择.Net 4.x Equivalent

注意:安装Entities后会报错是因为Unity默认的Scripting Runtime Version是.Net 3.5 Equivalent,我们改为.Net 4.x Equivalent即可。

Entities的使用

我们用一个简单的例子来讲解一下Entities的使用方法。

我们先用传统的写法写一个Cube的自转。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RotationCube : MonoBehaviour {public float speed;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {transform.Rotate(Vector3.up * speed * Time.deltaTime);}
}

接下来我们混合加入ECS,把上面的代码拆分成Component和System。我们先写一个Component,只有一个字段变量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RotationCubeComponent :MonoBehaviour
{public float speed;
}

然后我们再写一个System,作为逻辑的处理。首先我们要继承ComponentSystem,然后重写OnUpdate,这里相当于MonoBehaviour的Update,我们在这里处理逻辑。Group则是组件系统运行所需的ComponentData列表,可以随意命名,我们可以在这里写很多不同的Group,作为处理逻辑的条件。GetEntities<Group>()表示获取指定的实体,所有符合条件的实体都将被获取到(这里符合条件的是所有带有RotationCubeComponent组件的实体都将被获取到)。

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;public class RotationCubeSystem : ComponentSystem
{struct Group{public RotationCubeComponent  rotation;public Transform transform;}protected override void OnUpdate(){foreach (var enitites in GetEntities<Group>()){enitites.transform.Rotate(Vector3.up * enitites.rotation.speed * Time.deltaTime);}}
}

我们也可以用另外一种方式来写System。Inject表示注入数据,在Unity启动时会自动赋值合适的值

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;public class RotationCubeSystem : ComponentSystem
{struct Group{public readonly int Length;public ComponentArray<RotationCubeComponent> rotationCubeComponents;//public RotationCubeComponent  rotation;//public Transform transform;}[Inject] Group group;protected override void OnUpdate(){//foreach (var enitites in GetEntities<Group>())//{//    enitites.transform.Rotate(Vector3.up * enitites.rotation.speed * Time.deltaTime);//}for (int i = 0; i < group.rotationCubeComponents.Length; i++){group.rotationCubeComponents[i].transform.Rotate(Vector3.up * group.rotationCubeComponents[i].speed * Time.deltaTime);}}
}

最后,我们创建一个Cube,附加上GameObjectEntity给他一个实体ID标识,附加上RotationCubeComponent组件,我们把旋转速度调为100,再创建几个Cube并且附加上GameObjectEntity,但不附加RotationCubeComponent组件,运行Unity后发现,带有RotationCubeComponent组件的实体会自转,没有带RotationCubeComponent组件的实体不会自传。

我们讲的这个案例主要是让大家了解ECS这个框架的基本思想和传统的开发思路的区别,即数据和逻辑分离。

我将在下一篇文章中继续深入讲解ECS的框架。

Unity2018新功能之Entity Component System(ECS)二

Unity2018新功能之Entity Component System(ECS)一相关推荐

  1. unity2018新功能之——2D Animation System

    今天收到unity的邮件,然后便浏览了下2018的新特性. https://blogs.unity3d.com/cn/2018/05/02/2018-1-is-now-available/?utm_c ...

  2. Unity下一轮最大的变革-Entity Component System C# Jobs System

    ECS+jobs实现的酷炫效果 新一代Entity Component System(ECS)将会彻底改变Unity的底层概念(GameObject-Component 系统)和现有工作方式.Mono ...

  3. Entity Component System

    2019独角兽企业重金招聘Python工程师标准>>> http://t-machine.org/index.php/2007/09/03/entity-systems-are-th ...

  4. Unity2018新功能抢鲜 | 粒子系统改进

    本文首发于"洪流学堂"微信公众号. 洪流学堂,让你学Unity快人几步 Unity2018.1中对粒子系统进行了重大改进,包括功能.性能很多方面,快来看看吧! GPU网格实例化 粒 ...

  5. Unity2018新功能抢先预览 | Preset功能

    本文首发于"洪流学堂"微信公众号. 洪流学堂,让你学Unity快人几步 Presets(预设) Preset是Unity2018的新功能. Preset是保存对象属性的资源.Pre ...

  6. Unity2018新功能抢鲜 | ShaderGraph入门教程

    本文首发于"洪流学堂"微信公众号. 洪流学堂,让你学Unity快人几步 洪流学堂公众号回复节点,获取ShaderGraph节点详解PDF文件(带目录). Shader一直是Unit ...

  7. Unity2018新功能之2D Animation2D动画

    新版本中引入不少2D的新功能,我们打开资源包管理器,可以看到2D Animation,2D IK,2D Pixel Perfect,2D SpriteShape等等. 我们先来讲讲2D Animati ...

  8. Unity2018新功能抢鲜 | C# Job System Ⅱ

    本文首发于"洪流学堂"微信公众号. 洪流学堂,让你学Unity快人几步 上一篇文章我们讲了job system,这篇文章来看看如何使用job system以及常见的问题. Sche ...

  9. Unity2018新功能抢鲜 | ShaderGraph实战教程之溶解效果

    本文首发于洪流学堂微信公众号. 洪流学堂,让你快人几步!你好,我是你的技术探路者郑洪智,你可以叫我大智(vx: zhz11235). 洪流学堂公众号回复节点,获取ShaderGraph节点详解PDF文 ...

最新文章

  1. CNN模型复杂度(FLOPs、MAC)、参数量与运行速度
  2. PHP:第一章——PHP中的关键字
  3. windows如何调用Linux的API,Windows和Native API中的系统调用?
  4. ef sqlserver切换到mysql_可以为MySql和SqlServer使用EF上下文吗?
  5. 中立安全、赋能产业,UCloud优刻得凭差异化路线进军产业互联
  6. ssm项目中使用拦截器加上不生效解决方案
  7. 获取xcode下载地址的页面
  8. 实习日志_护理实习日志
  9. Android系统终端命令大全
  10. pyaudio:基于pyaudio利用Python编程从电脑端录制音频保存到指定文件夹+将录音读取出来
  11. 思维简史:从丛林到宇宙
  12. 甘超波:什么是个人定位
  13. python+openCV 自适应阈值分割
  14. java新版本新特性
  15. Android教程之名词扫盲汇总
  16. 对JSON中的key进行驼峰和下划线格式的相互转换
  17. android输入法架构解析
  18. pmp直方图与帕累托图的区别_PMP-项目质量管理
  19. PTA 7-51 生化危机 (25分) dfs
  20. [carla入门教程]-6 小项目:基于carla-ros-bridge构建一个小型比赛赛道

热门文章

  1. 哪些机型适配了android11,miui11支持哪些机型_miui11适配机型大全_飞翔教程
  2. adobexd怎么录屏_Adobe XD 入门教程-如何在 Adobe XD 中创建交互式原型?
  3. 英语论文词汇句式收集
  4. 思科推出最新认证考纲 将首先应用于CCIE认证
  5. 【智能物流】分分钟了解透彻自动化立体仓库
  6. 江苏大学计算机专业分数,江苏大学2020年所有招生专业录取分数大排行
  7. 城市售票网添加成功卡进网页声音提示音模块
  8. 数据分析项目2-人口分析
  9. 登录页面报警告: This page includes a password or credit card input in a non-secure context.
  10. [C#|Unity3D学习笔记]简易五子棋源码