Skip to content

Commit

Permalink
debug class replicated
Browse files Browse the repository at this point in the history
  • Loading branch information
wyb10a10 committed Aug 15, 2023
1 parent f720cee commit 14d3e41
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
4 changes: 3 additions & 1 deletion assets/Scene/example_sync_scene.scene
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
},
"_enabled": true,
"__prefab": null,
"cubeName": "cube",
"_id": "c23znRhDZPRa49AI40a2/T"
},
{
Expand All @@ -424,7 +425,7 @@
"__prefab": null,
"prefabs": [
{
"__uuid__": "b4fbe877-80a3-4f7c-969c-fac3aa757846",
"__uuid__": "fc4b58c9-e6a4-4392-9c90-1f10d66e95b4",
"__expectedType__": "cc.Prefab"
},
{
Expand Down Expand Up @@ -600,6 +601,7 @@
},
"_enabled": true,
"__prefab": null,
"cubeName": "cube",
"_id": "21I+sEcu1Bs5F0+PxRySKo"
},
{
Expand Down
9 changes: 9 additions & 0 deletions assets/Script/example/sync/SyncCube.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Component, _decorator } from "cc";
import { replicated } from "../../sync/SyncUtil";
import { CCString } from "cc";

const { ccclass, property } = _decorator;

@ccclass
export default class SyncCube extends Component {
// 定义一个字符串类型的同步属性
@replicated()
@property({type : CCString})
public cubeName: string = "cube";

onLoad() {
// 随机生成一个cubeName,后面接的随机数为0-1000的整数
this.cubeName = "cube" + Math.floor(Math.random() * 1000);
console.log("cubeName: " + this.cubeName);
}
// update (dt) {}
}
2 changes: 2 additions & 0 deletions assets/Script/sync/NodeReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export default class NodeReplicator implements IReplicator {
const replicator = createReplicator(component);
if (replicator) {
this.replicators.set(key, replicator);
console.log(`NodeReplicator scanNode, key: ${key}`);
} else {
console.error(`NodeReplicator scanNode error, key: ${key}`);
}
}
console.log(`NodeReplicator scanNode, component: ${component.constructor.name}`);
}

for (const child of node.children) {
Expand Down
3 changes: 2 additions & 1 deletion assets/Script/sync/SyncUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ function makeObjectReplicatedMark(cls: any, option?: ObjectReplicatedOption): Re
export function replicated(option?: ReplicatedOption) {
// 真正的装饰器
return (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => {
return makePropertyReplicatedMark(target, propertyKey, descriptor, option);
let cls = target.constructor ? target.constructor : target;
return makePropertyReplicatedMark(cls, propertyKey, descriptor, option);
};
}

Expand Down
23 changes: 22 additions & 1 deletion assets/resources/Prefab/SyncCapsule.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
},
{
"__id__": 5
},
{
"__id__": 7
}
],
"_prefab": {
"__id__": 7
"__id__": 9
},
"_lpos": {
"__type__": "cc.Vec3",
Expand Down Expand Up @@ -136,6 +139,24 @@
"__type__": "cc.CompPrefabInfo",
"fileId": "c4t1Er5TVHMZZoErUvdfHi"
},
{
"__type__": "38e43XxKnZFLbKI7dJoX0cG",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 8
},
"cubeName": "cube",
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "a6KpcLc2FFUoAOlGuxScvJ"
},
{
"__type__": "cc.PrefabInfo",
"root": {
Expand Down

0 comments on commit 14d3e41

Please sign in to comment.