2020-12-16 11:59:53 +01:00
|
|
|
|
using Unity.Entities;
|
|
|
|
|
using Unity.Mathematics;
|
2020-12-27 18:15:58 +01:00
|
|
|
|
using Unity.Collections;
|
2020-12-16 11:59:53 +01:00
|
|
|
|
using TAO.VertexAnimation;
|
|
|
|
|
|
|
|
|
|
public class SampleAnimationSystemsAuthoring : UnityEngine.MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[UnityEngine.SerializeField]
|
|
|
|
|
private SampleSystem sampleSystem = SampleSystem.PlayRandomAnimation;
|
|
|
|
|
|
2020-12-27 18:15:58 +01:00
|
|
|
|
[UnityEngine.SerializeField]
|
|
|
|
|
private string animationName = "Character1Shoot";
|
|
|
|
|
|
2020-12-16 11:59:53 +01:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
switch (sampleSystem)
|
|
|
|
|
{
|
|
|
|
|
case SampleSystem.PlayRandomAnimation:
|
|
|
|
|
{
|
|
|
|
|
var system = World.DefaultGameObjectInjectionWorld.GetExistingSystem<PlayRandomAnimationSystem>();
|
|
|
|
|
system.Enabled = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SampleSystem.PlayAnimationByName:
|
|
|
|
|
{
|
|
|
|
|
var system = World.DefaultGameObjectInjectionWorld.GetExistingSystem<PlayAnimationByNameSystem>();
|
2020-12-27 18:15:58 +01:00
|
|
|
|
system.animationName = animationName;
|
2020-12-16 11:59:53 +01:00
|
|
|
|
system.Enabled = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum SampleSystem
|
|
|
|
|
{
|
|
|
|
|
PlayRandomAnimation,
|
|
|
|
|
PlayAnimationByName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Example system to update animation parameters.
|
|
|
|
|
[UpdateBefore(typeof(VA_AnimatorSystem))]
|
|
|
|
|
public class PlayRandomAnimationSystem : SystemBase
|
|
|
|
|
{
|
|
|
|
|
protected override void OnCreate()
|
|
|
|
|
{
|
|
|
|
|
base.OnCreate();
|
|
|
|
|
|
|
|
|
|
Enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnUpdate()
|
|
|
|
|
{
|
|
|
|
|
Random random = Random.CreateFromIndex((uint)UnityEngine.Time.time);
|
|
|
|
|
float deltaTime = UnityEngine.Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
Entities.ForEach((ref VA_AnimatorComponent ac) =>
|
|
|
|
|
{
|
|
|
|
|
// Get the animation lib data.
|
|
|
|
|
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
|
|
|
|
|
|
|
|
|
|
// 'Play' the actual animation.
|
|
|
|
|
ac.animationTime += deltaTime;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// New random animation.
|
|
|
|
|
ac.animationIndex = random.NextInt(0, animationsRef.animations.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).ScheduleParallel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Example system to set the animation by name.
|
|
|
|
|
[UpdateBefore(typeof(VA_AnimatorSystem))]
|
|
|
|
|
public class PlayAnimationByNameSystem : SystemBase
|
|
|
|
|
{
|
2020-12-27 18:15:58 +01:00
|
|
|
|
public FixedString32 animationName;
|
|
|
|
|
|
2020-12-16 11:59:53 +01:00
|
|
|
|
protected override void OnCreate()
|
|
|
|
|
{
|
|
|
|
|
base.OnCreate();
|
|
|
|
|
|
|
|
|
|
Enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnUpdate()
|
|
|
|
|
{
|
|
|
|
|
float deltaTime = UnityEngine.Time.deltaTime;
|
2020-12-27 18:15:58 +01:00
|
|
|
|
FixedString32 an = animationName;
|
2020-12-16 11:59:53 +01:00
|
|
|
|
|
|
|
|
|
Entities.ForEach((Entity entity, ref VA_AnimatorComponent ac) =>
|
|
|
|
|
{
|
|
|
|
|
// Get the animation lib data.
|
|
|
|
|
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
|
|
|
|
|
|
|
|
|
|
// Set the animation index on the AnimatorComponent to play this animation.
|
2020-12-27 18:15:58 +01:00
|
|
|
|
ac.animationIndex = VA_AnimationLibraryUtils.GetAnimation(ref animationsRef, an);
|
2020-12-16 11:59:53 +01:00
|
|
|
|
|
|
|
|
|
// 'Play' the actual animation.
|
|
|
|
|
ac.animationTime += deltaTime;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}).ScheduleParallel();
|
|
|
|
|
}
|
|
|
|
|
}
|