cocos creator 3.3.0 examples

This commit is contained in:
King Wang
2021-09-02 00:27:24 +08:00
parent 4bfe797a89
commit c21445548e
58 changed files with 11943 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "7f656590-e733-47c8-9a29-8fc1efd57cc2",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
{
"ver": "1.1.30",
"importer": "prefab",
"imported": true,
"uuid": "da99e95a-3939-4ba1-8ce5-c60a6cd41fad",
"files": [
".json"
],
"subMetas": {},
"userData": {
"syncNodeName": "ChatBox"
}
}

View File

@@ -0,0 +1,105 @@
import { Component, EditBox, instantiate, Label, Prefab, ScrollView, _decorator } from 'cc';
import { MINIGAME } from 'cc/env';
import { BaseWsClient } from 'tsrpc-base-client';
import { WsClient as WsClientBrowser } from 'tsrpc-browser';
import { WsClient as WsClientMiniapp } from 'tsrpc-miniapp';
import { MsgChat } from '../../scripts/shared/protocols/MsgChat';
import { serviceProto, ServiceType } from '../../scripts/shared/protocols/serviceProto';
import { ChatBoxMsgItem } from './prefabs/ChatBox_MsgItem/ChatBox_MsgItem';
const { ccclass, property } = _decorator;
@ccclass('ChatBox')
export class ChatBox extends Component {
@property
roomName = '';
// Prefabs
@property(Prefab)
prefabMsgItem!: Prefab;
// Nodes
@property(Label)
labelTitle!: Label;
@property(ScrollView)
msgScroll!: ScrollView;
@property(EditBox)
inputSend!: EditBox;
client!: BaseWsClient<ServiceType>;
onLoad() {
// Create client by platform
if (MINIGAME) {
this.client = new WsClientMiniapp(serviceProto, {
server: 'ws://127.0.0.1:3000',
logger: console
})
}
else {
this.client = new WsClientBrowser(serviceProto, {
server: 'ws://127.0.0.1:3000',
logger: console
})
}
// Connect at startup
this.connect();
// Listen Msg
this.client.listenMsg('Chat', v => { this.onChatMsg(v) })
// When disconnected
this.client.flows.postDisconnectFlow.push(v => {
// Retry after 2 seconds
this.labelTitle.string = `🔴 Disconnected`;
setTimeout(() => {
this.connect();
}, 2000)
return v;
})
}
async connect(): Promise<void> {
this.labelTitle.string = `🟡 Connecting...`;
let res = await this.client.connect();
if (!res.isSucc) {
this.labelTitle.string = `🔴 Disconnected`;
// Retry after 2 seconds
await new Promise(rs => { setTimeout(rs, 2000) });
await this.connect();
}
this.labelTitle.string = '🟢 ' + this.roomName;
}
async send() {
if (!this.inputSend.string) {
return;
}
let ret = await this.client.callApi('Send', {
content: this.inputSend.string
});
// Error
if (!ret.isSucc) {
alert(ret.err.message);
return;
}
// Success
this.inputSend.string = '';
}
onChatMsg(msg: MsgChat) {
let node = instantiate(this.prefabMsgItem);
this.msgScroll.content!.addChild(node);
node.getComponent(ChatBoxMsgItem)!.options = {
msg: msg
}
this.msgScroll.scrollToBottom(0.2);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.22",
"importer": "typescript",
"imported": true,
"uuid": "e256ef5e-ec1a-413f-a72b-a3149113fbd6",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "f67e9512-1750-46bc-914f-062245e06148",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "b51ae285-508e-4c3d-955c-02e12f6c2289",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,548 @@
[
{
"__type__": "cc.Prefab",
"_name": "",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false
},
{
"__type__": "cc.Node",
"_name": "ChatBox_MsgItem",
"_objFlags": 0,
"_parent": null,
"_children": [
{
"__id__": 2
},
{
"__id__": 10
}
],
"_active": true,
"_components": [
{
"__id__": 18
},
{
"__id__": 20
},
{
"__id__": 22
},
{
"__id__": 24
}
],
"_prefab": {
"__id__": 26
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": -95.79999999999998,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "message",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 3
},
{
"__id__": 5
},
{
"__id__": 7
}
],
"_prefab": {
"__id__": 9
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 20,
"y": 15.11999999999999,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"__prefab": {
"__id__": 4
},
"_contentSize": {
"__type__": "cc.Size",
"width": 616,
"height": 81.35999999999999
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "c68UOAlNhN171Umca6yVvF"
},
{
"__type__": "cc.Label",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"__prefab": {
"__id__": 6
},
"_visFlags": 0,
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 111,
"g": 111,
"b": 111,
"a": 255
},
"_string": "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
"_horizontalAlign": 0,
"_verticalAlign": 0,
"_actualFontSize": 24,
"_fontSize": 24,
"_fontFamily": "Arial",
"_lineHeight": 36,
"_overflow": 3,
"_enableWrapText": true,
"_font": null,
"_isSystemFontUsed": true,
"_isItalic": false,
"_isBold": false,
"_isUnderline": false,
"_underlineHeight": 2,
"_cacheMode": 0,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "2frm37uaJHQr0AEEaYyM82"
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"__prefab": {
"__id__": 8
},
"_alignFlags": 40,
"_target": null,
"_left": 20,
"_right": 20,
"_top": 0,
"_bottom": 0,
"_horizontalCenter": 0,
"_verticalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 640,
"_originalHeight": 0,
"_alignMode": 2,
"_lockFlags": 0,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "0d0EfiWOlKKJ7mcNKs1LZa"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "8dI1EQZzVFsJ+aRAFdts43"
},
{
"__type__": "cc.Node",
"_name": "time",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 11
},
{
"__id__": 13
},
{
"__id__": 15
}
],
"_prefab": {
"__id__": 17
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 565.29,
"y": -40.68,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
},
"_enabled": true,
"__prefab": {
"__id__": 12
},
"_contentSize": {
"__type__": "cc.Size",
"width": 141.42,
"height": 30.24
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "c68UOAlNhN171Umca6yVvF"
},
{
"__type__": "cc.Label",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
},
"_enabled": true,
"__prefab": {
"__id__": 14
},
"_visFlags": 0,
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 75,
"g": 128,
"b": 187,
"a": 255
},
"_string": "下午10:57:53",
"_horizontalAlign": 1,
"_verticalAlign": 1,
"_actualFontSize": 24,
"_fontSize": 24,
"_fontFamily": "Arial",
"_lineHeight": 24,
"_overflow": 0,
"_enableWrapText": true,
"_font": null,
"_isSystemFontUsed": true,
"_isItalic": false,
"_isBold": false,
"_isUnderline": false,
"_underlineHeight": 2,
"_cacheMode": 0,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "2frm37uaJHQr0AEEaYyM82"
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
},
"_enabled": true,
"__prefab": {
"__id__": 16
},
"_alignFlags": 32,
"_target": null,
"_left": 0,
"_right": 20,
"_top": 0,
"_bottom": 0,
"_horizontalCenter": 0,
"_verticalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 0,
"_originalHeight": 0,
"_alignMode": 2,
"_lockFlags": 0,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "d5mZ4hY09Hg5vvBWIQESW/"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "f1/rIjCeNEyK90oXy196pl"
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 19
},
"_contentSize": {
"__type__": "cc.Size",
"width": 656,
"height": 151.59999999999997
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0.5
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "fdYt2dfxlHq55xokljDdID"
},
{
"__type__": "cc.Layout",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 21
},
"_resizeMode": 1,
"_layoutType": 2,
"_cellSize": {
"__type__": "cc.Size",
"width": 40,
"height": 40
},
"_startAxis": 0,
"_paddingLeft": 0,
"_paddingRight": 0,
"_paddingTop": 20,
"_paddingBottom": 20,
"_spacingX": 0,
"_spacingY": 0,
"_verticalDirection": 1,
"_horizontalDirection": 0,
"_constraint": 0,
"_constraintNum": 2,
"_affectedByScale": false,
"_isAlign": false,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "2aP3wVb2pMEo5d4J+YmiTl"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 23
},
"_visFlags": 0,
"_customMaterial": null,
"_srcBlendFactor": 2,
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_spriteFrame": {
"__uuid__": "0638b78e-5b91-4912-b2b8-06eed04deede@f9941",
"__expectedType__": "cc.SpriteFrame"
},
"_type": 0,
"_fillType": 0,
"_sizeMode": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_useGrayscale": false,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "1bGiG94TxIm65HBL1dQZbC"
},
{
"__type__": "a9052CyCzdOc4dnnkAaz7cY",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 25
},
"labelMessage": {
"__id__": 5
},
"labelTime": {
"__id__": 13
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "e4fisXaplL/qQ54HD/3H/H"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "87UoyxPYRKrJRuRW07pDnM"
}
]

