[第二十一天]从0开始的UnityAR手机游戏开发-切换Animation动画

范例小龙的动画是挂在Animation而不是Animator上,所以本次章节会教大家如何切换Animation的动画

如下图中所示,SJ001为小龙的物件名称
https://ithelp.ithome.com.tw/upload/images/20211005/20140569uXySdRTlOK.png

开启前次的ChangeAnimation,在脚本中添加本次的程序码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeAnimation : MonoBehaviour
{
    [Header("Hierarchy的正方体物件")]
    public GameObject Cube;
    //本次新增要抓取小龙物件的程序码
    [Header("Hierarchy的小龙物件")]
    public GameObject SJ001;
    public void changeAni() {
        //Cube的Animator Attack布林开启
        Cube.GetComponent<Animator>().SetBool("Attack", true);
        //5秒之後执行AniClose();
        Invoke("AniClose", 5);
    }
    void AniClose()
    {
        //Cube的Animator Attack布林关闭
        Cube.GetComponent<Animator>().SetBool("Attack", false);
    }
    //本次新增抓取物件Animation来拨放动画
    public void AniSJwait()
    {
        //SJ001拨放Animation的sj001_wait动画
        SJ001.GetComponent<Animation>().Play("sj001_wait");
    }
    public void AniSJskill2()
    {
        //SJ001拨放Animation的sj001_wait动画
        SJ001.GetComponent<Animation>().Play("sj001_skill2");
    }
    public void AniSJRun()
    {
        //SJ001拨放Animation的sj001_wait动画
        SJ001.GetComponent<Animation>().Play("sj001_run");
    }

}

点击EventSystem开启Inspector将SJ001(小龙物件)拖曳到ChangeAnimation的SJ001物件栏位
https://ithelp.ithome.com.tw/upload/images/20211005/201405694X6xS4CgVf.png

新增3个Button。
觉得介面太乱可以新增一个GameObject(空物件)将Button拖曳到底下成为子物件,点击GameObject(空物件)开启Inspector点选Add Component搜寻Layout选择Vertical Layoout Group可以将GameObject(空物件)底下的子物件排成整齐一列。
https://ithelp.ithome.com.tw/upload/images/20211005/20140569A5RJCSOVTQ.png

将3个Button的OnClick()的GameObject(None)栏位放入EventSystem後分别选择ChangeAnimation→AniSJwait()、ChangeAnimation→AniSJskill2()、ChangeAnimation→AniSJRun()
https://ithelp.ithome.com.tw/upload/images/20211005/201405693StYf6FhW0.png

按下执行键後辨识图卡就可以利用旁边的按钮切换小龙动画罗
https://ithelp.ithome.com.tw/upload/images/20211005/20140569xhL9I6CN5f.png


下一章节会教大家如何在APP内撷取画面


<<:  Day20 - 针对 Metasploitable 3 进行渗透测试(1)

>>:  【程序】我为什麽转职,转职後快乐吗 转生成恶役菜鸟工程师避免 Bad End 的 30 件事 - 22

.Net Core Web Api_笔记25_api结合EFCore资料库操作part3_产品分类资料的编辑与删除(EF的更新写法怎麽这麽多种!如何观察EF产生的SQL)

我们在上一篇的Show.html 已经完成了资料查询呈现 这里要多出操作(比方像是编辑、删除...)...

Day 05 : 来点不一样的 Two Sum

今天来稍微改变一下 Two Sum 这题题目 原本的题目要回传nums中的index,我们来把他改成...

[DAY 19] 卡多利亚良食故事馆

卡多利亚良食故事馆 地点:台南市後壁区42-27号 时间:9:00~17:00 对於一个研替来说 最...

【Git】 发 PR 是什麽?

第一次遇到发PR,原来就是档案修改後,发通知请原作来拉回去(Pull)的请求(Request) 1....

DAY 17- 杂凑函数 HASH

「将咱两个一齐打碎,用水调和;再捻一个你,再塑一个我。我中有你,你中有我;我与你生同一个衾,死同一个...