Unity DOTS 一文开启ECS大门

9 篇文章 0 订阅

前言

Unity DOTS 已经迎来1.0版本更新,但是目前开发DOTS还得用Entity 0.51.1版本比较合适,资料相对较多,Bug相对较少。下文就从最基本的开始,创建一个Entity+Component+System。

环境

软件版本
Unity2021.3.8f1
Visual Studio2019
软件包版本
URP12.1.7
Entity0.51.1-preview.21
Rendering.Hybrid0.51.1-preview.21
physics0.51.1-preview.21

过程

新建URP项目

新建项目

导入Entity等插件包

项目创建完成后,在任务管理器中打开工程目录,找到Packages文件夹
打开项目目录

打开文件夹,找到manifest.json文件
找到文件

打开并添加
添加内容

    "com.unity.entities": "0.51.1",
    "com.unity.physics": "0.51.1",
    "com.unity.rendering.hybrid": "0.51.1",

保存后回到Unity,打开PackageManager
打开PackageManager

点击Advanced Project Settings
在这里插入图片描述
更改
Enable Pre-release Packages
Show Despendencies
两项的设置
在这里插入图片描述
在这里插入图片描述
关闭设置
在这里插入图片描述

升级包
升级包
这两个同样升级一下
升级包
确保三个包都升级完毕,关闭PackageManager窗口

开启Entity Debugger窗口

开启Entity Debugger窗口便于查看
开启Entity Debugger窗口

编写第一个Entity脚本

创建脚本Spawer并挂载
创建脚本
在脚本中编写以下代码
编写代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using Unity.Mathematics;

public class Spawer : MonoBehaviour
{
    void Start()
    {
        MakeEntities();
    }
    private void MakeEntities()
    {
        //拿到默认世界中的实体管理器
        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        //创建一个实体
        Entity myEntity = entityManager.CreateEntity();
    }
}

保存并回到Unity,点击运行
点击运行
可以在Debugger窗口中看到我们刚创建的Entity
其中
Entity 0 为 PhysicsSystem
WorldTime 为 游戏时间
GameObject Scene 为 游戏场景

为Entity添加Component

创建脚本LevelComponent并添加以下代码
Tip:不需要拖拽到场景物体上
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public struct LevelComponent : IComponentData
{
    public float level;
}

修改Spawer脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using Unity.Mathematics;

public class Spawer : MonoBehaviour
{
    void Start()
    {
        MakeEntities();
    }
    private void MakeEntities()
    {
        //拿到默认世界中的实体管理器
        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        //创建一个原型 用来给实体分配数据
        EntityArchetype archetype = entityManager.CreateArchetype(
            //移动
            typeof(Translation),
            //旋转
            typeof(Rotation),

            //关卡组件
            typeof(LevelComponent)
            );

        //根据原型创建一个实体
        Entity myEntity = entityManager.CreateEntity(archetype);
    }
}

运行场景

运行
可以看到我们创建的Entity已经挂载了LevelComponent

创建一个System

创建一个System来控制LevelComponent中的值
创建LevelSystem脚本,并添加以下内容
添加脚本内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public class LevelSystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref LevelComponent levelComponent) => {
            levelComponent.level += 1f * Time.DeltaTime;
        });
    }
}

保存并运行场景
level在变化
可以看到Level的值在不断变化

至此Entity Component System系统全部正常运行。

参考

  1. https://docs.unity3d.com/Packages/com.unity.entities@0.51/manual/index.html

  2. https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.51/manual/requirements-and-compatibility.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 一: 什么是ECS?       ECS是一种新的架构模式(当然是针对Unity本身来说),这是一个取代GameObject / Component 的模式。 其模式遵循组合优于继承原则,游戏内的每一个基本单元都是一个实体,每个实体又由一个或多个组件构成,每个组件仅仅包含代表其特性的数据(即在组件中没有任何方法)。系统便是来处理拥有一个或多个相同组件的实体集合的工具,其只拥有行为(即在系统中没有任何数据)。       ECS旨在比GameObject / MonoBehaviour更容易处理大量物体。ECS由于是面向数据的设计,所以很容易并行高速处理,以及与Unity开发的C#JobSystem/Burst Compiler一起工作,从而获取更高的项目运行性能。二:ECS总体发展历史       目前Unity公司由Mike Acton 大佬主持DOD(Data Oriented Design 面向数据设计)的全面改革与推进工作,目的是旨在进一步推进Unity系统本身与Unity项目的执行效率。    ECS总体发展:    A: 在Unity2018引入Entities之前,其实ECS框架早已经出现,但是ECS还是因为守望先锋采用了这个框架才被我们所熟知,后来Git上面出现了一个Entitas的插件可以直接用在Unity上面。Entitas-Unity(有多个语言的port版本,Entitas-Unity下统一称为Entitas) 是 Unity的一个ECS(Entity/Component/System)框架,是在github上面的一个开源项目。    B: 经过Unity公司的认可与改造,目前Unity2018.x 版本已经通过 Package Manager 提供了Unity版本的ECS插件,名称为:“Entities”。    C: Unity2019.x 版本,对“Entities”插件的API进行了进一步改造与完善,以更规范的API实现更加高效的与规范的编程模式。 三:ECS(一)轻松入门篇       本“ECS入门篇”旨在全面讲解ecs 的相关理论与简单Hello World 的编程模式,且通过Unity2018.x与Unity2019.x 两个不同API的编程模式,初步讲解ECS的编程规范与模式规范。        需要进一步学习ECS的小伙伴们,可以关注后续“ECS(二)小试牛刀”篇的进一步讲解。   《Unity ECS(二) 小试牛刀》https://edu.csdn.net/course/detail/27096 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值