View File

@@ -0,0 +1,13 @@
{
"ver": "1.1.30",
"importer": "prefab",
"imported": true,
"uuid": "5393b4df-3fd4-4342-a3c1-b2dc4763d062",
"files": [
".json"
],
"subMetas": {},
"userData": {
"syncNodeName": "ChatBox_MsgItem"
}
}

View File

@@ -0,0 +1,30 @@
import { Component, Label, _decorator } from 'cc';
import { MsgChat } from '../../../../scripts/shared/protocols/MsgChat';
const { ccclass, property } = _decorator;
export interface ChatBoxMsgItemOptions {
msg: MsgChat
}
@ccclass('ChatBoxMsgItem')
export class ChatBoxMsgItem extends Component {
@property(Label)
labelMessage!: Label;
@property(Label)
labelTime!: Label;
private _options!: ChatBoxMsgItemOptions;
public get options(): ChatBoxMsgItemOptions {
return this._options;
}
public set options(v: ChatBoxMsgItemOptions) {
this._options = v;
this.labelMessage.string = v.msg.content;
this.labelTime.string = v.msg.time.toLocaleTimeString();
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.22",
"importer": "typescript",
"imported": true,
"uuid": "a90520b2-0b37-4e73-8767-9e401acfb718",
"files": [],
"subMetas": {},
"userData": {}
}