Search Unity

Tweening

Discussion in 'Scripting' started by unity_5fpsmegasupergiperprogramer, Sep 24, 2019.

  1. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    Hi, everybody. Write your Tweening with the right features.

    And I have a question in implementation.

    There are two options:
    1. Implement abstract class and create Tween from it.
    2. Create an ITween interface and create structures with this interface.

    As a result, the second option is faster in any case, because only structures are created, but when there is a lot more, it will be necessary for each structure to implement common functions.

    In the first case, when there is a minimum, everything is implemented in abstract class, but there are classes and nesting, and it is slower, because each time it will create classes.

    How to be? On how many afraid of the first option?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I don't know what you are really saying/asking honestly. But for tweening, we tend to use LeanTween. Another popular one is Dotween. Sure, we could do our own, but unless we'd write something that was feature rich and provided much better performance, we don't see a reason to.

    We have other things to focus our programming time on. No reason to reinvent the wheel with so many tweening options already available.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    I can think of more options.

    If by 'structures' you mean 'structs'. No it won't be much faster if you're attempting to save heap allocations by avoiding classes. Since because you're using an interface, when you cast the struct to said interface... it gets allocated on the heap.

    Have you profiled this? Do you know it'll be significantly slower?

    I use option 1 as you can see here:
    https://github.com/lordofduct/spacepuppy-unity-framework-3.0/tree/master/SPTween/Tween

    And the overhead hasn't really been significant in my usage.

    I bet if I created hundreds of tweens per frame it would start having issues. But I'm not.

    Anyways, your struct approach doesn't necessarily solve the bottleneck you're considering.

    Really at the end of the day the biggest bottleneck I've found is the 'getter/setter' reflection of the target to be tweened. Being able to dynamically access members. I've seen some engines implement it via delegates instead of reflection, which actually increases the object creation since delegates are again allocated on the heap.
     
  4. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    Thank you for the answer.

    Yesterday I ran a lot of tests. The option with "struct" does not fit, as it is difficult to expand.

    I only have one problem, I add Tween classes to the List, which then call Update. (that one main class would be called from children).

    And it is the "List.Add" that receives the memory allocation (~ 56 byte)

    Did you bypass this somehow? I tried to use "delegate + -" instead of List, it's even worse, first of all it takes more memory allocation, secondly it takes more CPU time.