当前位置:   article > 正文

用Unity DOTS制作4万飞剑的太极剑阵!_unity dots教程

unity dots教程

【USparkle专栏】如果你深怀绝技,爱“搞点研究”,乐于分享也博采众长,我们期待你的加入,让智慧的火花碰撞交织,让知识的传递生生不息!

一、演示效果

由于DOTS相关Package不同版本变动很大,许多老的教程也已经过时,给想要探索的小伙伴制造了不少麻烦。所以我便尝试用DOTS制作了这样一个由42804把飞剑组成的炫酷剑阵,每次点击地板,都会有10000把飞剑飞出大阵攻击目标点后返回。算是致敬了古龙小说中的“剑气纵横三万里 ,一剑光寒十九洲”的光景。本文将制作过程和学习经历分享出来,希望能给同样探索DOTS的小伙伴一些参考。


文章较长,对准备工作没兴趣的小伙伴直接跳到目录6。

二、创建工程

首先我们下载好Unity 2020.3.3f1,选择Universal Render Pipeline创建工程。

DOTS的相关Package并未发布,也无法在Package Manager中搜索到,我们需要手动去下载这几个包。

三、下载DOTS相关包

打开上方菜单栏,点击在左上角+号图标选择Add package from git URL,依次输入:

  • com.unity.entities
  • com.unity.rendering.hybrid
  • com.unity.physics

等待一会儿包就下载好了,但我们的准备工作还没做完。

四、动态合批设置

由于我们将要生成大量相同材质的飞剑,所以将它们合批处理降低DrawCall是有必要的。

首先点击Edit >ProjectSetting>Quality>查看当前使用的Rendering设置文件,关闭SRP Batcher:

再新建一个材质,勾选Enable GPU Instancing:

你就会发现Unity将拥有同一此材质的物体合批渲染了:

值得一提的是,本工程中的飞剑已经被我用Blender手动将顶点数降低到105个了,因为我最后大致要生成4万把飞剑,原本的飞剑模型有上千个顶点,庞大的定点数会导致我的场景近乎卡死,最后测试我的电脑能顶住的最大顶点数大概是10M左右。

五、制作飞剑发光效果

我希望每把飞剑都有一个发出淡淡光晕的效果,在2020.3.3版本的URP工程中,Unity已将Post Processing Package默认置入,所以我们无需像老版本一样去自己下载Post-Processing package包。直接使用就可以了。

首先新建一个材质球,勾选了Emission后扔给飞剑,并在场景中右键新建一个Global Volume:

同时点击主相机,勾选Solid Color,选择一个偏黑色的颜色,再将Post Processing勾选上:

我们的场景中不需要天空盒,所以点击Window>Rendering>Lighting>Environment,将天空盒材质设置为None:

最后我希望后处理效果只对指定层级的物体起作用,这里需要Global Volume和物体设置为相同层级,并且在主相机中指定该层级:

最后新建一个配置文件,点击Add Override,并添加Bloom效果即可。


准备工作做完了,终于要进入实践环节了,本文就不赘述DOTS相关的概念了,网上关于DOTS介绍的文章很多,官方文档对于DOTS阐述也算是详尽,这里我放个链接,对DOTS不了解的小伙伴可以先理解了这些概念后再来实践:

官方文档:
Entity Component System | Entities | 0.17.0-preview.42

六、生成太极图案的飞剑群实体

首先找到一张太极八卦阵的图案,然后掐太极法印!高呼口诀:

  • 天地无极,乾坤借法!
  • 太乙天尊,急急如律令!

然后就发现图片白色部分都变成透明的了:

接下来我们需要获取图片中每个像素点的位置,然后等比转换到Unity空间中去,让飞剑去填充这些位置。所以图片也不能太大,这里我们导入的时候在Unity中设置就好了:

代码如下:

  1. public SpriteRenderer spriteRenderer;
  2. //像素点相对位置
  3. public List<int2> posList;
  4. [Header("Drawing")]
  5. //剑阵密度
  6. public int drawDensity ;
  7. //剑阵离散程度
  8. public int disperseMin;
  9. public static GetPixel Instance;
  10. //图片宽高
  11. private int width;
  12. private int height;
  13. void Start()
  14. {
  15. Instance = this;
  16. width = spriteRenderer.sprite.texture.width;
  17. height = spriteRenderer.sprite.texture.height;
  18. Debug.Log("图片宽度" + width + "图片高度" + height);
  19. GetPixelPos();
  20. }
  21. public void GetPixelPos()
  22. {
  23. int halfHeight= height / 2;
  24. int halfWidth = width / 2;
  25. int2 tempPos;
  26. for (int i = 0; i < height; i += drawDensity)
  27. {
  28. for (int j = 0; j < width; j += drawDensity)
  29. {
  30. //获取每个位置像素点的颜色
  31. Color32 c = spriteRenderer.sprite.texture.GetPixel(j, i);
  32. tempPos.y = (j-halfHeight)*disperseMin;
  33. // Debug.Log("RGBA:" + c);
  34. //如果对应位置颜色不为透明,则记录坐标到List中
  35. if (c.a != 0)
  36. {
  37. tempPos.x = (i-halfWidth)* disperseMin;
  38. posList.Add(tempPos);
  39. }
  40. }
  41. }
  42. }

位置拿到了,接下来需要根据位置生成大量飞剑,并将飞剑转换为Entity,我们可以选择给飞剑的预制体添加Convert To Entity脚本的方法进行转换:

但点开Convert To Entity查看,发现它也是继承了MonoBehaviour,很明显它不会自己在编辑器中转换好,所以我们需要在运行的过程中将飞剑的预制体转换为Entity,代码如下:

  1. using Unity.Entities;
  2. public class Test : MonoBehaviour
  3. {
  4. //飞剑预制体
  5. public GameObject swordPrefab;
  6. //在 World 中, EntityManager 管理所有实体和组件
  7. private EntityManager _manager;
  8. //blobAssetStore是一个提供缓存的类,缓存能让你对象创建时更快。
  9. private BlobAssetStore _blobAssetStore;
  10. private GameObjectConversionSettings _settings;
  11. //转换后的飞剑实体
  12. private Entity swordEntity;
  13. void Start()
  14. {
  15. //获取该世界的EntityManager
  16. _manager = World.DefaultGameObjectInjectionWorld.EntityManager;
  17. _blobAssetStore = new BlobAssetStore();
  18. _settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, _blobAssetStore);
  19. //将飞剑预制体转换为Entity
  20. swordEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(swordPrefab, _settings);
  21. }
  22. private void OnDestroy()
  23. {
  24. _blobAssetStore.Dispose();
  25. }
  26. 再按位置生成飞剑Entity,顺便打印出来飞剑的数量:
  27. private void Update()
  28. {
  29. if (Input.GetKeyDown(KeyCode.W))
  30. {
  31. BurstGenerateSword();
  32. }
  33. }
  34. //大量生成飞剑!
  35. public void BurstGenerateSword()
  36. Debug.Log("生成数量:" + GetPixel.Instance.posList.Count);
  37. //遍历位置列表,生成对应数量的飞剑Entity
  38. for (int i = 0; i < GetPixel.Instance.posList.Count; i++)
  39. {
  40. SpawnNewSword(GetPixel.Instance.posList[i]);
  41. }
  42. }
  43. public void SpawnNewSword(float2 pos)
  44. {
  45. Entity newSword = _manager.Instantiate(swordEntity);
  46. //创建一个translation Component实例,并赋值对应坐标
  47. Translation ballTrans = new Translation
  48. {
  49. Value = new float3(pos.x, 0f, pos.y)
  50. };
  51. _manager.AddComponentData(newSword, ballTrans);
  52. }

再按位置生成飞剑Entity,顺便打印出飞剑的数量:

  1. private void Update()
  2. {
  3. if (Input.GetKeyDown(KeyCode.W))
  4. {
  5. BurstGenerateSword();
  6. }
  7. }
  8. //大量生成飞剑!
  9. public void BurstGenerateSword()
  10. Debug.Log("生成数量:" + GetPixel.Instance.posList.Count);
  11. //遍历位置列表,生成对应数量的飞剑Entity
  12. for (int i = 0; i < GetPixel.Instance.posList.Count; i++)
  13. {
  14. SpawnNewSword(GetPixel.Instance.posList[i]);
  15. }
  16. }
  17. public void SpawnNewSword(float2 pos)
  18. {
  19. Entity newSword = _manager.Instantiate(swordEntity);
  20. //创建一个translation Component实例,并赋值对应坐标
  21. Translation ballTrans = new Translation
  22. {
  23. Value = new float3(pos.x, 0f, pos.y)
  24. };
  25. _manager.AddComponentData(newSword, ballTrans);
  26. }

