Example2.

Example2
- Spawn system
- Character animation system
This commit is contained in:
max
2021-04-05 22:20:35 +02:00
parent d6f7cf3212
commit 7db5d33c8e
35 changed files with 4914 additions and 0 deletions

View File

@ -0,0 +1,29 @@
using Unity.Entities;
using Unity.Collections;
using TAO.VertexAnimation;
public class CharacterAnimationComponentAuthoring : UnityEngine.MonoBehaviour, IConvertGameObjectToEntity
{
[UnityEngine.SerializeField]
private VA_Animation idle;
[UnityEngine.SerializeField]
private VA_Animation run;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
CharacterAnimationComponent sampleAnimationComponent = new CharacterAnimationComponent
{
curAnimation = idle.GetName(),
idle = idle.GetName(),
run = run.GetName()
};
dstManager.AddComponentData(entity, sampleAnimationComponent);
}
}
public struct CharacterAnimationComponent : IComponentData
{
public FixedString64 curAnimation;
public FixedString64 idle;
public FixedString64 run;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c23c000b7fd45d24082124648f08658f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,41 @@
using Unity.Entities;
using Unity.Mathematics;
using TAO.VertexAnimation;
public class CharacterAnimationSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = UnityEngine.Time.deltaTime;
Random random = Random.CreateFromIndex((uint)UnityEngine.Time.time);
Entities.ForEach((Entity entity, ref VA_AnimatorComponent ac, ref CharacterAnimationComponent cac) =>
{
// Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
// Set the animation index on the AnimatorComponent to play this animation.
ac.animationIndex = VA_AnimationLibraryUtils.GetAnimation(ref animationsRef, cac.curAnimation);
// 'Play' the actual animation.
ac.animationTime += deltaTime * animationsRef.animations[ac.animationIndex].frameTime;
if (ac.animationTime > animationsRef.animations[ac.animationIndex].duration)
{
// Set time. Using the difference to smoothen out animations when looping.
//ac.animationTime -= animationsRef.animations[ac.animationIndex].duration;
ac.animationTime = 0;
// Select new animation to play.
if (random.NextBool())
{
cac.curAnimation = cac.idle;
}
else
{
cac.curAnimation = cac.run;
}
}
}).ScheduleParallel();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7c6af4647123d79418d8814e68f4f9d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,48 @@
using Unity.Entities;
[UnityEngine.RequireComponent(typeof(ConvertToEntity))]
public class SpawnerComponentAuthoring : UnityEngine.MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
{
public UnityEngine.GameObject prefab;
public int countX = 1;
public int countY = 1;
public int countZ = 1;
public int spaceX = 1;
public int spaceY = 1;
public int spaceZ = 1;
// Referenced prefabs have to be declared so that the conversion system knows about them ahead of time
public void DeclareReferencedPrefabs(System.Collections.Generic.List<UnityEngine.GameObject> gameObjects)
{
gameObjects.Add(prefab);
}
// Lets you convert the editor data representation to the entity optimal runtime representation
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var spawnerData = new SpawnerComponent
{
// The referenced prefab will be converted due to DeclareReferencedPrefabs.
// So here we simply map the game object to an entity reference to that prefab.
entity = conversionSystem.GetPrimaryEntity(prefab),
countX = countX,
countY = countY,
countZ = countZ,
spaceX = spaceX,
spaceY = spaceY,
spaceZ = spaceZ
};
dstManager.AddComponentData(entity, spawnerData);
}
}
public struct SpawnerComponent : IComponentData
{
public Entity entity;
public int countX;
public int countY;
public int countZ;
public int spaceX;
public int spaceY;
public int spaceZ;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 549527d13217f39419822ce998b347ca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,42 @@
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
public class SpawnerSystem : SystemBase
{
private EntityCommandBufferSystem enityCommandBufferSystem;
protected override void OnCreate()
{
enityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var commandBuffer = enityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter();
Entities.ForEach((Entity entity, int entityInQueryIndex, ref SpawnerComponent spawner, ref LocalToWorld location) =>
{
for (var x = 0; x < spawner.countX; x++)
{
for (var y = 0; y < spawner.countY; y++)
{
for (int z = 0; z < spawner.countZ; z++)
{
var instance = commandBuffer.Instantiate(entityInQueryIndex, spawner.entity);
// Place the instantiated in a grid with some noise
var position = math.transform(location.Value, new float3(x * spawner.spaceX, y * spawner.spaceY, z * spawner.spaceZ));
commandBuffer.SetComponent(entityInQueryIndex, instance, new Translation { Value = position });
}
}
}
commandBuffer.DestroyEntity(entityInQueryIndex, entity);
})
.WithName("SpawnerSystem")
.ScheduleParallel();
enityCommandBufferSystem.AddJobHandleForProducer(Dependency);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1d39fc589a5958b43a7f68a8e2c9b5f1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: