UnityDOTS備忘

Unity DOTS中創建一個AssetBundle并將其用作Entity

  1. 創建一個新的Unity項目,并確保已啟用DOTS功能。

  2. 創建一個AssetBundle,可以通過在Project視圖中右鍵單擊文件夾并選擇“Create > AssetBundle”來創建。

  3. 將您想要轉換為Entity的資源(例如模型、紋理等)拖放到AssetBundle中。

  4. 創建一個新的C#腳本,用于加載AssetBundle并將其轉換為Entity。以下是一個示例代碼:

System間的數

using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using UnityEngine;
using Unity.Mathematics;

public class AssetBundleToEntity : MonoBehaviour
{
? ? public GameObject prefab;
? ? public Material material;

? ? void Start()
? ? {
? ? ? ? var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

? ? ? ? var entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);

? ? ? ? Entity entity = entityManager.Instantiate(entityPrefab);

? ? ? ? entityManager.AddComponentData(entity, new Translation { Value = new float3(0, 0, 0) });

? ? ? ? entityManager.AddSharedComponentData(entity, new RenderMesh
? ? ? ? {
? ? ? ? ? ? material = material,
? ? ? ? ? ? mesh = entityManager.GetSharedComponentData<RenderMesh>(entityPrefab).mesh
? ? ? ? });
? ? }
}

據傳遞

動態加載代碼

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

public class DynamicAssetBundleLoading : MonoBehaviour
{
? ? public string assetBundlePath;
? ? public string assetName;
? ? public Material material;

? ? IEnumerator Start()
? ? {
? ? ? ? AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(assetBundlePath);
? ? ? ? yield return request;

? ? ? ? AssetBundle assetBundle = request.assetBundle;

? ? ? ? AssetBundleRequest assetRequest = assetBundle.LoadAssetAsync<GameObject>(assetName);
? ? ? ? yield return assetRequest;

? ? ? ? GameObject prefab = assetRequest.asset as GameObject;

? ? ? ? var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

? ? ? ? var entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);

? ? ? ? Entity entity = entityManager.Instantiate(entityPrefab);

? ? ? ? entityManager.AddComponentData(entity, new Translation { Value = new float3(0, 0, 0) });

? ? ? ? entityManager.AddSharedComponentData(entity, new RenderMesh
? ? ? ? {
? ? ? ? ? ? material = material,
? ? ? ? ? ? mesh = entityManager.GetSharedComponentData<RenderMesh>(entityPrefab).mesh
? ? ? ? });

? ? ? ? assetBundle.Unload(false);
? ? }
}

釋放Entity

using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using UnityEngine;

public class ReleaseEntity : MonoBehaviour
{
? ? EntityManager entityManager;

? ? void Start()
? ? {
? ? ? ? entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

? ? ? ? // 創建Entity
? ? ? ? Entity entity = entityManager.CreateEntity();

? ? ? ? // 添加組件數據
? ? ? ? entityManager.AddComponentData(entity, new Translation { Value = new float3(0, 0, 0) });

? ? ? ? // 添加SharedComponent數據
? ? ? ? entityManager.AddSharedComponentData(entity, new RenderMesh
? ? ? ? {
? ? ? ? ? ? material = GetComponent<Renderer>().material,
? ? ? ? ? ? mesh = GetComponent<MeshFilter>().sharedMesh
? ? ? ? });
? ? }

? ? void OnDestroy()
? ? {
? ? ? ? // 釋放Entity
? ? ? ? entityManager.DestroyEntity(entity);

? ? ? ? // 釋放相關的GameObject和AssetBundle引用
? ? ? ? Destroy(gameObject);
? ? ? ? Resources.UnloadUnusedAssets();
? ? }
}

System以MonoBehaviour數據事件通知

