如何在编辑器中通过脚本创建动画片段
How to create animation clip via script in Editor
我想在 Unity 中使用编辑器创建动画剪辑。
关于如何使用 MonoBehavior 做到这一点。
但是,当我运行在编辑器中输入以下代码时,出现错误。
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;
using System.Text;
public class ExampleWindow : EditorWindow {
private float m_start_time = 0.0f;
private float m_start_value = 0.0f;
private float m_end_time = 5.0f;
private float m_end_value = 10.0f;
public GameObject cubeObject;
[MenuItem("Window/ApplyAnimation")]
public static void ShowWindow ()
{
GetWindow<ExampleWindow>("ApplyAnimation");
}
void OnGUI ()
{
GUILayout.Label("Apply animation", EditorStyles.boldLabel);
if (GUILayout.Button("Apply animation"))
{
ApplyAnimation();
}
}
void ApplyAnimation()
{
Animation animation = GetComponent<Animation> ();
cubeObject = GameObject.Find("Cube");
if (!animation)
{
cubeObject.AddComponent<Animation>();
}
AnimationClip clip = new AnimationClip();
AnimationCurve curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
animation.AddClip(clip, "Move");
animation.Play("Move");
}
}
以下是错误信息
Assets/ExampleWindow.cs(35,31): error CS0103: The name 'GetComponent' does not exist in the current context
我该如何解决这个错误?
swap lanes and add cube object before get:
cubeObject = GameObject.Find("Cube");
Animation animation = cubeObject.GetComponent<Animation>();
在播放按钮之前在资产文件夹中创建动画。查看动画 cntl + 6 >> play:
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
}
我想在 Unity 中使用编辑器创建动画剪辑。
但是,当我运行在编辑器中输入以下代码时,出现错误。
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;
using System.Text;
public class ExampleWindow : EditorWindow {
private float m_start_time = 0.0f;
private float m_start_value = 0.0f;
private float m_end_time = 5.0f;
private float m_end_value = 10.0f;
public GameObject cubeObject;
[MenuItem("Window/ApplyAnimation")]
public static void ShowWindow ()
{
GetWindow<ExampleWindow>("ApplyAnimation");
}
void OnGUI ()
{
GUILayout.Label("Apply animation", EditorStyles.boldLabel);
if (GUILayout.Button("Apply animation"))
{
ApplyAnimation();
}
}
void ApplyAnimation()
{
Animation animation = GetComponent<Animation> ();
cubeObject = GameObject.Find("Cube");
if (!animation)
{
cubeObject.AddComponent<Animation>();
}
AnimationClip clip = new AnimationClip();
AnimationCurve curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
animation.AddClip(clip, "Move");
animation.Play("Move");
}
}
以下是错误信息
Assets/ExampleWindow.cs(35,31): error CS0103: The name 'GetComponent' does not exist in the current context
我该如何解决这个错误?
swap lanes and add cube object before get:
cubeObject = GameObject.Find("Cube");
Animation animation = cubeObject.GetComponent<Animation>();
在播放按钮之前在资产文件夹中创建动画。查看动画 cntl + 6 >> play:
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
}