然后运行场景按W,在默认Burst Compliler和Job Threads开启的情况下,可以看到飞剑迅速生成到了场景中央,并且没有一点卡顿:

七、调试Entity

每个Entity是无法显示在Hierarchy视窗中进行调试的,Unity提供了一个工具用来分析,依次点击:Window>Analysis>Entity Debugger,就可以在Entity Debugger中查看每个Entity具体包含的Component,以及对应属性值的变化:

图中我们可以看到,列表长度为42802,但Entity的数量生成了2倍多,这是因为飞剑的预制体中包含一个子物体,转换过程中EntityManager会将物体的子物体一同转换为Entity:

你也可以点击Filter,通过筛选Component的方式来寻找想要查看的Entity,左边的系统列表显示了项目中的系统以及系统运行每个框架所花费的时间。你可以使用为每个系统提供的复选框从列表中打开和关闭系统,来进行调试。

八、剑阵围绕中心旋转

飞剑已经生成了,接下来就需要创建一个System来更新这些Entity,让它们旋转起来。
首先定义一个Component添加到预制体上,这个Component的作用是一个标签:

  1. using Unity.Entities;
  2. [GenerateAuthoringComponent]
  3. public struct SwordTag : IComponentData{}

然后新建一个System,继承SystemBase基类,需要注意的是:JobComponentSystem和ComponentSystem这两个基类将会被摒弃,(Entities 0.17.0 package文档中官方明确已经明确表示),SystemBase将会是System唯一的基类,所以建议尽量使用SystemBase。

接着实现System接口,通过Entitie.ForEach方法筛选出Sword Entity,并更新位置:

  1. using Unity.Entities;
  2. using Unity.Jobs;
  3. using Unity.Transforms;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. public class SwordRotateSystem : SystemBase
  7. {
  8. protected override void OnUpdate()
  9. {
  10. float deltaTime = Time.DeltaTime;
  11. float angel = 0.01f;
  12. //获取所有包含Swordtag的Entity,并对他们的translation和rotation组件值进行更改
  13. Entities.
  14. WithAll<SwordTag>().
  15. ForEach((ref Translation translation, ref Rotation orientation) =>
  16. {
  17. var rotation = orientation;
  18. //目标点为(0,0,0
  19. float3 targetPosition = float3.zero;
  20. var targetDir = targetPosition - translation.Value;
  21. //飞剑垂直向下面向中心点
  22. quaternion temp1 = Quaternion.FromToRotation(Vector3.left, targetDir);
  23. orientation.Value = math.slerp(orientation.Value, temp1, deltaTime);
  24. #region 飞剑围绕中心点(0,0,0)旋转
  25. float3 pos = translation.Value;
  26. //旋转轴和旋转角度
  27. quaternion rot = quaternion.AxisAngle(math.up(), angel);
  28. float3 dir = pos - float3.zero;
  29. dir = math.mul(rot, dir);
  30. translation.Value = float3.zero + dir;
  31. #endregion
  32. }).ScheduleParallel();
  33. }
  34. }

Unity ECS会自动在系统中发现继承了SystemBase的类,点击运行场景,飞剑此时已经可以沿中心点旋转了:

九、飞剑出阵和归阵

接下来是整个剑阵最核心的功能,在实现的过程中我也实践了两种思路。

第一种思路是:
点击下方广场,获取射线与广场碰撞点的位置,创建Tag2组件如下:

  1. using Unity.Entities;
  2. using Unity.Mathematics;
  3. public struct Tag2 : IComponentData
  4. {
  5. //飞剑出阵后,要攻击的目标点坐标
  6. public float3 target;
  7. //替代出阵飞剑进行旋转的enitiy
  8. public Entity TempEntity;
  9. }
  1. 创建System1,在System1中遍历不带Tag2的飞剑,抽取10000把飞剑,添加Tag2,同时创建10000个TempEntity替代其继续旋转。Tag2存取位置和对应Tempentity信息。
  2. 创建System2 ,在System2中遍历所有带Tag2的飞剑,令其出阵飞向目标点。
  3. 在System2中遍历所有带Tag2的飞剑,当System2中的飞剑非常接近目标点,则移除Tag2,添加Tag3。
  4. 创建System3,在System3中遍历所有带Tag3的飞剑归阵,追赶上自己对应的Tempentity,之后销毁掉对应的Tempentity。

这个方案有这样2个问题:
1. System之间执行顺序问题
所有继承SystemBase,实现的OnUpdate()方法的System都是在主线程上跑的,因此我们可以很方便地规划System1,2,3的执行顺序,代码如下:

  1. [UpdateAfter(typeof(System1))]
  2. public class System2 : SystemBase
  3. {
  4. protected override void OnUpdate(){}
  5. }
  6. [UpdateAfter(typeof(System2))]
  7. public class System3 : SystemBase
  8. {
  9. protected override void OnUpdate(){}
  10. }

2. EntityCommandBuffer时序问题
在ECS中,所有的Entity都是按块(Chunk)存储的,一个块里的所有实体必定拥有相同数量和类型的组件,一旦某个实体的组件数量或类型改变了,它就不属于当前的块,它会被移到其它块里,这个操作在主线程中运行并没有什么问题。

但我希望能发挥CPU全部的性能来运行这个剑阵,所以需要去分配一些任务到子线程中执行,JobSystem帮我们解决了这个问题,它屏蔽了直接对线程的操作,而是把异步逻辑封装成一个个“Job”,由引擎来调度和分配给合适的线程去执行,官方实例代码在这:

Unity - Scripting API: IJob

除了上面的方法使用JobSystem,ECS提供SystemBase类中常用的Entities.ForEach方法也可以将数据转换到子线程处理,我们可以使用Schedule()或者ScheduleParallel()来执行Job的Lambda方法:

  1. partial class ApplyVelocitySystem : SystemBase
  2. {
  3. protected override void OnUpdate()
  4. {
  5. Entities
  6. .ForEach((ref Translation translation,
  7. in Velocity velocity) =>
  8. {
  9. translation.Value += velocity.Value;
  10. }).Schedule();
  11. //Run()是立即在主线程上执行
  12. //目前并没有找到官方的Schedule()和ScheduleParallel()之间的区别
  13. //目前看到的一个解释是Schedule()强制每个Job单独一个线程
  14. //而ScheduleParallel()则是在并行线程上处理Job,每个线程处理query中一个或多个chunk
  15. }
  16. }

这时候,假设Job1中删除了某个实体的组件,该实体被移到其它Chunk,而它并行的Job2也在对这个实体进行操作,就会产生冲突(操作不存在的组件或者操作了错误块里的实体),所以Job2必须等待Job1读写数据完毕,这就是硬性同步点(Sync points)。

以下导致结构变化的操作,都会产生硬性的同步点:

  • 创建实体
  • 删除实体
  • 向实体添加组件
  • 从实体中删除组件
  • 更改共享组件中的值

每个同步点都会造成停顿,等待当前World中所有先前安排的作业完成。过多的同步点会让效率大大降低,但以上的操作还是无法避免的。

为了解决这种问题,Unity提供了EntityCommandBuffer(实体命令缓冲区,简称ECB)来解决这个问题。

ECB可以将导致结构性更改的命令排入队列,存储在ECB中的命令可以在一帧中晚些时候回放执行。当回放ECB时,这会将跨帧分布的多个同步点减少到单个同步点。下面这个案例是使用系统自带的ECB,这样可以最大程度的减少同步点:

  1. struct Lifetime : IComponentData
  2. {
  3. public byte Value;
  4. }
  5. class LifetimeSystem : SystemBase
  6. {
  7. EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
  8. protected override void OnCreate()
  9. {
  10. base.OnCreate();
  11. // 从World中获取ECS系统并且存起来
  12. m_EndSimulationEcbSystem = World
  13. .GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
  14. }
  15. protected override void OnUpdate()
  16. {
  17. // 请求一个ECS并且转换成可并行的
  18. var ecb = m_EndSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
  19. Entities
  20. .ForEach((Entity entity, int entityInQueryIndex, ref Lifetime lifetime) =>
  21. {
  22. // 检测entity的lifetime,如果为0则销毁它
  23. if (lifetime.Value == 0)
  24. {
  25. // 将entityInQueryIndex传给操作,这样ECS回放时能保证正确的顺序
  26. ecb.DestroyEntity(entityInQueryIndex, entity);
  27. }
  28. else
  29. {
  30. lifetime.Value -= 1;
  31. }
  32. }).ScheduleParallel();
  33. // 保证ECB system依赖当前这个Job
  34. m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
  35. }
  36. }

这样,飞剑添加Component,创建临时的Entity的操作好像都没啥问题了,但实际运行起来时会发现飞剑并没有飞回原定的位置,明显比原先剑阵慢了一帧:

前面也说到了,EntityCommandBuffer中的命令不会立即执行,而是会在下一帧被EntityCommandBufferSystem使用,有一帧延迟。我们生成替代出阵飞剑的Entity,在下一帧才会真正的生成并跟随剑阵旋转,所以飞剑最后和Entity同步的位置也自然是慢了一帧。目前的解决办法是多计算一帧的距离,但这样做既麻烦又有误差,所以我们换一种简单的思路。

对与EntityCommandBuffer时序问题有疑惑的小伙伴可以看这篇文章:
Unity DOTS编码实践:EntityCommandBuffer

第二种思路是:
生成TempEntity和飞剑在八卦阵中,为飞剑添加Tag1组件,记录相同位置的TempEntity。

  1. using Unity.Entities;
  2. using Unity.Mathematics;
  3. public struct Tag1 : IComponentData
  4. {
  5. //对应的TempEntity
  6. public Entity TempEntity;
  7. }
  1. 创建System1,遍历所有TempEntity,使它们沿中心旋转。
  2. 创建System2,遍历所有无Tag2组件的飞剑,并且每帧和对应TempEntity位置同步。
  3. (System2中执行)若发生点击事件,则抽取10000把飞剑添加Tag2,Tag2记录目标点位置。
  1. using Unity.Entities;
  2. using Unity.Mathematics;
  3. public struct Tag2 : IComponentData
  4. {
  5. //目标点位置
  6. public float3 targetpos;
  7. }

主要代码如下:
System1:

  1. using Unity.Entities;
  2. using Unity.Jobs;
  3. using Unity.Transforms;
  4. using Unity.Mathematics;
  5. public struct TempEntityTag : IComponentData{}
  6. public class TempEntityRotateSystem : SystemBase
  7. {
  8. protected override void OnUpdate()
  9. {
  10. float deltaTime =Time.DeltaTime;
  11. float angel = 0.01f;
  12. Entities.
  13. WithAll<RotateTag,TempEntityTag>().
  14. ForEach(( ref Translation translation, in Target target) =>
  15. {
  16. #region TempEntity围绕剑阵中心点旋转
  17. float3 pos = translation.Value;
  18. //旋转轴和旋转角度
  19. quaternion rot = quaternion.AxisAngle(math.up(), angel);
  20. float3 dir = pos - target.Tpos;
  21. dir = math.mul(rot, dir);
  22. translation.Value = target.Tpos + dir;
  23. #endregion
  24. }).ScheduleParallel();
  25. }
  26. }

