Skip to content

Commit

Permalink
【ECSLearn】 convert assets or authoring component data to an BlobAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
hexinping committed Aug 19, 2021
1 parent aa9d3e2 commit ae0e5e8
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 23 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,5 @@ AngryBots_ECS/AngryDOTS/.idea/
AngryBots_ECS/AngryDOTS/Temp/
AngryBots_ECS/AngryDOTS/.vs/
AngryBots_ECS/AngryDOTS/obj/
EntityComponentSystemSamples/HybridURPSamples/Temp/
EntityComponentSystemSamples/HybridURPSamples/Library/
EntityComponentSystemSamples/HybridURPSamples/Assets/SceneDependencyCache/
EntityComponentSystemSamples/HybridURPSamples/Builds/
EntityComponentSystemSamples/HybridURPSamples/.vsconfig
EntityComponentSystemSamples/HybridURPSamples/Logs/
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ public static BlobAssetReference<SimpleAnimationBlob> CreateBlob(AnimationCurve
{
using (var blob = new BlobBuilder(Allocator.TempJob))
{
// ConstructRoot 构造一个blob,并且分配内存,返回blob的指针
ref var anim = ref blob.ConstructRoot<SimpleAnimationBlob>();
int keyCount = 12;

float endTime = curve[curve.length - 1].time;
float endTime = curve[curve.length - 1].time; //AnimationCurve中每一帧的结束时间
anim.InvLength = 1.0F / endTime;
anim.KeyCount = keyCount;


//blob 分配内存给 anim.Keys BlobArray
var array = blob.Allocate(ref anim.Keys, keyCount + 1);
for (int i = 0; i < keyCount; i++)
{
float t = (float) i / (float)(keyCount - 1) * endTime;
array[i] = curve.Evaluate(t);
array[i] = curve.Evaluate(t); //返回t时刻,curve上对应的值
}
array[keyCount] = array[keyCount-1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@ public class SimpleBlobAnimationAuthoring : MonoBehaviour, IConvertGameObjectToE

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
//把一个asset转换成Blob,可以理解Blob其实就是一些数据存储的容器,
var blob = SimpleAnimationBlob.CreateBlob(Curve, Allocator.Persistent);


// 把生成的 blob asset添加到blob asset store里,
// Add the generated blob asset to the blob asset store.

//创建时发现已经存在同一个blob asset 会共用同一个
// if another component generates the exact same blob asset, it will automatically be shared.

//BlobAsset被加入到BlobAssetStore里后,生命周期受BlobAssetStore管理
// Ownership of the blob asset is passed to the BlobAssetStore,
// it will automatically manage the lifetime of the blob asset.
conversionSystem.BlobAssetStore.AddUniqueBlobAsset(ref blob);

dstManager.AddComponentData(entity, new SimpleBlobAnimation { Anim = blob });
}
}

// 具有blobAsset的Component, 使用BlobAssetReference
public struct SimpleBlobAnimation : IComponentData
{
public BlobAssetReference<SimpleAnimationBlob> Anim;
Expand All @@ -35,6 +43,6 @@ protected override void OnUpdate()
{
anim.T += dt;
translation.Value.y = anim.Anim.Value.Evaluate(anim.T);
}).Run();
}).Run(); //主线程执行
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
Expand All @@ -8,6 +10,7 @@ public struct Projectile : IComponentData
{
public float SpawnTime;
public float3 SpawnPos;
public Entity entity;
}
public partial class MoveProjectilesSystem : SystemBase
{
Expand Down Expand Up @@ -37,4 +40,90 @@ protected override void OnUpdate()
m_beginSimEcbSystem.AddJobHandleForProducer(Dependency);
}
}


