From 14d3e41711004bbe710d1701a1bf72f6ef3e7719 Mon Sep 17 00:00:00 2001 From: wyb10a10 Date: Tue, 15 Aug 2023 23:26:57 +0800 Subject: [PATCH] debug class replicated --- assets/Scene/example_sync_scene.scene | 4 +++- assets/Script/example/sync/SyncCube.ts | 9 +++++++++ assets/Script/sync/NodeReplicator.ts | 2 ++ assets/Script/sync/SyncUtil.ts | 3 ++- assets/resources/Prefab/SyncCapsule.prefab | 23 +++++++++++++++++++++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/assets/Scene/example_sync_scene.scene b/assets/Scene/example_sync_scene.scene index 7b872b0..e79c4ce 100644 --- a/assets/Scene/example_sync_scene.scene +++ b/assets/Scene/example_sync_scene.scene @@ -411,6 +411,7 @@ }, "_enabled": true, "__prefab": null, + "cubeName": "cube", "_id": "c23znRhDZPRa49AI40a2/T" }, { @@ -424,7 +425,7 @@ "__prefab": null, "prefabs": [ { - "__uuid__": "b4fbe877-80a3-4f7c-969c-fac3aa757846", + "__uuid__": "fc4b58c9-e6a4-4392-9c90-1f10d66e95b4", "__expectedType__": "cc.Prefab" }, { @@ -600,6 +601,7 @@ }, "_enabled": true, "__prefab": null, + "cubeName": "cube", "_id": "21I+sEcu1Bs5F0+PxRySKo" }, { diff --git a/assets/Script/example/sync/SyncCube.ts b/assets/Script/example/sync/SyncCube.ts index b756a65..cc538cd 100644 --- a/assets/Script/example/sync/SyncCube.ts +++ b/assets/Script/example/sync/SyncCube.ts @@ -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) {} } diff --git a/assets/Script/sync/NodeReplicator.ts b/assets/Script/sync/NodeReplicator.ts index 57f1350..e190d95 100644 --- a/assets/Script/sync/NodeReplicator.ts +++ b/assets/Script/sync/NodeReplicator.ts @@ -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) { diff --git a/assets/Script/sync/SyncUtil.ts b/assets/Script/sync/SyncUtil.ts index e6ba569..834ae42 100644 --- a/assets/Script/sync/SyncUtil.ts +++ b/assets/Script/sync/SyncUtil.ts @@ -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); }; } diff --git a/assets/resources/Prefab/SyncCapsule.prefab b/assets/resources/Prefab/SyncCapsule.prefab index 5081175..ac0e5d9 100644 --- a/assets/resources/Prefab/SyncCapsule.prefab +++ b/assets/resources/Prefab/SyncCapsule.prefab @@ -24,10 +24,13 @@ }, { "__id__": 5 + }, + { + "__id__": 7 } ], "_prefab": { - "__id__": 7 + "__id__": 9 }, "_lpos": { "__type__": "cc.Vec3", @@ -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": {