目录— 个文件
⌕
目录加载中…
…/utility/UtilityAction/UtilityAction.ts
1import { Node, Enum } from "cc";
2import BaseAction, { BaseAniConfigType, CommonAniType } from "./contents/baseAction";
3import FadeAction, { FadeAniUseData } from "./contents/simple/fadeAction";
4import MoveAction, { MoveAniUseData } from "./contents/simple/moveAction";
5import ScaleAction, { ScaleAniUseData } from "./contents/simple/scaleAction";
6
7export enum ActionType {
8 fade = "fade",
9 scale = "scale",
10 move = "move",
11}
12Enum(ActionType)
13
14/**
15 * 默认配置
16 */
17const defaultConfig = {
18 type: ActionType.fade,
19 duration: 0.5,
20};
21
22/**
23 * 可用动画和配置数据
24 */
25const allActionMap: { [key in keyof typeof ActionType]: { class: typeof FadeAction | typeof ScaleAction | typeof MoveAction, data: FadeAniUseData | ScaleAniUseData | MoveAniUseData | undefined } } = {
26 fade: { class: FadeAction, data: undefined as FadeAniUseData | undefined },
27 scale: { class: ScaleAction, data: undefined as ScaleAniUseData | undefined },
28 move: { class: MoveAction, data: undefined as MoveAniUseData | undefined },
29}
30
31export type ComAniType = keyof typeof ActionType;
32type AniConfigType = Omit<BaseAniConfigType, "useData">
33type UseConfigType = { type?: ComAniType } & BaseAniConfigType & ComAniType;
34
35
36/** 通用动画 */
37export class UtilityAction {
38
39 /** 播放显示动画 */
40 static async show<T extends ComAniType>(node: Node, config?:
41 ({ type?: T } & AniConfigType & { useData?: typeof allActionMap[T]["data"] })
42 ) {
43
44 await this._play(node, "show", config as any);
45 }
46 /** 播放改变动画 */
47 static async change<T extends ComAniType>(node: Node, config?:
48 ({ type?: T } & AniConfigType & { useData?: typeof allActionMap[T]["data"] })
49 ) {
50 await this._play(node, "change", config as any);
51 }
52 /** 播放隐藏动画 */
53 static async hide<T extends ComAniType>(node: Node, config?:
54 ({ type?: T } & AniConfigType & { useData?: typeof allActionMap[T]["data"] })
55 ) {
56 await this._play(node, "hide", config as any);
57 }
58
59 // #region 私有方法
60 private static async _play(node: Node, action: CommonAniType, config?: UseConfigType) {
61 const actionClass = this._getClass(config?.type);
62 actionClass.setConfig(config, defaultConfig);
63 return await actionClass.play(node, action, config?.onFinish, config?.onChange);
64 }
65
66 private static _getClass(type?: ComAniType): BaseAction {
67 return new allActionMap[type || defaultConfig.type].class;
68 }
69 private static _getType(type?: ComAniType) {
70 return type || defaultConfig.type;
71 }
72 // #endregion
73
74}
讲解75 行
通用动画播放入口人工通读
UtilityAction.ts
把淡入淡出、缩放、位移三种动画封装成 show、change、hide 三个异步入口,按配置中的 type 选择实现类。每次播放都新建动作实例并合并默认时长,返回的 Promise 在动画完成时 resolve。
先看 ActionType 与 allActionMap 的映射,再读 show、change、hide 如何转调 _play,最后看 _getClass 每次都新建实例。
特殊处理与易错点
- 易错点setConfig 写入的是实例字段 currentConfig,但 _getClass 每次都 new,配置不会跨调用累积。L62
- 设计本文件默认时长 0.5 秒会覆盖 baseAction 内置的 0.3 秒,不传 duration 时以 0.5 秒为准。L17
- 设计_getClass 每次返回新实例,同一节点的并发动画靠基类的 uuid 表互斥。L67
17注释默认配置
25注释可用动画和配置数据
37注释UtilityAction:UtilityAction 是「动作工具集」目录下的实现/辅助。辅助实现文件,配合同目录的入
40方法按配置类型播放显示动画并等待完成,节点会从隐藏态过渡到显示态。
47方法播放先隐藏再显示的组合动画,两段各占一半时长,切换时触发 onChange。
53方法按配置类型播放隐藏动画并等待完成,节点进入隐藏态。
60方法取动作类并合并配置后调用 play,把 onFinish 与 onChange 交给动作实例。
66方法根据类型或默认的 fade 从映射表实例化对应动作类。
69方法返回传入的动画类型,缺省时返回默认的 fade,当前文件内未被调用。