我正在尝试在运行时创建动画。但是,我没有找到这样做的方法。
Unity可以在运行时创建吗?
我想在网络播放器中做些什么:
检测鼠标点击并获取点击位置。 (花出现在那里)
随机决定花色
通过使用使用3个精灵配置的动画来绽放花朵。 (精灵只是顺序改变)
据我所知,没有方法可以改变动画的颜色(精灵),因此,我正在寻找方法来改变3个精灵的颜色并将它们组合成动画并运行它。
虽然我可以创建一个实例并更改颜色,但我找不到合并的方法。
首先我想设计的是否可能?
答案 0 :(得分:5)
我的导入程序的一部分
private AnimationClip CreateSpriteAnimationClip(string name, List<Sprite> sprites, int fps, bool raiseEvent = false)
{
int framecount = sprites.Count;
float frameLength = 1f / 30f;
AnimationClip clip = new AnimationClip();
clip.frameRate = fps;
AnimationUtility.GetAnimationClipSettings(clip).loopTime = true;
EditorCurveBinding curveBinding = new EditorCurveBinding();
curveBinding.type = typeof(SpriteRenderer);
curveBinding.propertyName = "m_Sprite";
ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[framecount];
for (int i = 0; i < framecount; i++)
{
ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
kf.time = i * frameLength;
kf.value = sprites[i];
keyFrames[i] = kf;
}
clip.name = name;
AnimationUtility.SetAnimationType(clip, ModelImporterAnimationType.Generic);
//if (name != "Fall")
Debug.Log(clip.wrapMode);
clip.wrapMode = WrapMode.Once;
//setAnimationLoop(clip);
AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
clip.ValidateIfRetargetable(true);
if (raiseEvent)
{
//AnimationUtility.SetAnimationEvents(clip, new[] { new AnimationEvent() { time = clip.length, functionName = "on" + name } });
}
//clip.AddEvent(e);
return clip;
}
答案 1 :(得分:1)
我通常使用这种方法将游戏对象从一个pos动画到另一个pos, 可能对你有用。
public void MoveGO (GameObject TempGO, Vector3 StartPos, Vector3 EndPos)
{
float clipLength = 1f;
AnimationCurve curve1 = null, curve2 = null, curve3 = null;
AnimationClip clip = null;
curve1 = AnimationCurve.Linear (0, StartPos.x, clipLength, EndPos.x);
curve2 = AnimationCurve.Linear (0, StartPos.y, clipLength, EndPos.y);
curve3 = AnimationCurve.Linear (0, StartPos.z, clipLength, EndPos.z);
clip = new AnimationClip ();
clip.SetCurve ("", typeof(Transform), "localPosition.x", curve1);
clip.SetCurve ("", typeof(Transform), "localPosition.y", curve2);
clip.SetCurve ("", typeof(Transform), "localPosition.z", curve3);
if (TempGO.GetComponent ("Animation") == null) {
TempGO.AddComponent ("Animation");
}
if (TempGO.animation.IsPlaying ("AnimationDemo")) {
//TempGO.animation["AnimationDemo"].time = 0.5f ;
TempGO.animation.Sample ();
TempGO.animation.RemoveClip ("AnimationDemo");
}
TempGO.animation.AddClip (clip, "AnimationDemo");
TempGO.animation ["AnimationDemo"].speed = 1f;
TempGO.animation.Play ("AnimationDemo");
//TempGO.animation.wrapMode=WrapMode.PingPong;
}
答案 2 :(得分:0)
您可以使用Unity的新animation tools创建该动画。因为你知道有三个精灵你可以制作一个具有该动画的预制件。然后只需将正确的精灵加载到空的精灵对象中。