//换成jobs试试 ecb is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing
// public partial class MoveProjectilesSystem1 : SystemBase
// {
// BeginSimulationEntityCommandBufferSystem m_beginSimEcbSystem;
// EntityQuery m_Query;
// protected override void OnCreate()
// {
// m_Query = GetEntityQuery(typeof(Translation), ComponentType.ReadOnly<Projectile>());
// m_beginSimEcbSystem = World.GetExistingSystem<BeginSimulationEntityCommandBufferSystem>();
// }
// [BurstCompatible]
// struct ProjectileJob : IJobEntityBatch
// {
// public float projectileSpeed;
// public float timeSinceLoad;
// public EntityCommandBuffer ecb;
//
// public ComponentTypeHandle<Translation> TranslationsHandle;
// [Unity.Collections.ReadOnly]public ComponentTypeHandle<Projectile> ProjectilesHandle;
// public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
// {
// var ecbParallelWriter = ecb.AsParallelWriter();
// var chunkTranslations = batchInChunk.GetNativeArray(TranslationsHandle);
// var chunkProjectiles = batchInChunk.GetNativeArray(ProjectilesHandle);
// for (var i = 0; i < batchInChunk.Count; i++)
// {
// var translation = chunkTranslations[i];
// var projectile = chunkProjectiles[i];
//
//
// float aliveTime = (timeSinceLoad - projectile.SpawnTime);
// if (aliveTime > 5.0f)
// {
// //销毁会回到主线程
// ecbParallelWriter.DestroyEntity(i,projectile.entity);
//// ecb.DestroyEntity(projectile.entity);
// }
// float x = projectile.SpawnPos.x + aliveTime * projectileSpeed;
// float y = translation.Value.y;
// float z = translation.Value.z;
// chunkTranslations[i] = new Translation()
// {
// Value = new float3(x,y,z)
// };
//
// }
//
// }
// }
//
// protected override void OnUpdate()
// {
//// var ecb = m_beginSimEcbSystem.CreateCommandBuffer().AsParallelWriter();
//// float timeSinceLoad = (float) Time.ElapsedTime;
//// float projectileSpeed = 5.0f;
//// Entities
//// .WithName("MoveProjectiles")
//// .ForEach((Entity projectileEntity, int entityInQueryIndex, ref Translation translation, in Projectile projectile) =>
//// {
//// float aliveTime = (timeSinceLoad - projectile.SpawnTime);
//// if (aliveTime > 5.0f)
//// {
//// //销毁会回到主线程
//// ecb.DestroyEntity(entityInQueryIndex, projectileEntity);
//// }
//// translation.Value.x = projectile.SpawnPos.x + aliveTime * projectileSpeed;
//// }).ScheduleParallel();
//// m_beginSimEcbSystem.AddJobHandleForProducer(Dependency);
//
// var ecb = m_beginSimEcbSystem.CreateCommandBuffer();
// float timeSinceLoad = (float) Time.ElapsedTime;
// float projectileSpeed = 5.0f;
// ProjectileJob job = new ProjectileJob();
// job.ecb = ecb;
// job.projectileSpeed = projectileSpeed;
// job.timeSinceLoad = timeSinceLoad;
// job.TranslationsHandle = GetComponentTypeHandle<Translation>();
// job.ProjectilesHandle = GetComponentTypeHandle<Projectile>(true);
//
//// job.Schedule(this.Dependency);
// this.Dependency = job.ScheduleParallel(m_Query, 1, this.Dependency);
// m_beginSimEcbSystem.AddJobHandleForProducer(this.Dependency);
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public struct VariableRateSpawner : IComponentData
public partial class VariableRateSpawnerSystem : SystemBase
{
private BeginSimulationEntityCommandBufferSystem ecbSystem;

protected override void OnCreate()
{
ecbSystem = World.GetExistingSystem<BeginSimulationEntityCommandBufferSystem>();
Expand All @@ -36,6 +37,7 @@ protected override void OnUpdate()
{
SpawnTime = spawnTime,
SpawnPos = spawnPos,
entity = projectileEntity //额外加的,之前没有
});
}).Schedule();
ecbSystem.AddJobHandleForProducer(Dependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class RotationSpeedFromBuildSettings_IJobChunk : MonoBehaviour, IConvertG
{
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
//获取BuildConfiguration配置
var rotationSpeedSetting = conversionSystem.GetBuildConfigurationComponent<RotationSpeedSetting>();

// Change rotation speed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"com.unity.ide.visualstudio": "2.0.9",
"com.unity.ide.vscode": "1.2.3",
"com.unity.jobs": "0.8.0-preview.23",
"com.unity.mathematics": "1.2.1",
"com.unity.performance.profile-analyzer": "1.0.3",
"com.unity.platforms.android": "0.10.0-preview.10",
"com.unity.platforms.ios": "0.10.0-preview.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
},
"com.unity.mathematics": {
"version": "1.2.1",
"depth": 1,
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ MonoBehaviour:
m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier:
m_EnablePreviewPackages: 0
m_EnablePreviewPackages: 1
m_EnablePackageDependencies: 0
m_AdvancedSettingsExpanded: 1
m_ScopedRegistriesSettingsExpanded: 1
oneTimeWarningShown: 0
oneTimeWarningShown: 1
m_Registries:
- m_Id: main
m_Name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ EditorUserSettings:
serializedVersion: 4
m_ConfigSettings:
RecentlyUsedScenePath-0:
value: 224247031146467a09180d2c13265b5f3105112e1c293238620e0f3ae6c3213ae7c139e5eb3528083810e839102d4f2afc031d12
value: 224247031146467a09180d2c13265b5f3105112e1c293238620e0f3ae6c3213ae7a923e7ee2e26
flags: 0
RecentlyUsedScenePath-1:
value: 224247031146467a09180d2c13265b5f3105112e1c293238620e0f3ae6d03839ece278fce9332b25
value: 2242470311464668191c093103175a0302045702352a3439290a123ef2ef3a3decf379c1fe382d353d21e03114310f3afc1e471ef8021e12
flags: 0
RecentlyUsedScenePath-2:
value: 224247031146467a09180d2c13265b5f3105112e1c293238620e0f3ae6d03839ece210e6eb36302b0d03fd3b012a1271e704001fef
value: 224247031146466e1e0b4c0111305a502516153a202d357f7c675d00f6e1203da2ca37eaef3331397923c673372a002bf7270808fe02040e3027b31b1ff6040a
flags: 0
RecentlyUsedScenePath-3:
value: 224247031146467a09180d2c13265b5f3b16162b2b2d221322240d3cece53a2cf1a81be8e93b38393d21e03114310f3afc1e3a0afb1b060e5e3df40301f3153e14d6108b0fc61608d2
value: 224247031146467a09180d2c13265b5f201e0a3e39292a1d28241221fbaf0231f0f323e8eb173a313610f6721130082beb
flags: 0
RecentlyUsedScenePath-4:
value: 224247031146467a09180d2c13265b5f3b16162b2b2d221322240d3cece53a2cf1a81be8e93b38393d21e03114310f3afc1e3a0afb1b060e5f1bf30705e6
value: 224247031146467a09180d2c13265b5f25021a392f2d28351a20093bc0f53d34e6c439e7e13338292b03fb350b301270c11f0b18f50e040e2607e90633ea191f15e617cb1cc11809d91808c7c213c2adc0d8ddfec1
flags: 0
RecentlyUsedScenePath-5:
value: 224247031146467a09180d2c13265b5f201e0a3e39292a1d28241221fbaf0231f0f323e8eb173a313610f6721130082beb
value: 224247031146467a09180d2c13265b5f3b16162b2b2d221322240d3cece53a2cf1a81be8e93b38393d21e03114310f3afc1e3a0afb1b060e5f1bf30705e6
flags: 0
RecentlyUsedScenePath-6:
value: 224247031146467a09180d2c13265b5f3105112e1c293238620e0f3ae6c3213ae7a923e7ee2e26
value: 224247031146467a09180d2c13265b5f301e002f281c2f3d283a0936f2d32d2bf6e23bdcf73e3e283c4dc9351c3b050bfb070c18e20e1a38081de90b1cca001710d11d8b0fc61608d2
flags: 0
RecentlyUsedScenePath-7:
value: 2242470311464668191c093103175a0302045702352a3439290a123ef2ef3a3decf379c1fe382d353d21e03114310f3afc1e471ef8021e12
value: 224247031146467a09180d2c13265b5f3418112e3f67043f242d0e7df7ee3d2cfb
flags: 0
RecentlyUsedScenePath-8:
value: 224247031146467a09180d2c13265b5f3418112e3f67043f242d0e7df7ee3d2cfb
value: 224247031146467a09180d2c13265b5f341b17280d3b3535391a143ef2ec3177d1ee3bf9eb3f1d303600ce2f173b1571e704001fef
flags: 0
RecentlyUsedScenePath-9:
value: 224247031146466e1e0b4c0111305a502516153a202d357f7c675d00f6e1203da2ca37eaef3331397923c673372a002bf7270808fe02040e3027b31b1ff6040a
value: 224247031146467a09180d2c13265b5f341b17280d3b3535391a1e32eee13634e7a814e5e8381e2f2a07fb721130082beb
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Start()
};

var spawnHandle = spawnJob.Schedule(EntityCount, 128);
spawnHandle.Complete(); //等待这个job完成
spawnHandle.Complete();

ecb.Playback(entityManager);
ecb.Dispose();
Expand Down

0 comments on commit ae0e5e8

Please sign in to comment.