using Unity.Animation; using Unity.Entities; using Unity.Mathematics; using UnityEngine; #if UNITY_EDITOR using UnityEditor.Animations; #endif // We currently do not support blobassets referencing other blobassets. This helper class can be used to store blendspace data on entity // and create blendspace blobasset at entity initialization public class BlendTreeEntityStoreHelper { public struct BlendTree1DData : IComponentData { public StringHash BlendParameter; } public struct BlendTree1DMotionData : IBufferElementData { public float MotionThreshold; public float MotionSpeed; public BlobAssetReference Motion; public MotionType MotionType; } public struct BlendTree2DData : IComponentData { public StringHash BlendParam; public StringHash BlendParamY; } public struct BlendTree2DMotionData : IBufferElementData { public float2 MotionPosition; public float MotionSpeed; public BlobAssetReference Motion; public MotionType MotionType; } #if UNITY_EDITOR public static void AddBlendTree1DComponents(EntityManager entityManager, Entity entity, BlendTree blendTree) { entityManager.AddComponentData(entity, new BlendTree1DData { BlendParameter = new StringHash(blendTree.blendParameter) }); var buffer = entityManager.AddBuffer(entity); for(int i=0;i(entity); for (int i = 0; i < blendTree.children.Length; i++) { blendSpaceEntries.Add(new BlendTree2DMotionData { MotionPosition = blendTree.children[i].position, MotionSpeed = blendTree.children[i].timeScale, Motion = UnityEditor.Animations.BlendTreeConvertHelper.Convert(blendTree.children[i].motion), MotionType = UnityEditor.Animations.BlendTreeConvertHelper.GetMotionType(blendTree.children[i].motion), }); } } #endif public static BlobAssetReference CreateBlendTree1DFromComponents(EntityManager entityManager, Entity entity) { var data = entityManager.GetComponentData(entity); var motionData = entityManager.GetBuffer(entity); var targetMotionData = new Unity.Animation.BlendTree1DMotionData[motionData.Length]; for(int i=0;i CreateBlendTree2DFromComponents(EntityManager entityManager, Entity entity) { // Create blendspace var blendSpaceData = entityManager.GetComponentData(entity); var blendSpaceEntries = entityManager.GetBuffer(entity); var blendTree2DMotionData = new Unity.Animation.BlendTree2DMotionData[blendSpaceEntries.Length]; for(int i=0;i Convert(UnityEngine.Motion motion) { var animationClip = motion as AnimationClip; // var blendTree = motion as BlendTree; // // if (blendTree != null) // return Convert(blendTree); // else if( animationClip != null) if( animationClip != null) { var clip = ClipBuilder.AnimationClipToDenseClip(animationClip); return clip; } else throw new System.ArgumentException($"Selected Motion type is not supported."); } public static MotionType GetMotionType(UnityEngine.Motion motion) { var blendTree = motion as BlendTree; if (blendTree != null) { return blendTree.blendType == BlendTreeType.Simple1D ? MotionType.BlendTree1D : MotionType.BlendTree2DSimpleDirectionnal; } else return MotionType.Clip; } } } #endif