Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checkDepComponentInEditor decorator #17579

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/core/data/class-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
export { uniquelyReferenced } from './decorators/serializable';
export { ccclass } from './decorators/ccclass';
export { property } from './decorators/property';
export { requireComponent, executionOrder, disallowMultiple } from './decorators/component';
export { requireComponent, checkDepComponentInEditor, executionOrder, disallowMultiple } from './decorators/component';
export { executeInEditMode, menu, playOnFocus, inspector, icon, help } from './decorators/editable';
export { type, integer, float, boolean, string } from './decorators/type';

Expand Down
24 changes: 22 additions & 2 deletions cocos/core/data/decorators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
*/

import { DEV } from 'internal:constants';
import { CCClass } from '../class';
import { makeEditorClassDecoratorFn, makeSmartEditorClassDecorator, emptySmartClassDecorator } from './utils';
import {
makeEditorClassDecoratorFn,
makeSmartEditorClassDecorator,
emptySmartClassDecorator,
emptyDecoratorFn,
} from './utils';

/**
* @en Declare that the current component relies on another type of component.
Expand All @@ -45,6 +49,22 @@ import { makeEditorClassDecoratorFn, makeSmartEditorClassDecorator, emptySmartCl
*/
export const requireComponent: (requiredComponent: Function | Function[]) => ClassDecorator = makeEditorClassDecoratorFn('requireComponent');

/**
* @en Checks for dependent components before adding a component declared as CCClass. If the dependent component does not exist, prompt for it
* @zh 为声明为 CCClass 的组件进行添加前检查依赖组件。如果依赖的组件不存在,进行提示
* @param checkDepComponentInEditor
* @example
* ```ts
* import { _decorator, Sprite, Component } from cc;
* import { ccclass, checkDepComponentInEditor } from _decorator;
*
* @ccclass
* @checkDepComponentInEditor('cc.Sprite')
* class SpriteCtrl extends Component {}
* ```
*/
export const checkDepComponentInEditor: (component: string | string[]) => ClassDecorator = DEV ? makeEditorClassDecoratorFn('checkDepComponentInEditor') : emptyDecoratorFn;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a return type annotation for clarity


/**
* @en Set the component priority, it decides at which order the life cycle functions of components will be invoked. Smaller priority gets invoked before larger priority.
* This will affect `onLoad`, `onEnable`, `start`, `update` and `lateUpdate`, but `onDisable` and `onDestroy` won't be affected.
Expand Down
3 changes: 3 additions & 0 deletions cocos/scene-graph/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@
* 如果回调任务已调度,那么将不会重复调度它,只会更新时间间隔参数。
* @param callback The callback function of the task
* @param interval The time interval between each invocation
* @param repeat The repeat count of this task, the task will be invoked (repeat + 1) times, use [[macro.REPEAT_FOREVER]] to repeat a task forever

Check warning on line 442 in cocos/scene-graph/component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 153. Maximum allowed is 150
* @param delay The delay time for the first invocation, Unit: s
* @example
* ```ts
Expand Down Expand Up @@ -768,6 +768,9 @@
cls._executeInEditMode = !!val;
break;

case 'checkDepComponentInEditor':
cls._checkComponentInEditor = val;
break;
case 'playOnFocus':
if (val) {
const willExecuteInEditMode = ('executeInEditMode' in props) ? props.executeInEditMode : cls._executeInEditMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ import {
fillRequiredPipelineSettings,
} from './builtin-pipeline-types';

const { ccclass, disallowMultiple, executeInEditMode, menu, property, requireComponent, type } = _decorator;
const { ccclass, disallowMultiple, executeInEditMode, menu, property, checkDepComponentInEditor, requireComponent, type } = _decorator;

@ccclass('BuiltinPipelineSettings')
@menu('Rendering/BuiltinPipelineSettings')
@requireComponent(Camera)
@checkDepComponentInEditor('cc.Camera')
@disallowMultiple
@executeInEditMode
export class BuiltinPipelineSettings extends Component {
Expand Down
4 changes: 4 additions & 0 deletions editor/i18n/en/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ module.exports = {
},

blockInputEventsTip: 'This component will block all input events, preventing the input from penetrating to other nodes below the screen, typically for the background of the top-level UI of the screen.',

builtin_pipeline_settings: {
depend_component_warn: 'This component can only be added to a node that contains {dependent} component.',
},
},
};
4 changes: 4 additions & 0 deletions editor/i18n/zh/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ module.exports = {
},

blockInputEventsTip: '该组件将拦截所有输入事件,防止输入穿透到屏幕下方的其它节点,一般用于屏幕上层 UI 的背景。',

builtin_pipeline_settings: {
depend_component_warn: '该组件只能添加到包含 {dependent} 组件的节点中。',
},
},
};
Loading