在我的游戏中,我有一个大目录的装备:盔甲、武器和盾牌。它们之间的组合可以非常巨大。
此外,玩家可以在游戏中选择不同的盔甲-武器组合。为了解决这个问题,我使用了以下的对象结构。
每当我切换武器时,我会激活/停用必要的游戏对象。动画设置如下:
现在的问题是创建动画。我最初考虑通过程序预渲染所有组合,但我的目录非常庞大,这将创建数百甚至数千个动画。因此,我选择了另一种解决方案。一旦我知道玩家会选择什么齿轮,就在播放时间内创建动画。为此,我创建了一个脚本来处理它。问题是我一直在使用
UnityEditor
的API,现在我已经意识到构建将无法工作。具体来说,由于两个不同的类:EditorCurveBinding
和ObjectReferenceKeyframe
。
以下是我创建动画时如何使用这些类的几个片段:
static EditorCurveBinding GetEditorCurveBinding(string path = "")
{
EditorCurveBinding spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = path;
spriteBinding.propertyName = "m_Sprite";
return spriteBinding;
}
static ObjectReferenceKeyframe GetKeyframe(float time, Sprite sprite)
{
ObjectReferenceKeyframe keyframe = new ObjectReferenceKeyframe();
keyframe.time = time / FRAMERATE;
keyframe.value = sprite;
return keyframe;
}
现在,我认为我已经解决了曲线的问题,用这段代码来替换它,将EditorCurveBinding
替换为AnimationCurve
:
AnimationClip clip = ...
AnimationCurve curve = new AnimationCurve();
clip.SetCurve(path, typeof(SpriteRenderer), "m_Sprite", curve);
但我不知道如何为每个动画设置精灵。我认为使用curve.AddKey
可能会有帮助,但我没有看到任何添加精灵的方法。
我该如何重写该代码以避免使用UnityEditor
?