控制自己组件的执行顺序 (SDK for Cocos Creator)
最終更新: 2023年3月14日
本节说明为用户自己的组件控制其他Cubism组件的执行顺序的步骤。
以下说明以追加到已执行“载入SDK”的项目为前提。
概述
Cubism SDK for Cocos Creator的Original Workflow组件中有限制执行顺序的项目。
在Cubism SDK for Cocos Creator中,可以通过CubismUpdateController进行控制,上述组件的执行顺序据此进行控制。
CubismUpdateController控制的组件是附加到Cubism模型Prefab根中的组件。
通过使用CubismUpdateController,还可以控制用户自己组件的执行顺序。
本节以设置以下组件的执行顺序控制的步骤为例进行说明。
@ccclass('CubismExampleController')
export class CubismExampleController extends Component {
start() {
// CubismExampleController的原始化处理
}
lateUpdate(deltaTime: number) {
// CubismExampleController的更新处理
}
}
1. 将组件附加到Prefab
将CubismExampleController附加到置入Hierarchy的Prefab根的Node。
如果Prefab尚未以OW格式载入,则还要附加CubismUpdateController。

2. 在组件中实装ICubismUpdatable
在控制执行顺序的组件上实装ICubismUpdatable接口。
CubismUpdateController在执行时获取实装ICubismUpdatable的组件并控制它们的执行顺序。
@ccclass('CubismExampleController')
export class CubismExampleController extends Component implements ICubismUpdatable {
bindedOnLateUpdate: ICubismUpdatable.CallbackFunction;
get executionOrder(): number {
throw new Error('Method not implemented.');
}
get needsUpdateOnEditing(): boolean {
throw new Error('Method not implemented.');
}
get hasUpdateController(): boolean {
throw new Error('Method not implemented.');
}
set hasUpdateController(value: boolean) {
throw new Error('Method not implemented.');
}
readonly [ICubismUpdatable.SYMBOL]: typeof ICubismUpdatable.SYMBOL;
protected start() {
// CubismExampleController的原始化处理
}
protected lateUpdate(deltaTime: number) {
// CubismExampleController的更新处理
}
}
这里实装的ICubismUpdatable接口如下。
interface ICubismUpdatable {
readonly [ICubismUpdatable.SYMBOL]: typeof ICubismUpdatable.SYMBOL;
readonly bindedOnLateUpdate: ICubismUpdatable.CallbackFunction;
get executionOrder(): number;
get needsUpdateOnEditing(): boolean;
get hasUpdateController(): boolean;
set hasUpdateController(value: boolean);
}
executionOrder是决定该组件执行顺序的值。
这个值越小,则相比其他组件被调用得越早。
在CubismUpdateExecutionOrder中描述了为SDK中包含的组件设置的值。
hasUpdateController是当实装ICubismUpdatable的组件未附加到CubismUpdateController时,从Cocos Creator的Event函数调用的标志。
namespace CubismUpdateExecutionOrder {
export const CUBISM_FADE_CONTROLLER = 100;
export const CUBISM_PARAMETER_STORE_SAVE_PARAMETERS = 150;
export const CUBISM_POSE_CONTROLLER = 200;
export const CUBISM_EXPRESSION_CONTROLLER = 300;
export const CUBISM_EYE_BLINK_CONTROLLER = 400;
export const CUBISM_MOUTH_CONTROLLER = 500;
export const CUBISM_HARMONIC_MOTION_CONTROLLER = 600;
export const CUBISM_LOOK_CONTROLLER = 700;
export const CUBISM_PHYSICS_CONTROLLER = 800;
export const CUBISM_RENDER_CONTROLLER = 10000;
export const CUBISM_MASK_CONTROLLER = 10100;
3. 使组件与CubismUpdateController兼容
修改CubismExampleController如下。
import { _decorator, Component, Node } from 'cc';
import CubismUpdateController from '../../extensions/Live2DCubismSdkForCocosExtension/static/assets/Framework/CubismUpdateController';
import ICubismUpdatable from '../../extensions/Live2DCubismSdkForCocosExtension/static/assets/Framework/ICubismUpdatable';
const { ccclass, property } = _decorator;
@ccclass('CubismExampleController')
export class CubismExampleController extends Component implements ICubismUpdatable {
bindedOnLateUpdate: ICubismUpdatable.CallbackFunction;
// 该组件的执行顺序
get executionOrder(): number {
return 150;
}
// Scene不执行时是否控制执行顺序
get needsUpdateOnEditing(): boolean {
return false;
}
// 执行顺序是否受控
@property({ serializable: false, visible: false })
private _hasUpdateController: boolean = false;
get hasUpdateController(): boolean {
return this._hasUpdateController;
}
set hasUpdateController(value: boolean) {
this._hasUpdateController = value;
}
readonly [ICubismUpdatable.SYMBOL]: typeof ICubismUpdatable.SYMBOL;
protected start() {
// CubismExampleController的原始化处理
// 检查CubismUpdateController是否附加到模型的Prefab
this.hasUpdateController = this.getComponent(CubismUpdateController) != null;
}
protected lateUpdate(deltaTime: number) {
// 如果CubismUpdateController没有附加,则从CubismExampleController本身的Event函数执行更新处理
if (!this.hasUpdateController) {
this.onLateUpdate(deltaTime);
}
}
// 控制执行顺序的更新函数
public onLateUpdate(deltaTime: number) {
// CubismExampleController的更新处理
}
}
将lateUpdate()执行的更新处理移动到CubismUpdateController调用的onLateUpdate()中。
这样就完成了控制执行顺序的设置。
当您将此脚本附加到Cubism模型的Prefab并执行场景时,CubismUpdateController将调用此脚本的更新处理。