Unity 动画(旧版-新版)

旧版

旧版-动画组件:Animation

窗口-动画

动画文件后缀: .anim

将制作后的动画拖动到Animation组件上

旧版的操作

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class c1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GetComponent<Animation>().Play("r1");
        }
    }
}

新版

新版动画组件:Animator

控制器需要在项目中创建:动画控制器

双击新建动画控制器

点击场景中的物体,点击窗口-动画,使用"动画"开始制作2个动画

运行时

鼠标左键点击播放动画1,右键点击播放动画2.

代码实现

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class c2 : MonoBehaviour
{
    private Animator animator;

    void Start()
    {
        // 获取动画器组件
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            animator.Play("r2");
        }

        if (Input.GetMouseButtonDown(1))
        {
            animator.Play("r1");
        }
    }
}
相关推荐
EQ-雪梨蛋花汤11 小时前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎
SmalBox12 小时前
【URP】UnityHLSL顶点片元语义详解
unity·渲染
在路上看风景1 天前
9. Mono项目与Unity的关系
unity
在路上看风景1 天前
1.12 Memory Profiler Package - Summary
unity
SmalBox1 天前
【URP】Unity Shader Tags
unity·渲染
极客柒2 天前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
程序猿多布2 天前
Unity AssetBundle详解
unity·assetbundle
萘柰奈2 天前
Unity学习----【进阶】Addressables(二)--加载资源与打包及更新
学习·unity
lvcoc2 天前
unity 接入火山引擎API,包括即梦AI
windows·unity·ai·火山引擎
王家视频教程图书馆2 天前
2025年最新 unityHub游戏引擎开发2d手机游戏和桌面游戏教程
游戏·unity·游戏引擎