Custom Inspector Panel

Developers with custom inspector needs are advised to first refer to the documentation Decorator, and if that document meets the requirements, it is recommended that the methods in that document be used first.

The Inspector panel, the module in Cocos Creator that displays the currently selected state, provides some basic extension capabilities.

In Inspector panel, two levels of data are defined:

  1. the main type of the selected object
  2. the sub-data types contained in the content when rendering the content of the main type

When a node or an asset is selected in the Hierarchy/Assets Panel, Cocos Creator broadcasts the message that the object is selected. When the Inspector panel receives the message, it checks the type of the selected object, e.g. if the selected object is a node, then the type is node.

For both types, two types of renderers are allowed to be registered.

  1. the main type renderer
  2. When the main type renderer receives data to start rendering, it allows subtype renderers to be attached

The selected object in the example is node, and node carries multiple components, so the main type is node and the subtype is component.

After the Inspector receives the broadcast message for the selected object, it first determines the type, and then the Inspector takes the received data (the uuid of the object) and passes it to the node renderer, handing over the rendering privileges completely.

And in the node renderer, the rendering privileges for that region are handed over to the subtype renderer when rendering to each component, according to its own implementation. For the most part, we don’t need to be concerned about this. Let’s take a look at a few common ways to customize it.

Customizing Component Rendering

The default component renderer sometimes doesn’t meet our needs, so we need to customize a component’s rendering method.

First, create a new script component CustomLabelComponent.ts in the project and add a string property with the following content:

  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('CustomLabelComponent')
  4. export class CustomLabelComponent extends Component {
  5. @property
  6. label = '';
  7. start () {
  8. }
  9. }

Drag the component to the node and you will see an input box on the component.

Create a new extension and register the following contributions.inspector information in the extension’s package.json:

  1. {
  2. "contributions": {
  3. "inspector": {
  4. "section": {
  5. "node": {
  6. "CustomLabelComponent": "./dist/contributions/inspector/comp-label.js"
  7. }
  8. }
  9. }
  10. }
  11. }

Automatic Rendering

Write a src/contributions/inspector/comp-label.ts file with the following contents:

  1. 'use strict';
  2. type Selector<$> = { $: Record<keyof $, any | null> }
  3. export const template = `
  4. <ui-prop type="dump" class="test"></ui-prop>
  5. `;
  6. export const $ = {
  7. test: '.test',
  8. };
  9. export function update(this: Selector<typeof $> & typeof methods, dump: any) {
  10. // Use ui-porp to auto-render, set the type of prop to dump
  11. // render pass in a dump data to be able to automatically render the corresponding interface
  12. // Auto-rendered interface can automatically commit data after modification
  13. this.$.test.render(dump.label.value);
  14. }
  15. export function ready(this: Selector<typeof $> & typeof methods) {}

After compiling and refreshing the extension, we can see that the rendering of the CustomLabelComponent component has been taken over.

Note: Each ui-prop corresponds to one property, to display multiple properties you need to define multiple ui-props.

Manual Rendering

In the above auto-rendering example, we used a special ui-prop of type dump for rendering data submission, which allows us to quickly take over the rendering of the component, but if we face some extreme cases but it is difficult to handle some details, we can switch to manual rendering mode, the code is as follows:

  1. 'use strict';
  2. type Selector<$> = { $: Record<keyof $, any | null> }
  3. export const template = `
  4. <!-- Elements to help submit data -->
  5. <ui-prop type="dump" class="test"></ui-prop>
  6. <!-- The actual rendered elements -->
  7. <ui-label class="label"></ui-label>
  8. <ui-input class="test-input"></ui-input>
  9. `;
  10. export const $ = {
  11. label:'.label',
  12. test: '.test',
  13. testInput: '.test-input',
  14. };
  15. type PanelThis = Selector<typeof $> & { dump: any };
  16. export function update(this: PanelThis, dump: any) {
  17. // Cache 'dump' data, please hang on 'this', otherwise there may be problems when multiple opening
  18. this.dump = dump;
  19. // Pass the 'dump' data to the prop element that helped submit the data
  20. this.$.test.dump = dump.value.label;
  21. // Update the data on the input element responsible for 'input' and display
  22. this.$.testInput.value = dump.value.label.value;
  23. this.$.label.value = dump.value.label.name;
  24. }
  25. export function ready(this: PanelThis) {
  26. // Listen for commit events on the input, update the dump data when the input commits data, and use prop to send change-dump events
  27. this.$.testInput.addEventListener('confirm', () => {
  28. this.dump.value.label.value = this.$.testInput.value;
  29. this.$.test.dispatch('change-dump');
  30. });
  31. }

