文章目錄
- 前言
- 安裝 DOTS 包
- 什么是Authoring
- 1. 實體組件
- 2. Authoring類
前言
DOTS(面向數據的技術堆棧)是一套由 Unity 提供支持的技術,用于提供高性能游戲開發解決方案,特別適合需要處理大量數據的游戲,例如大型開放世界游戲。
我們今天來講一下,Dots框架中,Authoring類的創建和使用。
- 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
Authoring 類通常用于定義和管理實體和組件的元數據以及如何進行編輯和創建。
這一類主要的功能包括:
1.實體和組件定義:Authoring 類可以用于聲明或定義在游戲中使用的不同實體和組件。這使得開發者可以方便地創建和配置其對應的數據結構。
2.編輯器集成:這些類通常與 Unity 編輯器相結合,使開發者可以在編輯模式下直觀地創建和配置游戲實體。它們提供了可視化的工具,以便非程序員也可以參與設計游戲內容。
3.數據管理:Authoring 類幫助管理數據的加載、保存和版本控制。它們可以幫助開發者組織復雜的數據結構,使得資源管理更加高效。
4.轉換為運行時組件:這些類通常還包括邏輯,將數據在編輯器中設置的狀態轉換為運行時可以使用的實體和組件。這一轉化通常伴隨著實體的創建和初始化過程。
1. 實體組件
新建一個Rotator的ComponentData組件
using Unity.Entities;public struct Rotator : IComponentData
{public float speed;
}
2. Authoring類
using UnityEngine;
using Unity.Entities;public class RotatorAuthoring : MonoBehaviour
{public float Speed;//將 MonoBehaviour 的 RotatorAuthoring 組件數據(如 Speed)//轉換為 ECS 的 Rotator 組件數據(IComponentData)public class RotatorBaker : Baker<RotatorAuthoring>{public override void Bake(RotatorAuthoring authoring){//通過Bake方式,把當前游戲對象轉換為Entityvar entity = GetEntity(TransformUsageFlags.Dynamic);//給Entity添加數據組件AddComponent(entity, new Rotator { speed = authoring.Speed });}}
}