Are there known possibilities to create reference to blob asset inside other blob asset? For example, I have bottom tier data blobs that can be reused in some higher level structures? As far as I understand BlobPtr is method to point to data in same blob asset. Thank you in advance
Do you mean something like that?
Districts has sub reference Modifiers.
public BlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>> Districts;
public readonly struct DistrictMemory
{
public readonly FixedString64 Tag;
public readonly FixedString128 Name;
public readonly uint BuildTime;
public readonly uint BuildPointCost;
public readonly uint Cost;
public readonly BlobAssetReference<BlobArray<FixedString64>> Modifiers;
public DistrictMemory(FixedString64 tag, FixedString128 name,
uint buildTime, uint buildPointCost, uint cost, BlobAssetReference<BlobArray<FixedString64>> modifiers)
{
Tag = tag;
Name = name;
BuildTime = buildTime;
BuildPointCost = buildPointCost;
Cost = cost;
Modifiers = modifiers;
}
}
If this is what you are looking here are snippets how I am building it.
private BlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>> LoadDistricts(DataContainer dataContainer)
{
var source = new NativeHashMap<FixedString64, DistrictMemory>(4, Allocator.Temp);
for (int index = 0; index < dataContainer.Jobs.Count; index++)
{
var district = dataContainer.Districts[index];
source.Add(district.Tag, new DistrictMemory(
district.Tag,
district.Name,
district.BuildTime,
district.BuildPointCost,
district.Cost,
AddModifiers(district.Modifiers.ToArray())));
}
BlobBuilder builder = new BlobBuilder(Allocator.Temp);
ref var root = ref builder.ConstructRoot<BlobHashMap<FixedString64, DistrictMemory>>();
builder.ConstructHashMap(ref root, ref source);
var blobMapRef = builder.CreateBlobAssetReference<BlobHashMap<FixedString64, DistrictMemory>>(Allocator.Persistent);
return blobMapRef;
}
private BlobAssetReference<BlobArray<FixedString64>> AddModifiers(params string[] tags)
{
using (var builder = new BlobBuilder(Allocator.Temp)) {
ref BlobArray<FixedString64> root = ref builder.ConstructRoot<BlobArray<FixedString64>>();
var nodearray = builder.Allocate(ref root, tags.Length);
for (var index = 0; index < tags.Length; index++)
{
var tag = tags[index];
nodearray[index] = tag;
}
return builder.CreateBlobAssetReference<BlobArray<FixedString64>>(Allocator.Persistent);
}
}
Thank you for quick response. Yes, your case is similar to mine. But I made tiny example that failed for me:
public struct TestStruct
{
public BlobAssetReference<float> blobAssetFloat;
}
....
var bb = new BlobBuilder(Allocator.Temp);
ref var ts = ref bb.ConstructRoot<TestStruct>();
And I have a compile error:
error ConstructBlobWithRefTypeViolation: You may not build a type TestStruct with Construct as TestStruct.blobAssetFloat.m_data.m_Ptr is a reference or pointer. Only non-reference types are allowed in Blobs.
I use Entities 0.17.0-preview.42 package with Unity 2021.2.0b16. Yes I know about "2020 only supported version, but everything went smooth for now. What version are you using?
Found solution: [Entities 0.17 changelog]( Entities 0.17 changelog page-3#post-6791726). Seems Entities package issue
Sorry about that I forgot that I had to change the same thing to get it working.
And I am using 2021.2 too. UI toolkit is so much better on the newer version.
You can have BlobPtr pointing to any data in the same blob. But right now we do not support blobs having ptrs to other independent blobs. You have to handle that yourself. Eg. store an index that in a BlobArray that lives in another Blob.
Depending on what you need, you may also need a way of looking up the other referenced blob asset. Some ID or index or whatever might work for you. It just totally depends on the exact problem you are trying to solve and how your blob is accessed at runtime.
Of course, in many situations, just having a bigger blob that basically has both of your blobs combined into one might be the better approach, then you can just setup use BlobPtr. If that is an option, I would expect that to be both simpler in runtime code to manage& faster.
Thank you very mush for responses. Joachim, indexing into other blob array is actually viable solution, I didn’t think that way, thank you. Making self contained blob asset is always an option, but I try study every aspect of Unity ECS, and wanted to solve my tasks the “proper and logical way” first. Besides such interblob referenses are logical requirement for complex data structures.