Manual rendering mode still requires data to be submitted via a ui-prop element of type dump. However, the html layout in the template is a completely free-handed content that can be displayed in very complex ways depending on the requirements.

Customizing Asset rendering

When a file is selected in the Assets panel, the Inspector panel will display the important properties of the currently selected file, or we can customize the rendering if the default display does not meet the requirements.

Add the contributions.section.asset field to package.json and define a custom rendering script for the corresponding asset type as follows:

  1. {
  2. "contributions": {
  3. "inspector": {
  4. "section": {
  5. "asset": {
  6. "effect": "./dist/contributions/inspector/asset-effect.js"
  7. }
  8. }
  9. }
  10. }
  11. }

effect indicates that we want to customize the rendering of the asset inspector panel for the Cocos Effect (*.effect) file type. Common asset file types are as follows.

  • scene - Scene files
  • typescript - TypeScript script files
  • prefab - prefab files
  • fbx - FBX files
  • material - material files
  • directory - folder
  • image - image files

You can get the type definition of the file by looking at the importer field in the *.meta corresponding to the file.

Next, create a new src/contributions/inspector/asset-effect.ts script file in the extension directory and write the following code:

  1. 'use strict';
  2. interface Asset {
  3. displayName: string;
  4. file: string;
  5. imported: boolean;
  6. importer: string;
  7. invalid: boolean;
  8. isDirectory: boolean;
  9. library: {
  10. [extname: string]: string;
  11. };
  12. name: string;
  13. url: string;
  14. uuid: string;
  15. visible: boolean;
  16. subAssets: {
  17. [id: string]: Asset;
  18. };
  19. }
  20. interface Meta {
  21. files: string[];
  22. imported: boolean;
  23. importer: string;
  24. subMetas: {
  25. [id: string]: Meta;
  26. };
  27. userData: {
  28. [key: string]: any;
  29. };
  30. uuid: string;
  31. ver: string;
  32. }
  33. type Selector<$> = { $: Record<keyof $, any | null> } & { dispatch(str: string): void, assetList: Asset[], metaList: Meta[] };
  34. export const $ = {
  35. 'test': '.test',
  36. };
  37. export const template = `
  38. <ui-prop>
  39. <ui-label slot="label">Test</ui-label>
  40. <ui-checkbox slot="content" class="test"></ui-checkbox>
  41. </ui-prop>
  42. `;
  43. type PanelThis = Selector<typeof $>;
  44. export function update(this: PanelThis, assetList: Asset[], metaList: Meta[]) {
  45. this.assetList = assetList;
  46. this.metaList = metaList;
  47. this.$.test.value = metaList[0].userData.test || false;
  48. };
  49. export function ready(this: PanelThis) {
  50. this.$.test.addEventListener('confirm', () => {
  51. this.metaList.forEach((meta: any) => {
  52. // Modify the data in the corresponding meta
  53. meta.userData.test = !!this.$.test.value;
  54. });
  55. // The Assets panel is modifying the meta file of the asset, not the dump data, so the event sent is not the same as the component property modification
  56. this.dispatch('change');
  57. });
  58. };
  59. export function close(his: PanelThis, ) {
  60. // TODO something
  61. };

After compiling and refreshing the extension, go back to the Cocos Creator editor interface, select one of the Cocos Effect files, and you can find an additional Test checkbox at the bottom of the Inspector panel.

Note: Multiple extensions register data side by side. If a Component/Asset already has a custom renderer, any custom renderers registered again will be appended to it. If a Component/Asset does not have a custom renderer built-in and uses the default renderer, then when the extension registers a custom renderer, it takes over the rendered content completely.