unity开发,动画播放以及动画拼接播放

如需查看具体项目例子,可以去各大应用市场下载“萌萌鸡”app。体验功能!

Unity3D动画系统主要有两种:Animator和Animation. Animation Clip 动画剪辑 对于Animation Clip可以进行动画分割 对于每一个clip,在使用脚本来控制播放。

Unity3D支持多种格式模型,例如FBX、OBJ和MAX等
导入模型之后,添加animation,会发现找不到动画
记得 选择模型找到inspector,把animation Type设置为Legacy即可

unity开发,动画播放以及动画拼接播放_第1张图片
Paste_Image.png

接下来就可以剪切动画了,Clips下面有+号就可以添加以及截取自己想要的片段了,和剪辑视频一样

unity开发,动画播放以及动画拼接播放_第2张图片
Paste_Image.png

记得设置好了要保存,目录下,就多出来这几个Animation clips


unity开发,动画播放以及动画拼接播放_第3张图片
Paste_Image.png

将模型添加到Target下面,选中模型添加add component 搜索Animation ,在animation里面element添加刚刚截取的动画就可以

unity开发,动画播放以及动画拼接播放_第4张图片
Paste_Image.png

接下来就是书写代码了

using UnityEngine;
using System.Collections;
public class AnimateControl : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
    if (GUI.Button (new Rect (0, 0, 100, 80),"左手动起来")) {
        transform.animation.Play("fumo_left_hand");
    }
    if (GUI.Button (new Rect (0, 100, 100, 80),"右手动起来")) {
        transform.animation.Play("fumo_right_hand");
    }
    if (GUI.Button (new Rect (0, 200, 100, 80),"左脚动起来")) {
        transform.animation.Play("fumo_left_foot");
    }
    if (GUI.Button (new Rect (0, 300, 100, 80),"右脚动起来")) {
        transform.animation.Play("fumo_right_foot");
    }
}
}

把脚本绑定在模型上面,播放动画有2中方法

(1) transform.animation.Play("fumo_left_hand");//fumo_left_hand为动画的名字
(2) this.GetComponent().Play("run");

如果不是脚本不是绑定在模型上面,那么按照目录去获取到模型

GameObject root = GameObject.Find("/imageTarget");
mHealthAnimationBian= root.transform.Find("HealthAnimationBian").gameObject; 
//这样就能获取模型HealthAnimationBian
mHealthAnimationBian.transform.GetComponent().Play("fumo_right_foot");

这个有play()方法,也可以用CrossFade()方法
//play一个是直接插入播放,CrossFade另一个是混合淡入淡出~

接下来就是处理动画拼接播放

mHealthAnimationBian.GetComponent().CrossFade("fumo_left_hand");//先播放这个动画
StartCoroutine(IntroduceVoice());
 IEnumerator IntroduceVoice() 
 {
 //播放fumo_left_hand的动画所需要的时间 为9秒,过后启动第二个动画
 yield return new WaitForSeconds(9); 
 mHealthBaseAnimationDealPrefab.GetComponent ().CrossFade ("fumo_right_hand");
 } 

yield关键字用于遍历循环中,yield return用于返回IEnumerable,yield break用于终止循环遍历。
StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程。
当希望获取一个IEnumerable类型的集合,而不想把数据一次性加载到内存,就可以考虑使用yield return实现"按需供给"。

可能有小伙伴遇到过unity模型在手机上面显示的模型有变形的效果,怎么处理?
解决方案:模型中有一个normal 属性,设置calculate 就可以设置smoothing angle ,一般80-100

Paste_Image.png

动画复用的时候:有时候会出现没有动画效果,重启一下就好了,因为是复制的,可能没有及时刷新

unity3d 控制播放动画的速度:
动画speed是AnimationState的参数
用法:

//这是动画放慢一半速度
mHealthBaseAnimationDealPrefab.GetComponent ()  
["health_hudongshuohua"].speed=0.5f;//

你可能感兴趣的:(unity开发,动画播放以及动画拼接播放)