本文主要是介绍Unity动画系统Animator学习之蒙古包搭建动画演示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
蒙古包的分解
一、加载蒙古包模型
制作动画之前,必须先设置好哪些物体是做动画的,一定要分解开,并且设置好相应的名字。在3Dmax中设置好后,导出为.fbx格式,再导入到unity中的Assets中,拖入场景即可。
二、整体动画流程部分流程图
三、必须有各个零部件的不同动画控制
1.文字的动画控制
Wenzishow片段
注意Color.a是0,1,0,1,1变化,这样,文字显示的时候,会暗,亮,暗,亮,亮,一直显示文字
wenziHide片段的设置是从有到无,从亮变暗的过程。0秒的时候Color.a是1,1秒的时候,Color.a是0。
把文字控制器拖拽给文本控件
注意图片初始值为透明色的,也就是文字是不可见的,没有解说词。
2.高亮显示
很多只有一个动画片段的,基本用到的是高亮显示动画,大部分都差不多。不一样的地方,有的是需要有位置,有的仅仅高亮即可。如果仅仅是高亮显示,不需要动画控制器,一个动画片段可以了。
这个片段中,使用了Mesh Renderer.Enabled属性和Mesh Renderer.Material._Color属性。目的是高亮并闪烁,高亮方法主要通过r,g,b的值来控制。
第0秒时候的数值,如下图,Enabled没有勾选,意思是不显示,color的值分别是初始的值:1,1,0;
0.10s的时候,显示并高亮,Enabled勾选,意思是显示,color的值分别是初始的值:1,1,0;黄色比较显眼。
第20秒时候的数值, Enabled没有勾选,意思是不显示.
第35秒时候,Enabled勾选,意思是显示,color的值分别是:0.588,0.588,0.588;地板本身的颜色值
给地板加上这个动画控制器,初始设置为不可见
为所有的零件像如此设置好动画控制器,初始设置为不可见。
比如哈娜
3.摄像头的不同角度
根据不同需要,对摄像机有不同的移动和旋转。为每个动画过度都设置相应的过渡条件。每个片段都是什么内容?分别打开看看。Cam片段为俯视,Cam1是从俯视再到正门位置。Cam_rotate是绕着蒙古包旋转,因为这里需要看到哈娜之间怎么绑定的。Cam_rotate_again是穿插在哈娜绑定动画之后,乌尼开始动画之前。Jinru_mgb是进入蒙古包,旋转一圈,再次出来的动画。这几个摄像机的动画都是穿插在不同零部件的动画中。
通过脚本代码控制不同的动画,在适当的时候播放
//动画步骤int step = 0;float time = 3.0f;int i = 0;//蒙古包模型public GameObject[] mgb;public GameObject WenziObj;public GameObject dibanObj;public GameObject Cam;public GameObject Taonao;public GameObject[] menObj;public GameObject hanaObj;public GameObject hanaObj1;public GameObject hanaObj2;public GameObject hanaObj3;public GameObject hanaObj4;public GameObject shengziObj01;public GameObject shengziObj02;public GameObject shengziObj03;public GameObject shengziObj04;public GameObject shengziObj05;public GameObject shengziObj06;public GameObject lishengObj01;public GameObject lishengObj02;public GameObject[] wuniObj;public GameObject[] weizhanObj;public GameObject[] waishengObj;public GameObject[] dingzhanObj;public GameObject[] waisheng_Obj;public GameObject light;GameObject[] jiaju;//引用Animator wenziAnimator;AnimatorStateInfo wenziStateInfo;Animator CamAimator;AnimatorStateInfo CamStateInfo;Animator dibanAnim;Animator TaonaoAnim;Animator menAnim;Animator hanaAnim;Animator hanaAnim1;Animator hanaAnim2;Animator hanaAnim3;Animator hanaAnim4;Animator shengziAnim1;Animator shengziAnim2;Animator shengziAnim3;Animator shengziAnim4;Animator shengziAnim5;Animator shengziAnim6;Animator lishengAnim1;Animator lishengAnim2;Animator wuniAnim;Animator zhanbuAnim;Animator waishengAnim;Animator dingzhanAnim;Animator waisheng_Anim;// Use this for initializationvoid Start () {//初始化获取模型身上的动画组件wenziAnimator = WenziObj.GetComponent<Animator>();dibanAnim = dibanObj.GetComponent<Animator>();CamAimator = Cam.GetComponent<Animator>();TaonaoAnim = Taonao.GetComponent<Animator>();hanaAnim = hanaObj.GetComponent<Animator>();hanaAnim1 = hanaObj1.GetComponent<Animator>();hanaAnim2 = hanaObj2.GetComponent<Animator>();hanaAnim3 = hanaObj3.GetComponent<Animator>();hanaAnim4 = hanaObj4.GetComponent<Animator>();shengziAnim1 = shengziObj01.GetComponent<Animator>();shengziAnim2 = shengziObj02.GetComponent<Animator>();shengziAnim3 = shengziObj03.GetComponent<Animator>();shengziAnim4 = shengziObj04.GetComponent<Animator>();shengziAnim5 = shengziObj05.GetComponent<Animator>();shengziAnim6 = shengziObj06.GetComponent<Animator>();lishengAnim1 = lishengObj01.GetComponent<Animator>();lishengAnim2 = lishengObj02.GetComponent<Animator>();jiaju = GameObject.FindGameObjectsWithTag("jiaju");foreach (GameObject go in jiaju){go.gameObject.GetComponent<Renderer>().enabled = false;}light.GetComponent<Light>().enabled = false;}void Update () {print(step);switch (step){case 0://文字显示if(wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第一步,平整包址,确定方位";wenziAnimator.SetInteger("wenziState", 1);step = 1;}break;case 1://文字消失if(wenziAnimator){wenziStateInfo = wenziAnimator.GetCurrentAnimatorStateInfo(0);if(wenziStateInfo .IsName ("Base Layer.wenziShow")){if (wenziStateInfo .normalizedTime > 1){wenziAnimator.SetInteger("wenziState", 2);step = 2;}}}break;case 2://地板找平if(dibanAnim){dibanAnim.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 3.0f;step = 3;}}break;case 3://摄像机动画if(CamAimator){CamAimator.SetInteger("camera", 1);step = 4;}break;case 4://显示套脑if (TaonaoAnim ){TaonaoAnim .SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第二步,套脑主梁和东西轴线对齐,门楣正中和套脑南北轴线对齐,依次摆好哈纳";wenziAnimator.SetInteger("wenziState", 1);}time = 2.0f;step = 5;i = 0;}}break;case 5://显示门if (i < 2){menAnim = menObj[i].GetComponent<Animator>();if(menAnim){menAnim.SetInteger("taonaoStep", 1);i++;}}else{i = 0;step = 6;}break;case 6://显示hanaif (hanaAnim){hanaAnim.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 7;}}break;case 7://显示hanaif (hanaAnim1){hanaAnim1.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 8;}}break;case 8://显示hanaif (hanaAnim2){hanaAnim2.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 9;}}break;case 9://显示hanaif (hanaAnim3){hanaAnim3.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 10;}}break;case 10://显示hanaif (hanaAnim4){hanaAnim4.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 5.0f;step = 11;}}break;case 11://切换摄像机到cam2if (CamAimator){CamAimator.SetInteger("camera", 2);step = 12;}break;case 12://文字消失if (wenziAnimator){wenziStateInfo = wenziAnimator.GetCurrentAnimatorStateInfo(0);if (wenziStateInfo.IsName("Base Layer.wenziShow")){if (wenziStateInfo.normalizedTime > 1){wenziAnimator.SetInteger("wenziState", 2);step = 13;}}}break;case 13://切换摄像机到cam3if (CamAimator){CamStateInfo = CamAimator.GetCurrentAnimatorStateInfo(0);if (CamStateInfo.IsName("Base Layer.Cam_1")){if (CamStateInfo.normalizedTime > 1){CamAimator.SetInteger("camera", 3);step = 14;}}}break;case 14://文字显示if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第三步,绑定门和哈纳,绑定相邻哈纳,捆好里围绳";wenziAnimator.SetInteger("wenziState", 1);step = 15;}break;case 15://绳子if (shengziAnim1){shengziAnim1.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 3.0f;step = 16;}}break;case 16://绳子if (shengziAnim2){shengziAnim2.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 5.0f;step = 17;}}break;case 17://绳子if (shengziAnim3){shengziAnim3.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 3.0f;step = 18;}}break;case 18://绳子if (shengziAnim4){shengziAnim4.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 2.0f;step = 19;}}break;case 19://绳子if (shengziAnim5){shengziAnim5.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 2.0f;step = 20;}}break;case 20://绳子if (shengziAnim6){shengziAnim6.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 21;}}break;case 21://里绳1if (lishengAnim1){lishengAnim1.SetInteger("taonaoStep", 1);time -= Time.deltaTime;if (time < 0){time = 2.0f;step = 22;}}break;case 22://里绳2if (lishengAnim2){lishengAnim2.SetInteger("taonaoStep", 1);step = 23;}break;case 23://文字显示if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第四步,上套脑和插乌尼";wenziAnimator.SetInteger("wenziState", 1);step = 24;}break;case 24://显示套脑移动if (TaonaoAnim){TaonaoAnim.SetInteger("taonaoStep", 2);time -= Time.deltaTime;if (time < 0){time = 1.0f;step = 25;}}break;case 25://摄像机动画if (CamAimator){CamStateInfo = CamAimator.GetCurrentAnimatorStateInfo(0);if (CamStateInfo.IsName("Base Layer.Cam_rotate")){if (CamStateInfo.normalizedTime > 1){CamAimator.SetInteger("camera", 4);step = 26;}}}break;case 26://乌尼time -= Time.deltaTime;if (time < 0){if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第四步,先从西、东、北、南四个方向上各插二三根乌尼,再插入其余乌尼";wenziAnimator.SetInteger("wenziState", 1);}time = 1.0f;}if (i < 12){wuniAnim = wuniObj[i].GetComponent<Animator>();if (wuniAnim){time -= Time.deltaTime;if (time < 0){time = 1.0f;wuniAnim.SetInteger("taonaoStep", 1);i++;}}}else{for (i = 12; i <=56; i++){wuniAnim = wuniObj[i].GetComponent<Animator>();if (wuniAnim){wuniAnim.SetInteger("taonaoStep", 1);}}}if (i == 57){step = 27;i = 0;}break;case 27://文字显示if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第五步,苫围毡,依次苫好西南、东南、东北、西北围毡,注意上风口毡子压住下风口围毡";wenziAnimator.SetInteger("wenziState", 1);step = 28;}break;case 28://毡布if (i < 4){zhanbuAnim = weizhanObj[i].GetComponent<Animator>();if (zhanbuAnim){time -= Time.deltaTime;if (time < 0){time = 2.0f;zhanbuAnim.enabled = true;i++;}}}if (i == 4){i = 0;step = 30;}break;case 30://文字显示if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第六步,捆外围绳,捆在围毡外面的绳索,先捆中间一根,再上下两根";wenziAnimator.SetInteger("wenziState", 1);step = 31;}break;case 31://毡布绳子if (i < 3){waishengAnim = waishengObj[i].GetComponent<Animator>();if (waishengAnim){time -= Time.deltaTime;if (time < 0){time = 2.0f;waishengAnim.enabled = true;i++;}}}if (i == 3){step = 32;i = 0;}break;case 32://文字显示time -= Time.deltaTime;if (time < 0){time = 2.0f;if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第七步,盖顶毡,先盖前片(南边),再盖后片(北边)";wenziAnimator.SetInteger("wenziState", 1);step = 33;}}break;case 33://顶毡if (i < 2){dingzhanAnim = dingzhanObj[i].GetComponent<Animator>();if (dingzhanAnim){time -= Time.deltaTime;if (time < 0){time = 2.0f;dingzhanAnim.SetInteger("taonaoStep", 1);i++;}}}else{step = 34;i = 0;}break;case 34://文字显示time -= Time.deltaTime;if (time < 0){time = 2.0f;if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第八步,顶毡绳索绑成吉祥结";wenziAnimator.SetInteger("wenziState", 1);step = 35;}}break;case 35://顶毡吉祥结if (i < 6){waisheng_Anim = waisheng_Obj[i].GetComponent<Animator>();if (waisheng_Anim){time -= Time.deltaTime;if (time < 0){time = 1.0f;waisheng_Anim.SetInteger("taonaoStep", 1);i++;}}}else{i = 0;step = 36;time = 2.0f;}break;case 36://文字显示time -= Time.deltaTime;if (time < 0){time = 1.0f;if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第九步,盖顶饰,绑绳索";wenziAnimator.SetInteger("wenziState", 1);step = 37;i = 2;}}break;case 37:time -= Time.deltaTime;if (time < 0){time = 2.0f;dingzhanAnim = dingzhanObj[i].GetComponent<Animator>();if (dingzhanAnim){dingzhanAnim.enabled = true;i = 6;step = 38;}}break;case 38://顶毡的外绳if (i >= 6 && i < 18){print(i);waisheng_Anim = waisheng_Obj[i].GetComponent<Animator>();if (waisheng_Anim){waisheng_Anim.SetInteger("taonaoStep", 1);i++;}}else if (i == 18){i = 0;step = 39;}break;case 39://铺盖毡time -= Time.deltaTime;if (time < 0){time = 1.0f;if (wenziAnimator){WenziObj.transform.Find("Text").GetComponent<Text>().text = "第十步,铺盖毡,四四方方的毡子,每个角对着东、南、西、北四个方向";wenziAnimator.SetInteger("wenziState", 1);step = 41;i = 3;}}break;case 40://文字消失if (wenziAnimator){wenziStateInfo = wenziAnimator.GetCurrentAnimatorStateInfo(0);if (wenziStateInfo.IsName("Base Layer.wenziShow")){if (wenziStateInfo.normalizedTime > 1){wenziAnimator.SetInteger("wenziState", 2);step = -1;}}}break;case 41://铺盖毡dingzhanAnim = dingzhanObj[i].GetComponent<Animator>();if (dingzhanAnim){dingzhanAnim.enabled = true;step = 42;i = 18;}break;case 42://顶毡的外绳if (i >= 18 && i < 22){print(i);waisheng_Anim = waisheng_Obj[i].GetComponent<Animator>();if (waisheng_Anim){waisheng_Anim.SetInteger("taonaoStep", 1);i++;}}else if (i == 22){i = 0;step = 43;}break;case 43://门动画 step=4;及所有家具显示;及摄像机在里面走一圈;//显示所有家具foreach (GameObject go in jiaju){go.gameObject.GetComponent<Renderer>().enabled = true;}menAnim = menObj[0].GetComponent<Animator>();if (menAnim){menAnim.SetInteger("taonaoStep", 3);step = 44;}break;case 44:light.GetComponent<Light>().enabled = true;if (CamAimator){CamStateInfo = CamAimator.GetCurrentAnimatorStateInfo(0);if (CamStateInfo.IsName("Base Layer.Cam_pause")){CamAimator.SetInteger("camera", 5);step = 40;}}break;}}
demo演示地址
蒙古包搭建动画
额外的知识
Animator组件的学习
属性 | 功能 |
---|---|
Controller | 模型的动画控制器文件 |
Avatar | 模型的骨骼信息文件 |
Apply Root Motion | 绑定该组件的GameObject的位置是否可以由动画进行改变 |
Update Mode | 更新模式:Normal表示使用Update进行更新,Animate Physics表示使用FixUpdate进行更新(一般用在和物体有交互的情况下),Unscale Time表示无视timeScale进行更新(一般用在UI动画中) |
Culling Mode | 剔除模式:Always Animate表示即使摄像机看不见也要进行动画播放的更新,Cull Update Transform表示摄像机看不见时停止动画播放但是位置会继续更新,Cull Completely表示摄像机看不见时停止动画的所有更新。 |
常用API
Animator.GetCurrentAnimatorStateInfo(int index):获取当前动画器状态信息
获取的是当前动画器的Parameters(参数)信息。
Animator.SetInteger(“参数名”, int类型参数值):通过设置参数值来控制动画的播放
AnimatorStateInfo.IsName 是否此名称,也就是参数的名称。
AnimatorStateInfo.normalizedTime 归一化时间(0-1之间)用来表示动画开始播放到播放完成的时间,需要注意的是,用来检测动画是否播放完成时,需放在Update方法里每帧监听,不然无法监听到动画是否播放完成。
这篇关于Unity动画系统Animator学习之蒙古包搭建动画演示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!