在Unity的Data-Oriented Technology Stack (DOTS)中,如果您希望在ECS系統中的Job執行完畢后通知MonoBehaviour或外部代碼,可以使用Unity的Events系統或自定義事件系統來實現通信。以下是一種常見的方法:

  1. 使用Unity的Events系統:
    • 在MonoBehaviour中定義一個事件,當事件被觸發時通知外部代碼。
    • 在ECS系統中,創建一個System,在System中檢查Job是否執行完畢,并在適當的時機觸發MonoBehaviour中定義的事件。
    • using Unity.Entities;
      using UnityEngine;
      using UnityEngine.Events;

      public class JobCompletedEvent : MonoBehaviour
      {
      ? ? public UnityEvent onJobCompleted;

      ? ? public void TriggerJobCompletedEvent()
      ? ? {
      ? ? ? ? onJobCompleted.Invoke();
      ? ? }
      }

      public class JobCompletionSystem : ComponentSystem
      {
      ? ? protected override void OnUpdate()
      ? ? {
      ? ? ? ? // 檢查Job是否執行完畢
      ? ? ? ? if (jobCompleted)
      ? ? ? ? {
      ? ? ? ? ? ? // 獲取JobCompletedEvent組件
      ? ? ? ? ? ? JobCompletedEvent jobCompletedEvent = FindObjectOfType<JobCompletedEvent>();

      ? ? ? ? ? ? // 觸發事件通知外部代碼
      ? ? ? ? ? ? jobCompletedEvent?.TriggerJobCompletedEvent();
      ? ? ? ? }
      ? ? }
      }

    • 自定義事件系統:
      • 創建一個自定義的事件系統,允許ECS系統和MonoBehaviour之間進行通信。
      • 在ECS系統中,觸發自定義事件來通知MonoBehaviour或外部代碼。
      • using Unity.Entities;
        using UnityEngine;

        public class CustomEventSystem : ComponentSystem
        {
        ? ? public delegate void JobCompletedEventHandler();
        ? ? public static event JobCompletedEventHandler OnJobCompleted;

        ? ? protected override void OnUpdate()
        ? ? {
        ? ? ? ? // 檢查Job是否執行完畢
        ? ? ? ? if (jobCompleted)
        ? ? ? ? {
        ? ? ? ? ? ? // 觸發自定義事件通知外部代碼
        ? ? ? ? ? ? OnJobCompleted?.Invoke();
        ? ? ? ? }
        ? ? }
        }

  2. IBufferElementData

    要將DynamicBuffer與Entity關聯,我們只需要定義需要的IBufferElementData,而不是DynamicBuffer本身,使用GenerateAuthoringComponent屬性會自動幫我們生成DynamicBuffer并添加到Entity上,這里需要注意一下,下面定義一個簡單的IBufferElementData看一下吧:

  3. using Unity.Entities;[GenerateAuthoringComponent]
    public struct IntBufferElement : IBufferElementData
    {public int Value;
    }

?添加與釋放

EntityManager.AddComponent<IntBufferElement>(yourEntity);
EntityManager.RemoveComponent<IntBufferElement>(yourEntity);

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/11331.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/11331.shtml
英文地址,請注明出處:http://en.pswp.cn/web/11331.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

炒股開戶傭金最低萬1和萬0.854,融資融券現在利率最低4.0%~5%

??炒股開戶傭金一般是萬1和萬0.854&#xff0c;萬0.854有一定的資金量要求&#xff0c;高于萬1的是可以申請降低的。 開戶萬1傭金和萬0.854傭金只需要聯系證券公司客戶經理協商就行。 開戶流程&#xff1a; 1、向客戶經理索要開戶鏈接或者掃描二維碼、進入申請頁面&#x…

本地搭建各大直播平臺錄屏服務結合內網穿透工具實現遠程管理錄屏任務

文章目錄 1. Bililive-go與套件下載1.1 獲取ffmpeg1.2 獲取Bililive-go1.3 配置套件 2. 本地運行測試3. 錄屏設置演示4. 內網穿透工具下載安裝5. 配置Bililive-go公網地址6. 配置固定公網地址 本文主要介紹如何在Windows系統電腦本地部署直播錄屏利器Bililive-go&#xff0c;并…

