Unity Dots从入门到精通之 Prefab引用 转 实体引用

前言

DOTS(面向数据的技术堆栈)是一套由 Unity 提供支持的技术,用于提供高性能游戏开发解决方案,特别适合需要处理大量数据的游戏,例如大型开放世界游戏。

本文讲解我在开发Dots的过程中,当我们有一些预制体,希望作为实体,在运行时会克隆出来的,比如士兵和子弹。我们可以新建一个EntitiesReferences实体引用类。用来存储多个预制体转化的实体引用。

  • Unity 2022.3.52f1
  • Entities 1.3.10

安装 DOTS 包

要安装 DOTS 包,请按照以下步骤操作:

(1)从菜单“窗口 → 包管理器”打开包管理器。
(2)搜索“ Entities” 并安装 Entities和Entities Graphics。
(3)搜索“ Universal RP” 并安装 Universal RP,并设置Graphics/Scriptable Render Pipeline Settings。

这会添加“实体”作为依赖项,并递归添加它所依赖的关联包( Burst、Collections、Jobs、Mathematics等)。

在这里插入图片描述

实体引用Authoring

using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;

public class EntitiesReferencesAuthoring : MonoBehaviour
{
    public GameObject redSoldierPrefab;
    public GameObject blueSoldierPrefab;
    
    public GameObject bulletPrefabPrefab;
    
    public Transform redStartPoint;
    public Transform blueStartPoint;

	//Baker类,把Mono转为Entity
    private class Baker : Baker<EntitiesReferencesAuthoring>
    {
        public override void Bake(EntitiesReferencesAuthoring authoring)
        {
            Entity entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent(entity, new EntitiesReferences
            {
                redMeleePrefabEntity = GetEntity(authoring.redSoldierPrefab, TransformUsageFlags.Dynamic),
                blueMeleePrefabEntity = GetEntity(authoring.blueSoldierPrefab, TransformUsageFlags.Dynamic),
               
                bulletPrefabEntity = GetEntity(authoring.bulletPrefabPrefab, TransformUsageFlags.Dynamic),
                
                redStartPosition = authoring.redStartPoint.position,
                blueStartPosition = authoring.blueStartPoint.position,
            });
        }
    }
}

//实体引用类
public struct EntitiesReferences : IComponentData
{
    public Entity redMeleePrefabEntity;
    public Entity blueMeleePrefabEntity;

    public Entity bulletPrefabEntity;
    
    public float3 redStartPosition;
    public float3 blueStartPosition;
}
### Unity DOTS 入门教程 #### 了解 Data-Oriented Technology Stack (DOTS) Data-Oriented Technology Stack 是一组用于构建高效、可扩展的游戏和技术应用程序的技术栈。它旨在利用现代硬件特性,特别是多核处理器的能力来提高性能。 #### 使用 Unity 版本和 Package Manager 安装必要组件 对于 Unity 2020.X 或更高版本,在 Package Manager 中可以通过添加特定包 URL 来安装必要的 DOTS 组件[^2]: - `com.unity.dots.editor` - `com.unity.physics` - `com.unity.entities` - `com.unity.rendering.hybrid` 这些工具提供了创建基于实体系统的逻辑所需的功能支持。 #### 创建并配置 Entities Prefab 为了使对象能够参与数据导向型编程模型,需将 GameObject 换为 Entity 并设置相应的组件。例如,可以将 EntityManager 和 ConvertToEntity 组件附加到 Cube 上,并将其换为预制件(Prefab)[^3]。这一步骤允许开发者定义如何管理游戏世界内的大量实例化对象的数据结构。 #### 编写简单的 Jobsystem 示例代码 下面是一个简单的工作系统(Job System)实现示例,展示了如何分配任务给多个线程以加速处理速度: ```csharp using Unity.Burst; using Unity.Collections; using Unity.Jobs; [BurstCompile] struct SimpleJob : IJobParallelFor { public NativeArray<float> data; public void Execute(int index){ data[index] *= 2f; // 对数组中的每一个元素乘以2 } } void ScheduleSimpleJob(){ var job = new SimpleJob{ data = someNativeArray }; JobHandle handle = job.Schedule(someLength, 64); // 启动作业并将工作划分为大小为64的批次 } ``` 此代码片段使用了 Burst 编译器优化后的平行循环操作,从而提高了大规模数值运算的速度。 #### 实践案例:Unity DOTS-MAN 小游戏项目 通过实际动手制作一个小游戏如 "DOTS-MAN", 可加深对上述概念的理解。该项目不仅涉及到了基本的 ECS 构建模式,还可能包括碰撞检测、动画控制等功能模块的设计与集成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TxNet.Ltd.

你的赞同是对我的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值