一、工程结构
二、创建AnimationTest.cs脚本并挂在箭塔上
using UnityEngine; using System.Collections; public class AnimationTest : MonoBehaviour { public Animation animation; void Start () { if (null == animation) animation = GetComponent<Animation>(); //从Resources文件夹动态加载另外两个动画剪辑 AnimationClip jianta_ani_open = Resources.Load("jianta_ani_open") as AnimationClip; AnimationClip jianta_ani_close = Resources.Load("jianta_ani_close") as AnimationClip; animation.AddClip(jianta_ani_open, "open"); animation.AddClip(jianta_ani_close, "close"); } void OnGUI() { if (GUILayout.Button("Play open")) { animation.CrossFade("open"); } if (GUILayout.Button("Play close")) { animation.CrossFade("close"); } if (GUILayout.Button("Take 001")) { animation.CrossFade("Take 001"); } } }
运行效果