Skip to content

Instantly share code, notes, and snippets.

@jamesliu96
Created October 29, 2020 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesliu96/a8a36b8d5de947cf7eada9394bdbe6e3 to your computer and use it in GitHub Desktop.
Save jamesliu96/a8a36b8d5de947cf7eada9394bdbe6e3 to your computer and use it in GitHub Desktop.
[
{
"__type__": "cc.Prefab",
"_name": "",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false,
"readonly": false
},
{
"__type__": "cc.Node",
"_name": "console",
"_objFlags": 0,
"_parent": null,
"_children": [
{
"__id__": 2
},
{
"__id__": 6
},
{
"__id__": 14
}
],
"_active": true,
"_components": [
{
"__id__": 24
},
{
"__id__": 12
},
{
"__id__": 25
}
],
"_prefab": {
"__id__": 26
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 0,
"g": 0,
"b": 0,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 500,
"height": 300
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "line",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": false,
"_components": [
{
"__id__": 3
},
{
"__id__": 4
}
],
"_prefab": {
"__id__": 5
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 500,
"height": 25.2
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0,
"y": 1
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-250,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Label",
"_name": "text_line<Label>",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_useOriginalSize": false,
"_string": "placeholder",
"_N$string": "placeholder",
"_fontSize": 20,
"_lineHeight": 20,
"_enableWrapText": true,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_batchAsBitmap": false,
"_styleFlags": 0,
"_underlineHeight": 0,
"_N$horizontalAlign": 0,
"_N$verticalAlign": 1,
"_N$fontFamily": "Arial",
"_N$overflow": 3,
"_N$cacheMode": 0,
"_id": ""
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"alignMode": 1,
"_target": null,
"_alignFlags": 40,
"_left": 0,
"_right": 0,
"_top": 0,
"_bottom": 0,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 103.41,
"_originalHeight": 0,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "97hSOC309LKLRfUgoJfnLI",
"sync": false
},
{
"__type__": "cc.Node",
"_name": "scroll",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [
{
"__id__": 7
}
],
"_active": true,
"_components": [
{
"__id__": 11
},
{
"__id__": 21
},
{
"__id__": 22
}
],
"_prefab": {
"__id__": 23
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 79,
"g": 79,
"b": 79,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 2,
"height": 300
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 1,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
247,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "bar",
"_objFlags": 0,
"_parent": {
"__id__": 6
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 8
},
{
"__id__": 9
}
],
"_prefab": {
"__id__": 10
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 152,
"g": 152,
"b": 152,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 6,
"height": 10
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 1,
"y": 0
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
2,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 7
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 1,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 7
},
"_enabled": true,
"alignMode": 1,
"_target": null,
"_alignFlags": 40,
"_left": -2,
"_right": -2,
"_top": 0,
"_bottom": 0,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 10,
"_originalHeight": 0,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "273RvXwoRFmIM1JcFEYHiL",
"sync": false
},
{
"__type__": "cc.Scrollbar",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 6
},
"_enabled": true,
"_scrollView": {
"__id__": 12
},
"_touching": false,
"_opacity": 255,
"enableAutoHide": true,
"autoHideTime": 1,
"_N$handle": {
"__id__": 8
},
"_N$direction": 1,
"_id": ""
},
{
"__type__": "cc.ScrollView",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"horizontal": false,
"vertical": true,
"inertia": true,
"brake": 0.75,
"elastic": true,
"bounceDuration": 0.23,
"scrollEvents": [],
"cancelInnerEvents": true,
"_N$content": {
"__id__": 13
},
"content": {
"__id__": 13
},
"_N$horizontalScrollBar": null,
"_N$verticalScrollBar": {
"__id__": 11
},
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "content",
"_objFlags": 0,
"_parent": {
"__id__": 14
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 18
},
{
"__id__": 19
}
],
"_prefab": {
"__id__": 20
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 492,
"height": 0
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 1
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
125,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "view",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [
{
"__id__": 13
}
],
"_active": true,
"_components": [
{
"__id__": 15
},
{
"__id__": 16
}
],
"_prefab": {
"__id__": 17
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 492,
"height": 300
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-4,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Mask",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 14
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_spriteFrame": null,
"_type": 0,
"_segments": 64,
"_N$alphaThreshold": 0,
"_N$inverted": false,
"_id": ""
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 14
},
"_enabled": true,
"alignMode": 1,
"_target": null,
"_alignFlags": 45,
"_left": 0,
"_right": 8,
"_top": 0,
"_bottom": 0,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 240,
"_originalHeight": 250,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "c4nuFxxg9F8oneHKwjb6Jj",
"sync": false
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 13
},
"_enabled": true,
"alignMode": 1,
"_target": null,
"_alignFlags": 40,
"_left": 0,
"_right": 0,
"_top": 0,
"_bottom": -150,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 220,
"_originalHeight": 400,
"_id": ""
},
{
"__type__": "cc.Layout",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 13
},
"_enabled": true,
"_layoutSize": {
"__type__": "cc.Size",
"width": 492,
"height": 0
},
"_resize": 0,
"_N$layoutType": 2,
"_N$cellSize": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_N$startAxis": 0,
"_N$paddingLeft": 0,
"_N$paddingRight": 0,
"_N$paddingTop": 0,
"_N$paddingBottom": 0,
"_N$spacingX": 0,
"_N$spacingY": 0,
"_N$verticalDirection": 1,
"_N$horizontalDirection": 0,
"_N$affectedByScale": false,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "5cqNbgx9pIqrHFMRQiCBUx",
"sync": false
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 6
},
"_enabled": true,
"alignMode": 0,
"_target": null,
"_alignFlags": 37,
"_left": 350.07654921020657,
"_right": 3,
"_top": 0,
"_bottom": 0,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 0,
"_originalHeight": 237,
"_id": ""
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 6
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 1,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "45ABTnu0tMy7p1DWy2++uI",
"sync": false
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 1,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": ""
},
{
"__type__": "b4dc7UY3ftAapyA0BP9VCrZ",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__uuid__": "20f6a322-453f-4aab-bc0c-e6e378a35933"
},
"fileId": "98B9Njt3pKGYoWxXWpkiin",
"sync": false
}
]
{
"ver": "1.2.7",
"uuid": "20f6a322-453f-4aab-bc0c-e6e378a35933",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"readonly": false,
"subMetas": {}
}
const { ccclass, property } = cc._decorator;
const hijacks = {
log: cc.Color.WHITE,
info: cc.Color.WHITE,
warn: cc.Color.YELLOW,
error: cc.Color.RED,
clear: cc.Color.WHITE,
};
@ccclass
export default class Console extends cc.Component {
@property(cc.Boolean)
public fixBottom = true;
private nativeConsoleMethods: Record<string, Function> = {};
private scrollView: cc.ScrollView = null;
private lineTemplate: cc.Node = null;
private viewContent: cc.Node = null;
public onLoad() {
this.lineTemplate = this.node.getChildByName('line');
this.lineTemplate.active = false;
this.scrollView = this.node.getComponent(cc.ScrollView);
this.viewContent = this.node
.getChildByName('view')
.getChildByName('content');
this.viewContent.height = 0;
}
public onEnable() {
this.hijackConsole();
}
public start() {}
public update(dt: number) {}
public onDisable() {
this.restoreConsole();
}
public onDestroy() {
this.restoreConsole();
}
private hijackConsole() {
for (const type of Object.keys(hijacks)) {
if (console[type]) {
this.nativeConsoleMethods[type] = console[type];
console[type] = this.log(type);
}
}
}
private restoreConsole() {
for (const type of Object.keys(hijacks)) {
if (console[type] && this.nativeConsoleMethods[type]) {
console[type] = this.nativeConsoleMethods[type];
}
}
}
private log(type: string) {
this.unschedule(this.resetHeight);
if (type === 'clear') {
return () => {
this.nativeConsoleMethods['clear']();
this.viewContent.removeAllChildren();
this.viewContent.height = 0;
};
}
return (...args: any[]) => {
this.nativeConsoleMethods[type](...args);
this.appendLine(
args
.map((arg) => {
if (arg instanceof Error && arg.stack) {
return arg.stack;
} else if (typeof arg === 'object') {
try {
return JSON.stringify(arg);
} catch (e) {
return String(arg);
}
} else {
return String(arg);
}
})
.join('\t'),
hijacks[type]
);
};
}
private appendLine(text: string, color: cc.Color) {
const node = this.createLineNode(text, color);
this.viewContent.addChild(node);
this.scheduleOnce(this.resetHeight);
}
private createLineNode(text: string, color: cc.Color) {
const node = cc.instantiate(this.lineTemplate);
node.active = true;
node.color = color;
node.getComponent(cc.Label).string = text;
return node;
}
private resetHeight = () => {
this.viewContent.height = this.viewContent.children.reduce(
(acc, child) => acc + child.height,
0
);
if (this.fixBottom) {
this.scrollView.scrollToBottom(0.1);
}
};
}
{
"ver": "1.0.8",
"uuid": "b4dc7518-ddfb-406a-9c80-d013fd542ad9",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment