安装

1.在 Package Manager 面板,点击左边 + 号,选择 git :  输入 com.unity.entities 就会自动安装
2.manifest.json 加入 "com.unity.rendering.hybrid": "0.51.0-preview.32"

项目设置

1.Color Space改为 Linear
2.URP的设置中需要勾选Advanced SRP Batcher

文档地址

https://docs.unity3d.com/Packages/com.unity.entities@0.51/api/index.html
https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.51/manual/creating-a-new-hybrid-renderer-project.html

Build Config设置

建立共用的 config,另外还需 建立 Android、win、IOS的配置

RotationSpeed_ForEach.cs

必须是struct,不能是 class,继承IComponentData

1
2
3
4
5
6
7
using Unity.Entities;

[GenerateAuthoringComponent]
public struct RotationSpeed_ForEach:IComponentData
{
public float RadiansPerSecond;
}

RotationSpeedSystem_ForEach.cs

class必须加partial关键字,继承 SystemBase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

public partial class RotationSpeedSystem_ForEach:SystemBase
{
protected override void OnUpdate()
{
float deltaTime = Time.DeltaTime;

Entities.WithName("RotationSpeedSystem_ForEach")
.ForEach((ref Rotation rotation, in RotationSpeed_ForEach rotationSpeed) =>
{
rotation.Value = math.mul(
math.normalize(rotation.Value),
quaternion.AxisAngle(math.up(), rotationSpeed.RadiansPerSecond * deltaTime));
})
.ScheduleParallel();

}
}

新建 Cube

选中该Cube,左上角 Tag 标签下 勾选  ConvertToEntity ,挂载 RotationSpeedSystem_ForEach 脚本,速度填写 2 ,运行就能看到效果

示意图

示意图