Nachi那智不二越機器人維修技術合集

一、Nachi機械手維護基礎知識 1. 定期檢查&#xff1a;定期檢查機器人的各個部件&#xff0c;如機械手伺服電機、機器人減速器、機械臂傳感器等&#xff0c;確保其運行正常。 2. 清潔與潤滑&#xff1a;定期清潔Nachi工業機器人表面和內部&#xff0c;并使用合適的潤滑油進行潤…

VRRP協議-負載分擔配置【分別在路由器與交換機上配置】

VRRP在路由器與交換機上的不同配置 一、使用路由器實現負載分擔二、使用交換機實現負載分擔一、使用路由器實現負載分擔 使用R1與R2兩臺設備分別進行VRRP備份組 VRRP備份組1,虛擬pc1的網關地址10.1.1.254 VRRP備份組2,虛擬pc2的網關地址10.1.1.253 ①備份組1的vrid=1,vrip=…

修正牛頓法求解無約束問題

function [x,val,k]revisenm(fun,gfun, Hess, x0) %功能:用修正牛頓法求解無約束問題:min f(x) %輸入:x0是初始點&#xff0c;fun, gfun,Hess分別是求目標函數值&#xff0c;梯度&#xff0c;Hesse矩陣的函數 %輸出:x,val分別是近似最優點和最優值&#xff0c;k是迭代次數 nl…

vue3中使用cherry-markdown

附cherry-markdown官網及api使用示例 官網:https://github.com/Tencent/cherry-markdown/blob/main/README.CN.md api:Cherry Markdown API 考慮到復用性,我在插件的基礎上做了二次封裝,步驟如下: 1.下載 (一定要指定版本0.8.22,否則會報錯: [vitel Internal server e…

初識指針(5)<C語言>

前言 在前幾篇文章中&#xff0c;已經介紹了指針一些基本概念、用途和一些不同類型的指針&#xff0c;下文將介紹某些指針類型的運用。本文主要介紹函數指針數組、轉移表&#xff08;函數指針的用途&#xff09;、回調函數、qsort使用舉例等。 函數指針數組 函數指針數組即每個…

深度學習知識點全面總結

ChatGPT 深度學習是一種使用神經網絡來模擬人腦處理數據和創建模式的機器學習方法。下面是深度學習的一些主要知識點的總結&#xff1a; 1. 神經網絡基礎&#xff1a; - 神經元&#xff1a;基本的計算單元&#xff0c;模擬人腦神經元。 - 激活函數&#xff1a;用于增加神…

【CSP CCF記錄】數組推導

題目 過程 思路 每次輸入一個Bi即可確定一個Ai值&#xff0c;用temp記錄1~B[i-1]&#xff0c;的最大值分為兩種情況&#xff1a; 當temp不等于Bi時&#xff0c;則說明Bi值之前未出現過&#xff0c;Ai必須等于Bi才能滿足Bi是Ai前綴最大的定義。當temp等于Bi時&#xff0c;則說…

SpringAMQP-消息轉換器

這邊發送消息接收消息默認是jdk的序列化方式&#xff0c;發送到服務器是以字節碼的形式&#xff0c;我們看不懂也很占內存&#xff0c;所以我們要手動設置一下 我這邊設置成json的序列化方式&#xff0c;注意發送方和接收方的序列化方式要保持一致 不然回報錯。 引入依賴&#…

重磅推出:135屆廣交會采購商名錄,囊括28個行業數據!

5.5日&#xff0c;第135屆中國進出口商品交易會&#xff08;簡稱廣交會&#xff09;在廣州圓滿閉幕&#xff0c;這一全球貿易盛典再次展現了中國制造的卓越實力和文化魅力&#xff0c;成就斐然&#xff0c;吸引了全球目光。 本屆廣交會線下出口成交額達247億美元&#xff0c;對…

項目-坦克大戰-讓坦克動起來

為什么寫這個項目 好玩涉及到java各個方面的技術 1&#xff0c;java面向對象 2&#xff0c;多線程 3&#xff0c;文件i/o操作 4&#xff0c;數據庫鞏固知識 java繪圖坐標體系 坐標體系-介紹 坐標體系-像素 計算機在屏幕上顯示的內容都是由屏幕上的每一個像素組成的像素是一…

詳細解讀字節跳動視頻生成模型StoryDiffusion:連環畫創作和長視頻生成

Diffusion Models專欄文章匯總:入門與實戰 前言:最近字節跳動提出了一項關于特征一致圖像生成的工作StoryDiffusion,可以生成一系列特征主題相關的圖像,還可以擴展成具有時間一致性的幀序列,從而組成長視頻。這篇博客火速解讀這篇最新的工作,包括論文和代碼。 目錄 貢獻…

安卓開發問題:安卓Ble出現動態鑒權失敗以及掃描設備一直進入不了回調函數的問題

問題1描述 1、安卓12需要動態鑒權 // 鑒權函數 requestPermissions(permissionsList.toArray(strings), MyConstants.REQUEST_CODE_PERMISSIONS);但是在鑒權回調函數中如Manifest.permission.BLUETOOTH_SCAN、Manifest.permission.BLUETOOTH_CONNECT一直顯示失敗&…

力扣HOT100 - 70. 爬樓梯

解題思路&#xff1a; 動態規劃 注意 if 判斷和 for 循環 class Solution {public int climbStairs(int n) {if (n < 2) return n;int[] dp new int[n 1];dp[1] 1;dp[2] 2;for (int i 3; i < n; i) {dp[i] dp[i - 1] dp[i - 2];}return dp[n];} }

設計模式-10 - Memento Design Pattern

設計模式-10 - Memento Design Pattern 1.定義 備忘錄模式是一種設計模式&#xff0c;它允許在不破壞封裝性的情況下捕獲和恢復對象的內部狀態。 其結構&#xff1a; Originator&#xff1a;創建和管理備忘錄的對象。 Memento&#xff1a;存儲 Originator 狀態的備忘錄對象…

品牌設計理念和logo設計方法

一 品牌設計的目的 設計是為了傳播&#xff0c;讓傳播速度更快&#xff0c;傳播效率更高&#xff0c;減少宣傳成本 二 什么是好的品牌設計 好的設計是為了讓消費者更容易看懂、記住的設計&#xff0c; 從而輔助傳播&#xff0c; 即 看得懂、記得住。 1 看得懂 就是讓別人看懂…

樹莓派|采集視頻并實時顯示畫面

1、使用SSH遠程連接到樹莓派 2、新建存放代碼的目錄 mkdir /home/pi/my_code_directory 3、進入存放代碼的目錄 cd /home/pi/my_code_directory 4、新建py文件 nano cv2test.py 5、輸入代碼 import cv2# 打開攝像頭 cap cv2.VideoCapture(0)while True:# 讀取視頻幀ret…

BGP學習二:BGP通告原則,BGP反射器,BGP路徑屬性細致講解,新手小白無負擔

目錄 一.AS號 二.BGP路由生成 1.network 2.import-route引入 三.BGP通告原則 1.只發布最優且有效的路由 2.從EBGP獲取的路由&#xff0c;會發布給所有對等體 3.水平分割原則 4.IBGP學習BGP默認不發送給EBGP&#xff0c;但如果也從IGP學習到了這條路由&#xff0c;就發…

mysql 幻讀,臟讀和不可重復讀

MySQL的臟讀、幻讀和不可重復讀是數據庫事務處理中的三種常見問題&#xff0c;它們都涉及到數據的一致性和并發性。一&#xff0c;臟讀 臟讀是指一個事務讀取了另一個事務未提交的數據。這可能導致數據不一致的問題。 例如&#xff1a; 用戶user1的初始balance是100&#xff0…