Search Unity

[Released] C# Animator

Discussion in 'Assets and Asset Store' started by CSharpAnimator, Feb 8, 2019.

  1. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Hey guys, I've just released the first version of C# Animator to the asset store!

    Asset Store Link

    I developed C# animator while working on a relatively large commercial game a couple of years ago. The problem I was trying to solve was designing and maintaining large animator assets. I found some simple things missing from the animator window, such as copying and pasting state transitions, or the ability to transition from many states to another without duplicating the same transition multiple times.

    I solved this problem by writing what I think is a nice C# interface for generating animator assets:

    Code (CSharp):
    1.  
    2.         ParameterData speed = FloatParam("Speed", 1.0f);
    3.         ParameterData jumpTrigger = TriggerParam("JumpTrigger");
    4.      
    5.         Animator
    6.         (
    7.             Graph()
    8.             [
    9.                 Layer("Base").DefaultState("Idle")
    10.                 [
    11.                     State("Idle", idleMotion),
    12.                     State("Run", runMotion),
    13.                     State("Jump", jumpMotion)
    14.                 ]
    15.             ],
    16.  
    17.             Transitions()
    18.             [
    19.                 Transition().SourceMultiple("Idle", "Run").Destination("Jump")
    20.                 [
    21.                     Trigger(jumpTrigger)
    22.                 ],
    23.                 Transition().Source("Jump").Destination("Idle").ExitTime(0.9f),
    24.  
    25.                 Transition().Source("Idle").Destination("Run")
    26.                 [
    27.                     Float(speed, speedForRun, ConditionMode.Greater)
    28.                 ],
    29.                 Transition().Source("Run").Destination("Idle")
    30.                 [
    31.                     Float(speed, speedForRun, ConditionMode.Less)
    32.                 ]
    33.             ]
    34.         );
    Using C# to generate animators, and more generally, using text, creating complex animation state machines has become a non-issue for me. States and transitions can be easily moved, copied, reference, and mass generated. Also, given that this code is part of your game project, the animator can directly reference constants used for gameplay. For example, your character controller may have a list of states described as an enum. Using this asset, you can reference the same state enum used to drive your gameplay logic to determine your transition conditions, meaning the animator and gameplay logic will never get out of sync!

    A big goal of this tool is to be as easy as possible to integrate. The bare minimum for the tool to function is to import "CSharpAnimator.cs". Optionally(recommended), you can import "CSharpAnimator_Class.cs".

    In order to create an animator generator class, you simply need to derive from either "CSharpAnimator" or "CSharpAnimator_ClassBase". deriving from CSharpAnimator requires the class to have it's own file, where the filename matches the class name(because it works like a scriptable object), deriving from CSharpAnimator_ClassBase will not have this restriction.

    Once the class is made in C#, the animator asset can be created through the Assets/Create menu. Then just press the generate button in the animator's inspector.

    Here's a small example of how this works:


    Complete example code with comments:
    Code Examples
    Introduction Document


    C# Animator also comes with full source code.
     
    Last edited: Mar 30, 2019
    XCPU, JBR-games, StevenPicard and 5 others like this.
  2. 13Flo

    13Flo

    Joined:
    Jun 9, 2015
    Posts:
    87
    This looks amazing. I will definitely give it a try next time I need to deal with Animator!
     
    CSharpAnimator likes this.
  3. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Thank you!

    I'm curious though, is there a way you typically avoid dealing with the animator in Unity when you need to animate things?
     
  4. 13Flo

    13Flo

    Joined:
    Jun 9, 2015
    Posts:
    87
    There's not! I mean at least I don't know any other way :)
    But now I do thanks to you!!
     
    CSharpAnimator likes this.
  5. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    543
    Sorry i havnt looked into this to far yet, but does this allow a person to build the animator through script ?
     
  6. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Yep. You write a C# script that describes the graph and transitions. Then you can press a button on the animator's inspector to generate the animator asset.
     
    JBR-games likes this.
  7. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    543
    Can you use this to update or change an existing animator?
     
  8. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    You can regenerate the animator any time you change your script to update it. But you can't take an animator made using the animator window and work from that. If you have an animator that you made using the animator window, you'll have to rewrite it in C# Animator and then generate it to start working on it in C#.

    EDIT: Hope this is making sense. Let me know if it doesn't. Overall the general work flow is:

    Write C# code -> Generate asset -> Update C# code -> Regenerate asset -> etc.
    Every time that you generate the asset, any edits made in the animator window(besides the positions of the state nodes) are overwritten with the C# animator settings.
     
    Last edited: Feb 14, 2019
    JBR-games likes this.
  9. XCPU

    XCPU

    Joined:
    Nov 5, 2017
    Posts:
    113
    Given the generator part, was wondering, could the Code just be deleted after generation,
    and use the Animator window to make changes\tweeks forever after?
    Thinking of Builds and excess code in a project,
    seems like a nice way get the majority of an Animator started, for a programmer anyways.
     
    JBR-games likes this.
  10. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Yeah you could delete all C# Animator code(both your code and the plugin), and the animators will function like normal. It just outputs a Unity animator asset. All C# Animator code and all code that use it must be editor only, so it won't be left in builds anyway.

    One small difference is that the C# Animator scriptable object will be left as a sub asset of the animator asset. This shouldn't make any noticeable difference at all though. I am thinking about ways to avoid this still, though.

    But yeah, essentially you can write the code, generate once, and then just work directly in the animator window like normal.
     
  11. XCPU

    XCPU

    Joined:
    Nov 5, 2017
    Posts:
    113
    Excellent, sounds like a good option.
     
    CSharpAnimator likes this.
  12. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    If you end up using C# Animator, let me know if you have any problems with this or anything else. Thanks!
     
  13. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    543
    I think im going to pick this up soon... Any docs i can look over ?
     
  14. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Awesome!

    Only a document going over some of the stuff you can achieve with it. It comes with the package, but I'll update my initial post with the link:

    Here it is:
    https://pastebin.com/p8axC6K6

    I suspect you mean something more along the lines of an API reference though. Personally I find looking at examples and source more effective. Is an API reference something you would find useful?
     
  15. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    410
    Hi, I'm interested in this asset but I have existing Animator. Can it generate C# from Animator? If possible, it will become a very powerful tool and full Round-Trip is allowed. Thanks.
     
  16. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    Hey chrisk! C# Animator doesn't generate C# from an existing animator asset, unfortunately. I don't plan on adding this either. If I was in your situation and I wanted to use C# Animator, I would probably just write it out from scratch. In my experience writing C# to generate animators is pretty fast, even if you're recreating fairly large animators(I did this when I first created the tool).

    It's an interesting idea. I think being able to generate both ways would create a few problems, though.
     
  17. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    410
    Is this because it's impossible to implement? I prefer editing C# but for a simple tweak and testing, working on Animator directly would be more intuitive. Force to edit in C# and generate all the time will probably stop many onlookers to adopt your asset. Unless it's impossible to do so, please consider adding the full round-trip support. Thanks.
     
    JBR-games likes this.
  18. CSharpAnimator

    CSharpAnimator

    Joined:
    Feb 5, 2019
    Posts:
    12
    I don't think it's impossible(depending on what you're asking about), but there's a few problems I can think of straight away. For example, if you create a C# animator, generate the animator asset, edit the animator asset, then you generate C# from that asset. In this case what happens to things that you have written in C# that can't be described in an animator. For example, you could use a loop in C# to generate a variable amount of states, or you could write a few comments.

    Basically, from my understanding, given an animator asset, there's an infinite set of C# snippets that could generate that asset. So, if we're talking about a full round trip on any change in the animator, then it's not possible to avoid potentially losing work on the C# side.

    It could be possible to read some stuff back for tweaking, like I already do for the position of nodes in the animator graphs, but again this seems tricky. What if that value you're tweaking was a constant in C#? When you generate C# code, will it stomp that constant with a literal value?

    You can currently tweak stuff in the animator, though. It's just that when you go to generate, all those changes will be lost. You might find some value in tweaking the animator asset directly until you get the right values, then replicating those values in C# manually, but depending on how much you do this, it might be error prone and tedious.
     
  19. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    410
    Yeah, it's not easy to support full round-trip but I'm sure you understand that tweeking/testing in Animator has some value.
    I guess what we need is a text representation of Animator and it doesn't even have to be c#.
    It might be easier to have Animator DSL and we can use that to generate Animator and Animator to generate DSL.
    If it's possible, really great, if not, I'm not really sure what the true benefit of this asset is. One benefit I can think of is that I can diff the changes and merging two different Animator into one more easily.

    And having a DSL can have another benefit. We can probably support Animatable if we want. I think Animatable will be the future Animator since it will be based on ECS and run parallel jobs.

    Something to think about. Thanks.
     
    CSharpAnimator likes this.
  20. Ooi

    Ooi

    Joined:
    Nov 22, 2015
    Posts:
    10
    It'll be easy to do a full-round-trip if everything is done in markup languages like HTML/XML. But yeah... C# gives you more control especially when you want to add multiple animations by parsing asset names. Or generate the Animator assets more dynamically.

    EDIT: Just went and checked the Animator file. It's just purely written in YAML and looks like hell. Let stay with C#.