首页 > 其他分享 >最新Unity DOTS教程之BlobAsset核心机制分析

最新Unity DOTS教程之BlobAsset核心机制分析

时间:2023-12-04 09:45:52浏览次数:31  
标签:DOTS builder BlobAsset Unity result var ref public

最近DOTS发布了正式的版本, 我们来分享一下DOTS里面BlobAsset机制,方便大家上手学习掌握Unity DOTS开发。

BlobAsset 概叙

DOTS提供了BlobAsset机制来把数据生成高效的二进制数据。BlobAsset的数据是不可变的。BlobAsset只支持非托管类型数据。支持Burst编译器编译出来的类型。同时它只存储非托管类型的数据,这样使得它序列化与反序列化的性能与效率非常的块。BlobAsset也可以被entity种的component使用。

对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习游戏的零基础小白,也有一些正在从事游戏开发的技术大佬,欢迎你来交流学习。

BlobAsset不支持如:数组, string等其它的托管对象(垃圾回收器可以回收的为托管对象),而且运行的时候不允许改变里面的数据内容。BlobAsset为了更快的加载,他只会包含传值的类型,不能包含对内部的引用与内部指针。BlobAsset除了支持标准的值类型(float, int等)以外它还支持三种特殊的类型: BlobArray, BlobPtr, BlobString。如果BlobAsest包含内部指针,编译器会报错。使用BlobAsset主要注意以下几个方面:

A: 创建一个BlobAsset,你需要先生成一个BlobBuilder,来帮助你处理计算每个数据在内存种的偏移;

总结:使用BlobBuilder来生成BlobAsset,基于BlobAssetReference来高效访问里面的数据。

BlobAsset的创建与使用

创建一个BlobAsset,按照以下步骤来进行处理即可:

BlobBuilder构造出BlobAsset,并把数据存入进去,然后再生成BlobAssetReference<T>来给用户操作数据。参考代码如下:

 struct MarketData
 {
 public float PriceOranges;
 public float PriceApples;
 }
 BlobAssetReference<MarketData> CreateMarketData()
 {
 // Create a new builder that will use temporary memory to construct the blob asset
 var builder = new BlobBuilder(Allocator.Temp);
 // Construct the root object for the blob asset. Notice the use of `ref`.
 ref MarketData marketData = ref builder.ConstructRoot<MarketData>();
 // Now fill the constructed root with the data:
 // Apples compare to Oranges in the universally accepted ratio of 2 : 1 .
 marketData.PriceApples = 2f;
 marketData.PriceOranges = 4f;
 // Now copy the data from the builder into its final place, which will
 // use the persistent allocator
 var result = builder.CreateBlobAssetReference<MarketData>(Allocator.Persistent);
 // Make sure to dispose the builder itself so all internal memory is disposed.
 builder.Dispose();
 return result;
 }
 #endregion

如果你要存数组到BlobAsset,可以使用BlobArray来处理。数组数据内部基于索引的偏移来进行每个元素的定位。将数组存入到BlobAsset的代码如下:

 #region CreateBlobAssetWithArray
 struct Hobby
 {
 public float Excitement;
 public int NumOrangesRequired;
 }
 struct HobbyPool
 {
 public BlobArray<Hobby> Hobbies;
 }
 BlobAssetReference<HobbyPool> CreateHobbyPool()
 {
 var builder = new BlobBuilder(Allocator.Temp);
 ref HobbyPool hobbyPool = ref builder.ConstructRoot<HobbyPool>();
 // Allocate enough room for two hobbies in the pool. Use the returned BlobBuilderArray
 // to fill in the data.
 const int numHobbies = 2;
 BlobBuilderArray<Hobby> arrayBuilder = builder.Allocate(
 ref hobbyPool.Hobbies,
 numHobbies
 );
 // Initialize the hobbies.
 // An exciting hobby that consumes a lot of oranges.
 arrayBuilder[0] = new Hobby
 {
 Excitement = 1,
 NumOrangesRequired = 7
 };
 // A less exciting hobby that conserves oranges.
 arrayBuilder[1] = new Hobby
 {
 Excitement = 0.2f,
 NumOrangesRequired = 2
 };
 var result = builder.CreateBlobAssetReference<HobbyPool>(Allocator.Persistent);
 builder.Dispose();
 return result;
 }
 #endregion

存储string到BlobAsset, 代码如下:

#region CreateBlobAssetWithString
 struct CharacterSetup
 {
 public float Loveliness;
 public BlobString Name;
 }
 BlobAssetReference<CharacterSetup> CreateCharacterSetup(string name)
 {
 var builder = new BlobBuilder(Allocator.Temp);
 ref CharacterSetup character = ref builder.ConstructRoot<CharacterSetup>();
 character.Loveliness = 9001; // it's just a very lovely character
 // Create a new BlobString and set it to the given name.
 builder.AllocateString(ref character.Name, name);
 var result = builder.CreateBlobAssetReference<CharacterSetup>(Allocator.Persistent);
 builder.Dispose();
 return result;
 }
 #endregion

存储BlobPtr数据到BlobAsset,代码如下:

#region CreateBlobAssetWithInternalPointer
 struct FriendList
 {
 public BlobPtr<BlobString> BestFriend;
 public BlobArray<BlobString> Friends;
 }
 BlobAssetReference<FriendList> CreateFriendList()
 {
 var builder = new BlobBuilder(Allocator.Temp);
 ref FriendList friendList = ref builder.ConstructRoot<FriendList>();
 const int numFriends = 3;
 var arrayBuilder = builder.Allocate(ref friendList.Friends, numFriends);
 builder.AllocateString(ref arrayBuilder[0], "Alice");
 builder.AllocateString(ref arrayBuilder[1], "Bob");
 builder.AllocateString(ref arrayBuilder[2], "Joachim");
 // Set the best friend pointer to point to the second array element.
 builder.SetPointer(ref friendList.BestFriend, ref arrayBuilder[2]);
 var result = builder.CreateBlobAssetReference<FriendList>(Allocator.Persistent);
 builder.Dispose();
 return result;
 }
 #endregion

这里的BlobPtr是基于数据的偏移来存储的;

今天的BlobAsset机制,就给大家分享到这里了,更多的DOTS系列,关注我们,持续更新!

标签:DOTS,builder,BlobAsset,Unity,result,var,ref,public
From: https://www.cnblogs.com/bycw/p/17874255.html

相关文章

  • 最新Unity DOTS系列之Aspect核心机制分析
    最近DOTS发布了正式的版本,我们来分享一下DOTS里面Aspect机制,方便大家上手学习掌握UnityDOTS开发。Aspect 机制概述当我们使用ECS开发的时候,编写某个功能可能需要某个entity的一些组件,如果我们一个个组件的查询出来,可能参数会写很长。如果我们编写某个功能的时候,需要entity的......
  • Unity DOTS系列之Struct Change核心机制分析
    最近DOTS发布了正式的版本,我们来分享一下DOTS里面StructChange机制,方便大家上手学习掌握UnityDOTS开发。基于ArchType与Chunk的Entity管理机制我们回顾以下ECS的内存管理核心机制,基于ArchType+Chunk的Entity管理模式。每个Entity不直接存放数据,数据全部存放到ComponentData......
  • Unity学习笔记--数据持久化Json
    JSON相关json是国际通用语言,可以跨平台(游戏,软件,网页,不同OS)使用,json语法较为简单,使用更广泛。json使用键值对来存储。认识json文件//注意字典类型存储时,键是以string类型存储的需要添加“”{"name":"TonyChang","age":21,"sex":true,"Float":2.5,"arrarys"......
  • (自用)基于unity的指令(命令)模式
    指令模式1.配置输入 所有游戏中都包含玩家输入指令的部分(这些部分通常写在游戏循环中如unity中的UpData())游戏会每一帧都进行一次读取,当玩家按下相应按键时则会进行对应方法 为了可以时刻检测并记录玩家进行的操作,或者对某个对应的操作的指令进行更改,我们需要将这些......
  • 使用unity开发Pico程序,场景中锯齿问题
    1、问题使用unity【非HDR】开发Pico程序,场景中锯齿问题,设置了unity的抗锯齿和渲染方式,及悬挂抗锯齿的脚本,都不能很好的解决项目中图片、文字的锯齿问题,通过摸索找到了妥善的方法1、修改项目中图片的GenerateMIpMaps为勾选状态,MipMapsPreserveCoverage这个可以未勾选,若是勾选......
  • Unity实现的行为树
    游戏AI行为决策——行为树前言行为树,是目前游戏种应用较为广泛的一种行为决策模型。这离不开它成熟的可视化编辑工具,例如Unity商城中的「BehaviourDesigner」,甚至是虚幻引擎也自带此类编辑工具。而且它的设计逻辑并不复杂,其所利用的树状结构,很符合人的思考方式。接下来,我们会......
  • Unity builtin GUIStyle内置样式
    https://gist.github.com/bikrone/666bb26fb0d4468df12c890ecc6c512eusingUnityEditor;usingUnityEngine;publicsealedclassExampleClass:EditorWindow{privatestaticreadonlystring[]mList={"AboutWIndowLicenseLabel"......
  • Unity学习笔记--数据持久化XML文件(2)
    IXmlSerializable接口:使用该接口可以帮助处理不能被序列化和反序列化的特殊类得到处理,使特殊类继承IXmlSerializable接口,实现其中的读写方法,检测到读写方法被重写之后,便会按照自定义实现的接口来实现方法。usingSystem;usingSystem.IO;usingSystem.Runtime.InteropServi......
  • 如何拆解Unity 2022.3版本的AssetBundle
    1)如何拆解Unity2022.3版本的AssetBundle2)Unity2022LTS版本的稳定性3)关于AssetBundle禁用TypeTree之后的一些可序列化的问题这是第363篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答、社区帖子等技术知识点,助力大家更全面地掌握和学习。UWA社区主页:community.......
  • Unity2D中瓦片地图的创建与绘制教程
    Unity2D中瓦片地图的创建与绘制素材切割创建地图创建瓦片绘制地图瓦片调色板画笔拓展素材资源链接素材切割选中以下素材,以Tiles为例(素材链接在文章最下方)修改素材属性。将SpriteMode属性改为Multiple多张(不然切割不了);PixelsPerUnit改为16像素;FilterMode改为Point(nofilte......