Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Activating GameObjects from the animation clip

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jul 16, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I want to use an EditorWindow to create animation curves that will activate my hitboxes at the correct time.

    When adding manually a hitbox in my animation clip. I can see its type among other things: UnityEngine.GameObject
    http://i.imgur.com/50mB7Qg.png

    So I coded this:
    Code (CSharp):
    1. EditorCurveBinding bind = new EditorCurveBinding();
    2. bind.type = typeof(UnityEngine.GameObject);
    3. bind.propertyName = "m_IsActive";
    4. bind.path = "Hitboxes/" + hitbox.name;
    5.  
    6. var keys = new List<ObjectReferenceKeyframe>();
    7.  
    8. var keyOn = new ObjectReferenceKeyframe();
    9. keyOn.value = hitbox.gameObject;
    10. keyOn.time = 0f;
    11.  
    12. keys.Add(keyOn);
    13.  
    14. AnimationUtility.SetObjectReferenceCurve(anim, bind, keys.ToArray());
    I get the following error because of AnimationUtility.SetObjectReferenceCurve:
    "Can't assign curve because the type does not inherit from component". (http://i.imgur.com/XwjtnSE.png)

    Indeed, typeof(GameObject) doesn't inherit from Component, but what can I do to make this work? Logically this should be possible since it can be done manually.
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I found the solution:
    Code (CSharp):
    1.  
    2. private void AddAnimationCurveAccordingToHitboxNames()
    3.     {
    4.         var spriteBind = new EditorCurveBinding();
    5.         spriteBind.type = typeof(SpriteRenderer);
    6.         spriteBind.propertyName = "m_Sprite";
    7.  
    8.         foreach (var hitbox in Hitboxes)
    9.         {
    10.             var goBinding = new EditorCurveBinding();
    11.             goBinding.type = typeof(GameObject);
    12.             goBinding.propertyName = "m_IsActive";
    13.             goBinding.path = "Hitboxes/" + hitbox.name;
    14.  
    15.            
    16.             foreach (var anim in AnimationClips)
    17.             {
    18.                 var frameTime = 1 / anim.frameRate;
    19.  
    20.                 var sprites = AnimationUtility.GetObjectReferenceCurve(anim, spriteBind);
    21.  
    22.                 // find the sprites that are named correctly and their time
    23.                 foreach (var s in sprites)
    24.                 {
    25.                     var val = s.value;
    26.                     var time = s.time;
    27.  
    28.                     var spriteName = FindNameWithoutTagWithoutVersion(val.name, true);
    29.  
    30.                     // TODO: This only works if there's 1 same sprite name per animation? Do we care?
    31.                     // TODO: What if the hitbox is the last frame, it doesn't get deactivated
    32.                     if (spriteName == hitbox.name)
    33.                     {
    34.                         var curve = new AnimationCurve();
    35.  
    36.                         var keyBefore = new Keyframe();
    37.                         keyBefore.value = 0;
    38.                         keyBefore.time = time - frameTime;
    39.  
    40.                         var keyAfter = new Keyframe();
    41.                         keyAfter.value = 0;
    42.                         keyAfter.time = time + frameTime;
    43.  
    44.                         var keyOn = new Keyframe();
    45.                         keyOn.value = 1;
    46.                         keyOn.time = time;
    47.  
    48.                         curve.AddKey(keyBefore);
    49.                         curve.AddKey(keyOn);
    50.                         curve.AddKey(keyAfter);
    51.  
    52.                         AnimationUtility.SetEditorCurve(anim, goBinding, curve);
    53.                     }
    54.                 }
    55.             }
    56.             AssetDatabase.SaveAssets();
    57.             AssetDatabase.Refresh();
    58.         }
    59.     }
    60.  
     
    Last edited: Jul 16, 2017