title: Create animations programmatically in Unity?
body: <p>In my game I have a big catalog of gear: Armors, weapons and shields. The combinations between these can be really immense.</p>
  <p><a href="https://i.stack.imgur.com/gFJVt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gFJVt.png" alt="enter image description here" /></a></p>
  <p>Besides that, the player has the option of switching in-game to a different set of armor-weapon combination. In the end to solve this, I have used the following object structure.</p>
  <p><a href="https://i.stack.imgur.com/fNzDl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fNzDl.png" alt="enter image description here" /></a></p>
  <p>Whenever I switch the weapons, I activate/deactivate the necessary GameObjects. The animations are set in this way:</p>
  <p><a href="https://i.stack.imgur.com/S7iKA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/S7iKA.png" alt="enter image description here" /></a></p>
  <p>Now, the problem is creating the animation. I first considered pre-rendering programatically all the combinations, but my catalog is so huge, that it would create 100s, if not 1000s of animations. So I opted for a different solution. Create in playtime the animation, once I knew what gear would the player select. For that, I created a script to take care of that. The problem is that I have been using APIs from <code>UnityEditor</code>, and now I have realized the build will not work. Specifically because of 2 different classes: <code>EditorCurveBinding</code> and <code>ObjectReferenceKeyframe</code>.</p>
  <p>This is a couple snippets of how I was using this classes when creating the animations:</p>
  <pre class="lang-cs prettyprint-override"><code>static EditorCurveBinding GetEditorCurveBinding(string path = &quot;&quot;)
  {
      EditorCurveBinding spriteBinding = new EditorCurveBinding();
      spriteBinding.type = typeof(SpriteRenderer);
      spriteBinding.path = path;
      spriteBinding.propertyName = &quot;m_Sprite&quot;;
  
      return spriteBinding;
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>static ObjectReferenceKeyframe GetKeyframe(float time, Sprite sprite)
  {
      ObjectReferenceKeyframe keyframe = new ObjectReferenceKeyframe();
      keyframe.time = time / FRAMERATE;
      keyframe.value = sprite;
      return keyframe;
  }
  </code></pre>
  <p>Now, the problem with the Curve, I think I managed to solve, replacing it with this code, replacing <code>EditorCurveBinding</code> with <code>AnimationCurve</code>:</p>
  <pre><code>AnimationClip clip = ...
  AnimationCurve curve = new AnimationCurve();
  clip.SetCurve(path, typeof(SpriteRenderer), &quot;m_Sprite&quot;, curve);
  </code></pre>
  <p>But I have no idea how to set the sprites for each animation. I thought that using <code>curve.AddKey</code>could be helpful, but I have seen no way to add a sprite there.</p>
  <p>How could I rewrite that code to avoid using <code>UnityEditor</code>?</p>
  <p><a href="https://pastebin.com/TYJzPwip" rel="noreferrer">Full code</a></p>