日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity DOTS 一文开启ECS大门

發布時間:2023/12/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity DOTS 一文开启ECS大门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Unity DOTS 一文開啟ECS大門

  • 前言
  • 環境
  • 過程
    • 新建URP項目
    • 導入Entity等插件包
    • 開啟Entity Debugger窗口
    • 編寫第一個Entity腳本
    • 為Entity添加Component
    • 創建一個System
  • 參考

前言

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

點擊Advanced Project Settings

更改
Enable Pre-release Packages
Show Despendencies
兩項的設置


關閉設置

升級包

這兩個同樣升級一下

確保三個包都升級完畢,關閉PackageManager窗口

開啟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的值在不斷變化

至此Entity Component System系統全部正常運行。

參考

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

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

  • 總結

    以上是生活随笔為你收集整理的Unity DOTS 一文开启ECS大门的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。