Unity3d之Animation(动画系统)

1,动画系统配置,2,代码控制动画

原文地址: http://blog.csdn.net/dingkun520wy/article/details/51247487

1,动画系统配置

创建游戏对象并添加Animation组件,然后将动画文件拖入组件。

进入动画文件的Debug属性面板

选中Legacy属性

选中游戏对象,打开Animation编辑窗口

添加动画变化属性

需改关键帧的属性值

配置完成后运行即可得到动画效果

2,代码控制动画

Play("ation 1" );,播放动画,传入参数为动画名字

Stop("ation 1") ,停止动画,传入参数为动画名字

CrossFade("ation 1", 0.5f); ,有过度的切换动画,传入参数(动画名字,过度时间)

实例代码

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NewBehaviourScript : MonoBehaviour {  
  5.        
  6.     Animation m_anim;  
  7.     private float scaleW = 1.0f;        //宽度缩放比  
  8.     private float scaleH = 1.0f;        //高度缩放比  
  9.     // Use this for initialization  
  10.     void Start () {  
  11.         //获取动画组件  
  12.         m_anim = GetComponent<Animation>();  
  13.         if (!m_anim.isPlaying)  
  14.         {  
  15.             //若没有动画播放,默认播放New Animation 1动画  
  16.             m_anim.CrossFade("ation 1", 0.2f);  
  17.         }  
  18.     }  
  19.       
  20.     // Update is called once per frame  
  21.     void Update () {  
  22.         scaleW = (float)Screen.width / 800;     //计算宽度缩放比  
  23.         scaleH = (float)Screen.height / 480;    //计算高度缩放比  
  24.     }  
  25.     void OnGUI()  
  26.     {  
  27.         GUI.skin.button.fontSize = (int)(25 * scaleW);        //调整按钮字体大小  
  28.   
  29.         if (GUI.Button(new Rect(70 * scaleW, 50 * scaleH, 90 * scaleW, 40 * scaleH), "ation 1"))  
  30.         {  
  31.             m_anim.Play("ation 1" );  
  32.         }   
  33.         if (GUI.Button(new Rect(70 * scaleW, 110 * scaleH, 90 * scaleW, 40 * scaleH), "imation"))  
  34.         {  
  35.             m_anim.Play("imation");  
  36.         }  
  37.         if (GUI.Button(new Rect(70 * scaleW, 170 * scaleH, 220 * scaleW, 40 * scaleH), "有过度播放ation 1"))  
  38.         {  
  39.             m_anim.CrossFade("ation 1", 0.5f);  
  40.         }  
  41.         if (GUI.Button(new Rect(70 * scaleW, 230 * scaleH, 220 * scaleW, 40 * scaleH), "有过度播放imation"))  
  42.         {  
  43.             m_anim.CrossFade("imation", 0.5f);  
  44.         }  
  45.           
  46.     }  
  47. }  


将代码添加到游戏对象,运行游戏。

原文地址:https://www.cnblogs.com/lexiaoyao-jun/p/5450790.html