0

I'm making my own animation library I want to handle all animation logic within a single Update function.

Here is the code

[RequireComponent (typeof(RectTransform))]
public class UIMotion : MonoBehaviour
{

    public enum MoveDirType
    {
        XDir,
        YDir
    }

    [SerializeField]   private   Spring   spring;
    [SerializeField]   private   float    target;
    [SerializeField]   private   MoveDirType  moveDir;
    
    private   Vector2         initialPos;
    private   RectTransform   rect;
    
    private void Awake  ()
    {
        rect       =  GetComponent <RectTransform> ();
        initialPos =  rect.anchoredPosition;
    }

    void Update ()
    {
        float    springValue   =  spring.GetSpringValue (target, Time.smoothDeltaTime);
        switch (moveDir)
        {
            case MoveDirType.XDir:
              rect.anchoredPosition   =  new Vector2 (springValue, rect.anchoredPosition.y);
            break;
            case MoveDirType.YDir:
               rect.anchoredPosition  =  new Vector2 (rect.anchoredPosition.x, springValue);
            break;
        }
    }
    
    void OnDisable()
    {
        rect.anchoredPosition = initialPos;   
    }
}

As you can see in the code. Every UIMotion Component will have its own Update function after animation it will be unless to run Update code that's why I took this approach but there are some problems in this approach.

My Approach

public class AnimationThread : MonoBehaviour
{
   public List <Action> animationActions; // action = UIMotion Update Function
   
   private void Update ()
   {
      foreach (Action action in animationActions) action.Invoke ();
   }
}

This code is work for starting animation but I have another scenario also like playing animation on a panel open or on button click.

Note: You might suggest just disable the component when the animation is finished but in my case, I'm using springs and I'm unable to figure out where exactly the animation is ending.

How can I handle these all animations from one update function OR or is there any way to find where the animation is ending

Spring Code:

public class Spring 
{
    public   float   equilibriumPosition;
    public   float   dampingRatio;
    public   float   angularFrequency;

    internal float   value;
    internal float   velocity;  

    public Spring () {}

    public float GetSpringValue (float targetValue, float deltaTime)
    {
        SpringMotion.CalcDampedSimpleHarmonicMotion(
            ref value, 
            ref velocity,
            targetValue,
            deltaTime,
            angularFrequency,
            dampingRatio
        );

        return value;
    }
}

Resources

Reference

Full Source Code

Article

Exactly Want to achieve something like this


Thanks in Advance

  • I don't quite follow you ... what you currently have is a separate `Update` method for each instance of your components ... you ask us how to reinvent an entire library like DoTween ...? – derHugo Oct 02 '22 at 06:44
  • @derHugo I just wondering how much cost each update function will take. `In Short` I want to optimize it –  Oct 02 '22 at 06:45

0 Answers0