DOTS(一)
一 概述DOTS(Data-Oriented Technology Stack):数据导向型技术堆栈;
传统方式遇到的问题:
1.数据冗余;
2.单线程处理;
3.编译器问题;
针对传统问题,DOTS技术带来的三种解决办法:
1.ECS(Entity Component System):数据和行为分离;
2.Job System:多线程,充分发挥多核CPU的特性;
3.Burst Complier:编译生成高效的代码;
二 环境搭建
1.安装Entities
打开Unity2019,点击Window→点击Package Manager→点击Advanced→选中Show preview packages→找到列表里的"Entities"→点击右下角的“Install”进行安装;
2.安装Hybrid Renderer
点击Window→点击Package Manager→点击Advanced→选中Show preview packages→找到列表里的"Hybrid Renderer"→点击右下角的“Install”进行安装;
三 DOT结构汇总
image-20210330170156702.png
1 ECS结构
组件
1.自定义 IComponentData
2.系统组件 Unity.Transforms
3.添加组件 EntityManager.AddcomponentData()
4.设置组件值 EntityManager.SetcomponentData()
系统
继承 ComponentSystem 实现 Entities.ForEach()
实例
gameObject 转 Entity (两种方法)
1.继承 IConvertGameObjectToEntity
2.第二种方法
GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld( World.DefaultGameObjectInjectionWorld, null);Entity entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, settings);
创建预制体 EntityManager.Instantiate()
特性
DisableAutoCreation 用于关闭system
2 JobSystem
继承 JobComponentSystem 实现 Entities.ForEach().Schedule()
3 Burst
继承 JobComponentSystem 实现 Entities.ForEach().WithBurst()
4 批处理
开启Gpu Instancing
image-20210330171706168.png
四 ECS案例
例如:打印一个数字:
1.创建组件脚本PrintComponentData.cs:
using Unity.Entities;public struct PrintComponentData : IComponentData{ public float printData;}
2.创建系统脚本PrintSystem.cs:
using UnityEngine;using Unity.Entities; public class PrintSystem : ComponentSystem{ protected override void OnUpdate() { Entities.ForEach((ref PrintComponentData printComponentData)=> { Debug.Log(printComponentData.printData); }); }}
3.创建实体需要挂载的脚本PrintAuthoring.cs:
using UnityEngine;using Unity.Entities; public class PrintAuthoring : MonoBehaviour, IConvertGameObjectToEntity{ public float printData; public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { dstManager.AddComponentData(entity, new PrintComponentData() { printData=printData}); }}
4.给实体挂载脚本ConvertToEntity.cs和PrintAuthoring.cs:
20210116151716353.png
四 调试面板介绍
显示出场景中的所有实体:
点击Window→点击Analysis→选中Entity Debugger→点击运行按钮即可看到场景中所有的实体及相关的组件;
20210116155805145.png
五 默认组件的使用
创建脚本TranslationSystem.cs,如下:
using Unity.Entities;using Unity.Transforms;using Unity.Mathematics; public class TranslationSystem : ComponentSystem{ protected override void OnUpdate() { Entities.ForEach((ref Translation translationComponentData) => { translationComponentData.Value = new float3(1, 1, 1); }); }}
运行后如下:
20210116161259977.png
六 Transform命名空间下组件的使用
1.创建脚本RotationEulerXYZAuthoring.cs,并挂载到场景:
using UnityEngine;using Unity.Entities;using Unity.Transforms; public class RotationEulerXYZAuthoring : MonoBehaviour, IConvertGameObjectToEntity{ public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { dstManager.AddComponentData(entity, new RotationEulerXYZ()); }}
2.创建脚本RotationEulerXYZSystem.cs,如下:
using Unity.Entities;using Unity.Mathematics;using Unity.Transforms; public class RotationEulerXYZSystem : ComponentSystem{ protected override void OnUpdate() { Entities.ForEach((ref RotationEulerXYZ rotationEulerXYZComponentData) => { rotationEulerXYZComponentData.Value = new float3(45, 45, 45); }); }}
3.运行后如下
20210116164914734.png
七 屏蔽系统之间的互相干扰
通过上面的例子,我们会发现,在六里面,Cube旋转的同时也发生了位移。说明虽然是不同的场景,但是脚本依然会对其他场景的物体造成影响:
20210128112532560.png
在这里我们只需要加个,就可以了:
using Unity.Entities;using Unity.Transforms;using Unity.Mathematics; public class TranslationSystem : ComponentSystem{ protected override void OnUpdate() { Entities.ForEach((ref Translation translationComponentData) => { translationComponentData.Value = new float3(1, 1, 1); }); }}八 实例预制体
1.创建预制体cube
image-20210330172206063.png
2.创建脚本 ECSPrefabCreator.cs 并挂载到场景
using Unity.Entities;using Unity.Transforms;using UnityEngine;public class ECSPrefabCreator : MonoBehaviour{ public GameObject cube; public float interval, sum; void Start() { GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld( World.DefaultGameObjectInjectionWorld, null); Entity entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, settings); EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; Translation tran = new Translation(); for (int i = 0; i < sum; i++) { for (int j = 0; j < sum; j++) { Entity entityCube = entityManager.Instantiate(entityPrefab); entityManager.SetComponentData(entityCube,tran); tran.Value.x += interval; } tran.Value.x = -interval; tran.Value.y += interval; } }}九 使用JobSystem+Brust+批处理 优化
创建 JobSystem.cs脚本
public class JobSystem : JobComponentSystem{ protected override JobHandle OnUpdate(JobHandle inputDeps) { JobHandle jobHandle = Entities.ForEach((ref RotationEulerXYZ p) => { p.Value = new float3(0, 45, 0); }).WithBurst().Schedule(inputDeps); return jobHandle; }}
页:
[1]