在获取之前交换通道并添加立方体对象:
cubeObject = GameObject.Find("Cube");
Animation animation = cubeObject.GetComponent<Animation>();
在“播放”按钮之前在资源文件夹中创建动画。回顾动画cntl+6>>播放:
void ApplyAnimation()
{
cubeObject = Selection.activeGameObject;
if (!cubeObject) return;
var _animation = cubeObject.GetComponent<Animation>();
if (!_animation) _animation = cubeObject.AddComponent<Animation>();
var clip = new AnimationClip();
var curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
clip.name = "Move"; // set name
clip.legacy = true; // change to legacy
_animation.clip = clip; // set default clip
_animation.AddClip(clip, clip.name); // add clip to animation component
AssetDatabase.CreateAsset(clip, "Assets/"+clip.name+".anim"); // to create asset
_animation.Play(); // then play
}