System2:

  1. using Unity.Entities;
  2. using Unity.Jobs;
  3. using Unity.Transforms;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using Unity.Collections;
  7. using Unity.Physics.Systems;
  8. using Unity.Physics;
  9. [UpdateAfter(typeof(TempEntityRotateSystem))]
  10. public class SwordRotateSystem : SystemBase
  11. {
  12. EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
  13. protected override void OnCreate()
  14. {
  15. base.OnCreate();
  16. // 从World中获取ECS系统并且存起来
  17. m_EndSimulationEcbSystem = World
  18. .GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
  19. }
  20. protected override void OnUpdate()
  21. {
  22. bool isGo = false;
  23. float3 hitpos = float3.zero;
  24. float deltaTime = Time.DeltaTime;
  25. // 请求一个ECS并且转换成可并行的
  26. var ecb = m_EndSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
  27. if (Input.GetMouseButtonDown(0))
  28. {
  29. //获取物理世界
  30. BuildPhysicsWorld physicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
  31. NativeArray<RigidBody> rigidBodies = new NativeArray<RigidBody>(1, Allocator.TempJob);
  32. NativeArray<float3> rayHitPos = new NativeArray<float3>(1, Allocator.TempJob);
  33. //获取射线发射位置
  34. UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35. RaycastJobHandle raycastJonHande = new RaycastJobHandle()
  36. {
  37. mStartPos = ray.origin,
  38. mEndPos = ray.direction * 10000,
  39. physicsWorld = physicsWorld.PhysicsWorld,
  40. Bodies = rigidBodies,
  41. rayHitpos = rayHitPos
  42. };
  43. //需要依赖当前Job
  44. JobHandle jobHandle = raycastJonHande.Schedule(this.Dependency);
  45. jobHandle.Complete();
  46. if (rigidBodies[0].Entity != null)
  47. {
  48. Debug.Log("目标坐标:" + rayHitPos[0]);
  49. Debug.Log("射线击中目标" + rigidBodies[0].Entity);
  50. hitpos = rayHitPos[0];
  51. isGo = true;
  52. }
  53. rigidBodies.Dispose();
  54. rayHitPos.Dispose();
  55. }
  56. Entities.
  57. WithAll<SwordTag>().
  58. WithNone<GoTag>().
  59. ForEach((Entity entity, int entityInQueryIndex, ref Translation translation, ref Rotation orientation, ref Target target) =>
  60. {
  61. #region 飞剑群出击!
  62. if (isGo && entityInQueryIndex < 10000)
  63. {
  64. GoTag tag = new GoTag
  65. {
  66. targetPos = hitpos,
  67. TempEntity = target.targetTempentity,
  68. originPos = translation.Value,
  69. isBack = false
  70. };
  71. // 将entityInQueryIndex传给操作,这样ECS回放时能保证正确的顺序
  72. ecb.AddComponent(entityInQueryIndex, entity, tag);
  73. }
  74. #endregion
  75. if (!HasComponent<LocalToWorld>(target.targetTempentity))
  76. {
  77. return;
  78. }
  79. var rotation = orientation;
  80. float3 targetPosition = target.Tpos;
  81. var targetDir = targetPosition - translation.Value;
  82. //飞剑垂直向下面向中心点
  83. quaternion temp1 = Quaternion.FromToRotation(Vector3.left, targetDir);
  84. orientation.Value = temp1;
  85. LocalToWorld tempEntityPos = GetComponent<LocalToWorld>(target.targetTempentity);
  86. translation.Value = tempEntityPos.Position;
  87. }).ScheduleParallel();
  88. // 保证ECB system依赖当前这个Job
  89. m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
  90. }
  91. //发射射线Job
  92. public struct RaycastJobHandle : IJob
  93. {
  94. public NativeArray<RigidBody> Bodies;
  95. public NativeArray<float3> rayHitpos;
  96. public float3 mStartPos;
  97. public float3 mEndPos;
  98. public PhysicsWorld physicsWorld;
  99. public void Execute()
  100. {
  101. //创建输入
  102. RaycastInput raycastInput = new RaycastInput()
  103. {
  104. Start = mStartPos,
  105. End = mEndPos * 100,
  106. //声明碰撞过滤器,用来过滤某些层级下的物体是否进行射线检测
  107. Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0, }
  108. };
  109. Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
  110. // 发射射线去检测Entity实体
  111. if (physicsWorld.CollisionWorld.CastRay(raycastInput, out raycastHit))
  112. {
  113. //拿到我们射线击中的entity
  114. Bodies[0] = physicsWorld.Bodies[raycastHit.RigidBodyIndex];
  115. //拿到击中点的位置信息
  116. rayHitpos[0] = raycastHit.Position;
  117. }
  118. }
  119. }
  120. }

System3:

  1. using Unity.Entities;
  2. using Unity.Jobs;
  3. using Unity.Transforms;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. [UpdateAfter(typeof(SwordRotateSystem))]
  7. public class GroupSystem : SystemBase
  8. {
  9. EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
  10. protected override void OnCreate()
  11. {
  12. base.OnCreate();
  13. // 从World中获取ECS系统并且存起来
  14. m_EndSimulationEcbSystem = World
  15. .GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
  16. }
  17. protected override void OnUpdate()
  18. {
  19. // 请求一个ECS并且转换成可并行的
  20. var ecb = m_EndSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
  21. float deltaTime = Time.DeltaTime;
  22. float angel = 0.01f;
  23. Entities
  24. .WithName("Group").
  25. ForEach((Entity entity, int entityInQueryIndex, ref Translation translation, ref Rotation orientation, ref GoTag goTag, ref Target target) =>
  26. {
  27. var rotation = orientation;
  28. float3 targetPosition = goTag.targetPos;
  29. float distance = math.distance(targetPosition, translation.Value);
  30. LocalToWorld targetTransform = GetComponent<LocalToWorld>(goTag.TempEntity);
  31. //距离目标点位置小于30,则返回剑阵
  32. if (distance < 30f)
  33. {
  34. if (goTag.TempEntity != null)
  35. {
  36. goTag.isBack = true;
  37. }
  38. }
  39. //追上自己对应的Tempentity
  40. if (goTag.isBack)
  41. {
  42. float3 newPos = targetTransform.Position;
  43. var a = newPos - translation.Value;
  44. //飞剑剑头指向目标点
  45. quaternion b = Quaternion.FromToRotation(Vector3.down, a);
  46. orientation.Value = b;
  47. float d1 = math.distance(translation.Value, newPos);
  48. translation.Value += math.normalizesafe(a);
  49. float d2 = math.distance(translation.Value, newPos);
  50. float c = math.distance(newPos, float3.zero) / 100f;
  51. float d = d1 - d2;
  52. if (d1 >10+c)
  53. {
  54. int loop = (int )((10 + c) / d);
  55. for (int i = 0; i < loop; i++)
  56. {
  57. translation.Value += math.normalizesafe(a);
  58. }
  59. }
  60. else
  61. {
  62. target.Tpos = float3.zero;
  63. translation.Value = targetTransform.Position;
  64. float distance3 = math.distance(newPos, translation.Value);
  65. ecb.RemoveComponent(entityInQueryIndex, entity, ComponentType.ReadWrite<GoTag>());
  66. }
  67. return;
  68. }
  69. #region 飞向目标点
  70. var targetDir = targetPosition - translation.Value;
  71. quaternion temp1 = Quaternion.FromToRotation(Vector3.down, targetDir);
  72. orientation.Value = temp1;
  73. float3 distancePos = goTag.targetPos - goTag.originPos;
  74. translation.Value += distancePos * deltaTime * target.randomSpeed / 5f;
  75. #endregion
  76. }).ScheduleParallel();
  77. // 保证ECB system依赖当前这个Job
  78. m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
  79. }
  80. }

然后在主线程中生成飞剑和TempEntity就可以了,需要注意的是,场景中的地面也需要转换为Entity,并且添加上Physic相关组件。其他的代码就不放了,感兴趣的小伙伴可以下载工程试一下,我放在文章末尾了。最后我们来看下俯视角效果:

我的电脑是i5-7500四核+GTX1050,场景中有12万8千个实体,正常运行能跑85fps,一万把飞剑出阵也能保持在60左右:

十、参考

文章到这就结束了,回顾下来,踩的坑非常多,希望这篇文章能给小伙伴提供帮助,也期待大佬指正!

对于GameObject转换为Entity流程感兴趣的同学,可以移步这篇文章:

砂糖酱:Dots代码阅读笔记——ConvertToEntity的一生

官方文档

工程地址:Unity-ECS-TaiJi-Swords-Array


这是侑虎科技第1437篇文章,感谢作者炎拳供稿。欢迎转发分享,未经作者授权请勿转载。如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。

作者主页:ydwj的游戏开发日记

再次感谢炎拳的分享,如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/387531
推荐阅读
相关标签
  

闽ICP备14008679号