Unity2022中创建动画 Animation(旧方法)
在场景中添加一个 Cube。,并添加组件 Animation。说直白点就是CSS3中的关键帧动画,感觉还没CSS3中的关键帧动画灵活。

添加Animation动画




添加一段脚本控制动画,按下鼠标左键和右键自动切换动画。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AnimationController : MonoBehaviour { private Animation anim; void Start() { anim = GetComponent<Animation>(); foreach (AnimationState astate in anim) { Debug.Log("名称:"+anim.name); } // 设置默认动画为 right //if (anim != null && anim["left"] != null) //{ // anim.clip = anim["left"].clip; //} } void Update() { if (Input.GetMouseButtonDown(0)) { if (anim["right"] != null) { anim.Play("right"); Debug.Log("播放 right 动画"); } } if (Input.GetMouseButtonDown(1)) { if (anim["left"] != null) { anim.Play("left"); Debug.Log("播放 left 动画"); } } } }
浙公网安备 33010602011771号