Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    Dismiss Notice

Question How to Control Animation In Tiny?

Discussion in 'Project Tiny' started by b920687vd, Sep 29, 2020.

  1. b920687vd

    b920687vd

    Joined:
    Feb 20, 2019
    Posts:
    4
    Hi friends!
    I'm using the prefab with Animator in the normal Unity project, and I want to use it in Tiny mode.
    I have import the prefab ( and other resources ) into the Tiny Project and add TinyAnimationAuthoring on GameObjects which have Animator. And now the first Animation Clip can be played successfully.
    Now I have two problems:
    1. How to change the playing Animation Clip by CODE? I cant get the TinyAnimationAuthoring Component in System code, is there any Sample? ( The sample TinyFactory don't have any code to control it)
    2. Can I get some event when the Animation complete? I want to run some func when a Animation is over.

    Thanks for help!
     
  2. todans

    todans

    Joined:
    Mar 4, 2014
    Posts:
    12
    You need TinyAnimation class. It has a number of methods for getting/setting animation properties at runtime. In fact it's a wrapper that changes components, related to animations, so you can find its source code and see in details, what exactly it's doing.
    Also it has strange behavior for method SelectClipAtIndex() causing some animation blinking. I wrote my own realization of it that feets needs of my project (it repeats realization from Unity's class except one commented string that produces animation blinking):
    Code (CSharp):
    1. public static void SelectClipAtIndex(World world, Entity entity, int index)
    2.         {
    3.             /////////////Stop() method////////////////////////
    4.             var currentClip = world.EntityManager.GetComponentData<TinyAnimationPlayer>(entity).CurrentClip;
    5.             var buffer = world.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>().CreateCommandBuffer();
    6.  
    7.             if (world.EntityManager.HasComponent<UpdateAnimationTimeTag>(currentClip))
    8.                 buffer.RemoveComponent<UpdateAnimationTimeTag>(currentClip);
    9.  
    10.             if (world.EntityManager.HasComponent<ApplyAnimationResultTag>(currentClip))
    11.                 buffer.RemoveComponent<ApplyAnimationResultTag>(currentClip);
    12.             //world.EntityManager.SetComponentData(currentClip, new TinyAnimationTime { Value = 0f, InternalWorkTime = 0f});
    13.             ////////////////////////////////////////////////////
    14.             var clipsBuffer = world.EntityManager.GetBuffer<TinyAnimationClipRef>(entity);
    15.             var selectedClip = clipsBuffer[index].Value;
    16.             world.EntityManager.SetComponentData(entity, new TinyAnimationPlayer { CurrentClip = selectedClip, CurrentIndex = index });
    17.             world.EntityManager.SetComponentData(selectedClip, new TinyAnimationTime { Value = 0f, InternalWorkTime = 0f});
    18.          
    19.             currentClip = selectedClip;
    20.  
    21.             ////////////////Play() method/////////////////////
    22.             // Are we playing a stopped clip?
    23.             if (!world.EntityManager.HasComponent<UpdateAnimationTimeTag>(currentClip) &&
    24.                 !world.EntityManager.HasComponent<ApplyAnimationResultTag>(currentClip))
    25.             {
    26.                 var playbackInfo = world.EntityManager.GetComponentData<TinyAnimationPlaybackInfo>(currentClip);
    27.  
    28.                 // Are we updating a cyclical wrap mode?
    29.                 if (playbackInfo.WrapMode == WrapMode.Loop || playbackInfo.WrapMode == WrapMode.PingPong)
    30.                 {
    31.                     var startOffset = playbackInfo.CycleOffset * playbackInfo.Duration;
    32.                     world.EntityManager.SetComponentData(currentClip, new TinyAnimationTime { Value = startOffset, InternalWorkTime = startOffset});
    33.                 }
    34.             }
    35.          
    36.             buffer.AddComponent<UpdateAnimationTimeTag>(currentClip);
    37.             buffer.AddComponent<ApplyAnimationResultTag>(currentClip);
    38.             ////////////////////////////////////////////////
    39.         }
    And this is an example of using some methods to control animation:
    Code (CSharp):
    1. Entities
    2.                 .WithAll<MobTag>()
    3.                 .ForEach((Entity entity) =>
    4.                 {
    5.                     var curClip = TinyAnimation.GetCurrentClipIndex(World, entity);
    6.                     if (curClip != animSettings.Idle)
    7.                     {
    8.                         var time = TinyAnimation.GetTime(World, entity);
    9.                         var duration = TinyAnimation.GetDurationAtIndex(World, entity, curClip);
    10.                         if (math.abs(duration - time) <= 0.01f || (curClip == animSettings.Death && time > 0.1f))
    11.                         {
    12.                             CustomTinyAnimation.SelectClipAtIndex(World, entity, animSettings.Idle);
    13.                         }
    14.                     }
    15.                 })
    16.                 .WithoutBurst().Run();
     
    BaspooGameDev likes this.
  3. b920687vd

    b920687vd

    Joined:
    Feb 20, 2019
    Posts:
    4
    Thanks a lot! I'll have a try
     
  4. b920687vd

    b920687vd

    Joined:
    Feb 20, 2019
    Posts:
    4
    A new question: Can I control the playing speed of Animation?
     
  5. todans

    todans

    Joined:
    Mar 4, 2014
    Posts:
    12
    I can only recommend to inspect components, that are added at runtime to entities, related to animations, and check their properties and realization. I use this system for such purpose:
    Code (CSharp):
    1.  
    2. public struct LogComponentsTag: IComponentData { }
    3.  
    4. public class LogComponentsSystem: SystemBase
    5.     {
    6.         private EndSimulationEntityCommandBufferSystem _ecbSystem;
    7.  
    8.         protected override void OnStartRunning()
    9.         {
    10.             _ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    11.             base.OnStartRunning();
    12.         }
    13.  
    14.         protected override void OnUpdate()
    15.         {
    16.             var cmdBuffer = _ecbSystem.CreateCommandBuffer();
    17.             Entities
    18.                 .WithAll<LogComponentsTag>()
    19.                 .ForEach((Entity entity, int entityInQueryIndex) =>
    20.                 {
    21.                     LogEntityComponents(entity);
    22.                     //EntityManager.RemoveComponent<LogComponentsTag>(entity);
    23.                     cmdBuffer.RemoveComponent<LogComponentsTag>(entity);
    24.                 })
    25.                 .WithoutBurst()
    26.                 .Run();
    27.         }
    28.  
    29.         private void LogEntityComponents(Entity entity)
    30.         {
    31.             var chunk = EntityManager.GetChunk(entity);
    32.             var componentTypesArray = chunk.Archetype.GetComponentTypes();
    33.                
    34.             Debug.Log($"Start of entity components");
    35.             for (var i = 0; i < componentTypesArray.Length; i++)
    36.             {
    37.                 var componentType = componentTypesArray[i];
    38.                 var nm = TypeManager.GetTypeInfo(componentType.TypeIndex).Debug.TypeName;
    39.                 Debug.Log(nm);
    40.             }
    41.             Debug.Log($"End of entity components");
    42.         }
    43.     }
     
  6. b920687vd

    b920687vd

    Joined:
    Feb 20, 2019
    Posts:
    4
    Thanks very much
     
  7. ExitCodeZero

    ExitCodeZero

    Joined:
    Dec 9, 2017
    Posts:
    1
    Hi,

    I just added an image sequence animation (drag & drop group of sprites) and added the TinyAnimationAuthoring. the animation isn't playing, is there anything else should I add?