From d012d3a6a15c950fb5f0d366b07285a4d3ce225d Mon Sep 17 00:00:00 2001 From: JianMiau Date: Mon, 2 May 2022 21:51:23 +0800 Subject: [PATCH] [add] Array AddListener --- assets/Scene/Test.fire | 840 ++++++++++++++++++ assets/Scene/Test.fire.meta | 7 + assets/Script/Badminton/Badminton.ts | 8 +- .../Engine/Component/Button/HoldButton.ts | 6 +- .../Utils/CCExtensions/ArrayExtension.ts | 18 + .../Engine/Utils/CCExtensions/CCExtension.ts | 6 +- 6 files changed, 876 insertions(+), 9 deletions(-) create mode 100644 assets/Scene/Test.fire create mode 100644 assets/Scene/Test.fire.meta diff --git a/assets/Scene/Test.fire b/assets/Scene/Test.fire new file mode 100644 index 0000000..ecf1c56 --- /dev/null +++ b/assets/Scene/Test.fire @@ -0,0 +1,840 @@ +[ + { + "__type__": "cc.SceneAsset", + "_name": "", + "_objFlags": 0, + "_native": "", + "scene": { + "__id__": 1 + } + }, + { + "__type__": "cc.Scene", + "_objFlags": 0, + "_parent": null, + "_children": [ + { + "__id__": 2 + } + ], + "_active": true, + "_components": [], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 0, + "height": 0 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1 + ] + }, + "_is3DNode": true, + "_groupIndex": 0, + "groupIndex": 0, + "autoReleaseAssets": false, + "_id": "0d57aada-5c13-49fb-a6cd-1445b1145755" + }, + { + "__type__": "cc.Node", + "_name": "Canvas", + "_objFlags": 0, + "_parent": { + "__id__": 1 + }, + "_children": [ + { + "__id__": 3 + }, + { + "__id__": 5 + } + ], + "_active": true, + "_components": [ + { + "__id__": 17 + }, + { + "__id__": 18 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 960, + "height": 640 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + 480, + 320, + 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": "a5esZu+45LA5mBpvttspPD" + }, + { + "__type__": "cc.Node", + "_name": "Main Camera", + "_objFlags": 0, + "_parent": { + "__id__": 2 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 4 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 960, + "height": 640 + }, + "_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": "e1WoFrQ79G7r4ZuQE3HlNb" + }, + { + "__type__": "cc.Camera", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 3 + }, + "_enabled": true, + "_cullingMask": 4294967295, + "_clearFlags": 7, + "_backgroundColor": { + "__type__": "cc.Color", + "r": 0, + "g": 0, + "b": 0, + "a": 255 + }, + "_depth": -1, + "_zoomRatio": 1, + "_targetTexture": null, + "_fov": 60, + "_orthoSize": 10, + "_nearClip": 1, + "_farClip": 4096, + "_ortho": true, + "_rect": { + "__type__": "cc.Rect", + "x": 0, + "y": 0, + "width": 1, + "height": 1 + }, + "_renderStages": 1, + "_alignWithScreen": true, + "_id": "81GN3uXINKVLeW4+iKSlim" + }, + { + "__type__": "cc.Node", + "_name": "Test", + "_objFlags": 0, + "_parent": { + "__id__": 2 + }, + "_children": [ + { + "__id__": 6 + }, + { + "__id__": 8 + } + ], + "_active": true, + "_components": [ + { + "__id__": 13 + }, + { + "__id__": 14 + }, + { + "__id__": 15 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 183, + "g": 0, + "b": 0, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 135, + "height": 55 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -166.208, + 275.308, + 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": "acRFoRLs1KQIesFojDKsH6" + }, + { + "__type__": "cc.Node", + "_name": "Label", + "_objFlags": 512, + "_parent": { + "__id__": 5 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 7 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 200, + "height": 63 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0.5, + 0.5, + 1 + ] + }, + "_eulerAngles": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_skewX": 0, + "_skewY": 0, + "_is3DNode": false, + "_groupIndex": 0, + "groupIndex": 0, + "_id": "bavOO+5W5HZowjbEwdS7NY" + }, + { + "__type__": "cc.Label", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 6 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_string": "比賽結算", + "_N$string": "比賽結算", + "_fontSize": 50, + "_lineHeight": 50, + "_enableWrapText": true, + "_N$file": null, + "_isSystemFontUsed": true, + "_spacingX": 0, + "_batchAsBitmap": false, + "_styleFlags": 1, + "_underlineHeight": 0, + "_N$horizontalAlign": 1, + "_N$verticalAlign": 1, + "_N$fontFamily": "Arial", + "_N$overflow": 0, + "_N$cacheMode": 0, + "_id": "50v+yil/pH9q0qfFVzMOjl" + }, + { + "__type__": "cc.Node", + "_name": "holdLine", + "_objFlags": 512, + "_parent": { + "__id__": 5 + }, + "_children": [ + { + "__id__": 9 + }, + { + "__id__": 11 + } + ], + "_active": true, + "_components": [], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 0, + "height": 0 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -8.671, + -33.651, + 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": "60nYWYaL9IpYqQOeKg/yLq" + }, + { + "__type__": "cc.Node", + "_name": "bg", + "_objFlags": 0, + "_parent": { + "__id__": 8 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 10 + } + ], + "_prefab": null, + "_opacity": 100, + "_color": { + "__type__": "cc.Color", + "r": 167, + "g": 167, + "b": 167, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 120, + "height": 5 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -50, + 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": "94YKMHe19Ml6Peax6L9q/A" + }, + { + "__type__": "cc.Sprite", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 9 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_spriteFrame": { + "__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91" + }, + "_type": 0, + "_sizeMode": 0, + "_fillType": 0, + "_fillCenter": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0 + }, + "_fillStart": 0, + "_fillRange": 0, + "_isTrimmedMode": true, + "_atlas": null, + "_id": "1bO5+KRI5MZ5DmTz5N9pFM" + }, + { + "__type__": "cc.Node", + "_name": "line", + "_objFlags": 0, + "_parent": { + "__id__": 8 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 12 + } + ], + "_prefab": null, + "_opacity": 100, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 120, + "height": 5 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -50, + 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": "42ErHH3g1Ib4g7aWzlYuNh" + }, + { + "__type__": "cc.Sprite", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 11 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_spriteFrame": { + "__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91" + }, + "_type": 3, + "_sizeMode": 0, + "_fillType": 0, + "_fillCenter": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0 + }, + "_fillStart": 0, + "_fillRange": 0, + "_isTrimmedMode": true, + "_atlas": null, + "_id": "b2SK7xGHZE9YyisMziLtZ8" + }, + { + "__type__": "cc.Button", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 5 + }, + "_enabled": true, + "_normalMaterial": null, + "_grayMaterial": null, + "duration": 0.1, + "zoomScale": 0.9, + "clickEvents": [], + "_N$interactable": true, + "_N$enableAutoGrayEffect": false, + "_N$transition": 1, + "transition": 1, + "_N$normalColor": { + "__type__": "cc.Color", + "r": 183, + "g": 0, + "b": 0, + "a": 255 + }, + "_N$pressedColor": { + "__type__": "cc.Color", + "r": 200, + "g": 200, + "b": 200, + "a": 255 + }, + "pressedColor": { + "__type__": "cc.Color", + "r": 200, + "g": 200, + "b": 200, + "a": 255 + }, + "_N$hoverColor": { + "__type__": "cc.Color", + "r": 230, + "g": 230, + "b": 230, + "a": 255 + }, + "hoverColor": { + "__type__": "cc.Color", + "r": 230, + "g": 230, + "b": 230, + "a": 255 + }, + "_N$disabledColor": { + "__type__": "cc.Color", + "r": 120, + "g": 120, + "b": 120, + "a": 200 + }, + "_N$normalSprite": { + "__uuid__": "eacf7329-d93d-41c2-a973-4065b01b6041" + }, + "_N$pressedSprite": null, + "pressedSprite": null, + "_N$hoverSprite": null, + "hoverSprite": null, + "_N$disabledSprite": null, + "_N$target": { + "__id__": 5 + }, + "_id": "b1JdTzpetM5Y2+ajB7CgHk" + }, + { + "__type__": "cc.Sprite", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 5 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_spriteFrame": { + "__uuid__": "992fd08c-39b8-4680-b9cc-f7f5a8c07d09" + }, + "_type": 0, + "_sizeMode": 0, + "_fillType": 0, + "_fillCenter": { + "__type__": "cc.Vec2", + "x": 0, + "y": 0 + }, + "_fillStart": 0, + "_fillRange": 0, + "_isTrimmedMode": true, + "_atlas": null, + "_id": "7eE2MaVsZIZKu2cx0RBtjX" + }, + { + "__type__": "0d1d5HfnyFLAK2RYSpW387d", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 5 + }, + "_enabled": true, + "MaxTime": 1, + "IsHaveHoldLine": true, + "HoldLine": { + "__id__": 8 + }, + "ProgressBG": { + "__id__": 10 + }, + "ProgressLine": { + "__id__": 12 + }, + "OnInvoke": [ + { + "__id__": 16 + } + ], + "_id": "42FPUDXnROx55pRRLBeL22" + }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 6 + }, + "component": "", + "_componentId": "cc.Label", + "handler": "destroy", + "customEventData": "false" + }, + { + "__type__": "cc.Canvas", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 2 + }, + "_enabled": true, + "_designResolution": { + "__type__": "cc.Size", + "width": 960, + "height": 640 + }, + "_fitWidth": false, + "_fitHeight": true, + "_id": "59Cd0ovbdF4byw5sbjJDx7" + }, + { + "__type__": "cc.Widget", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 2 + }, + "_enabled": true, + "alignMode": 1, + "_target": null, + "_alignFlags": 45, + "_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": 0, + "_originalHeight": 0, + "_id": "29zXboiXFBKoIV4PQ2liTe" + } +] \ No newline at end of file diff --git a/assets/Scene/Test.fire.meta b/assets/Scene/Test.fire.meta new file mode 100644 index 0000000..bd99450 --- /dev/null +++ b/assets/Scene/Test.fire.meta @@ -0,0 +1,7 @@ +{ + "ver": "1.2.9", + "uuid": "0d57aada-5c13-49fb-a6cd-1445b1145755", + "asyncLoadAssets": false, + "autoReleaseAssets": false, + "subMetas": {} +} \ No newline at end of file diff --git a/assets/Script/Badminton/Badminton.ts b/assets/Script/Badminton/Badminton.ts index c05bdd9..73e2325 100644 --- a/assets/Script/Badminton/Badminton.ts +++ b/assets/Script/Badminton/Badminton.ts @@ -113,6 +113,7 @@ export class Badminton extends cc.Component { //#region 初始化 protected onLoad(): void { + // this.node.getChildByName("Test").getComponent(HoldButton).OnInvoke.AddListener(() => { this.OnChangeAvatar(666); }); Badminton._instance = this; new LocalStorageData(); this.config.Init(); @@ -172,13 +173,14 @@ export class Badminton extends cc.Component { let picObj: cc.Node = item.node.getChildByName("Avatar").getChildByName("Pic"); if (picObj != null) { picObj.getComponent(cc.Sprite).spriteFrame = this.config.GetAvatarPicById(this.TeamMemberList[idx].AvatarId); - // picObj.GetComponent().OnInvoke.AddListener(this.OnChangeAvatar(idx)); let EventHandler: cc.Component.EventHandler = new cc.Component.EventHandler(); EventHandler.target = this.node; EventHandler.component = this.name.split("<")[1].split(">")[0]; EventHandler.handler = "OnChangeAvatar"; EventHandler.customEventData = idx.toString(); picObj.getComponent(HoldButton).OnInvoke.push(EventHandler); + // picObj.getComponent(HoldButton).OnInvoke.AddListener(this.OnChangeAvatar(idx)); + // picObj.getComponent(HoldButton).OnInvoke.AddListener(() => { this.OnChangeAvatar(idx); }); picObj.on("click", () => { item.isChecked = !item.isChecked; }, this); } this._m_toggleList.push(item); @@ -347,10 +349,6 @@ export class Badminton extends cc.Component { // this.AvatarPanel.OpenChange(teamMemberList[index]); } - public Log(a, b): void { - console.log("Log"); - } - //#endregion } diff --git a/assets/Script/Engine/Component/Button/HoldButton.ts b/assets/Script/Engine/Component/Button/HoldButton.ts index 0196097..fcd3ec0 100644 --- a/assets/Script/Engine/Component/Button/HoldButton.ts +++ b/assets/Script/Engine/Component/Button/HoldButton.ts @@ -86,7 +86,11 @@ export default class HoldButton extends cc.Component { this._m_pressDeltaTime = 0; if (this.OnInvoke != null) { this.OnInvoke.forEach((eventHandler: cc.Component.EventHandler) => { - eventHandler.emit([this.node.getComponent(cc.Button)]); + if (eventHandler.target === "AddListener" && eventHandler.component === "AddListener" && eventHandler.handler) { + (eventHandler.handler)(); + } else { + eventHandler.emit([this.node.getComponent(cc.Button)]); + } }); } } diff --git a/assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts b/assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts index fa1e53f..60838cc 100644 --- a/assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts +++ b/assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts @@ -14,8 +14,26 @@ declare interface Array { * @param key 需排序的key(優先順序左到右)(沒有就放空) */ ObjectSort(asc?: boolean[], key?: string[]): any[]; + /** + * 設計給Array使用 + * Add a non persistent listener to the UnityEvent. + * @param call Callback function. + */ + AddListener(call: Function): void; } +Array.prototype.AddListener || Object.defineProperty(Array.prototype, "AddListener", { + enumerable: false, + value: function (call: Function): void { + let EventHandler: cc.Component.EventHandler = new cc.Component.EventHandler(); + EventHandler.target = "AddListener"; + EventHandler.component = "AddListener"; + EventHandler.handler = call; + this.push(EventHandler); + console.log(`AddListener`); + } +}); + Array.prototype.ExRemoveAt || Object.defineProperty(Array.prototype, "ExRemoveAt", { enumerable: false, value: function (index: number): any { diff --git a/assets/Script/Engine/Utils/CCExtensions/CCExtension.ts b/assets/Script/Engine/Utils/CCExtensions/CCExtension.ts index f836100..4c8d126 100644 --- a/assets/Script/Engine/Utils/CCExtensions/CCExtension.ts +++ b/assets/Script/Engine/Utils/CCExtensions/CCExtension.ts @@ -3,7 +3,7 @@ declare namespace cc { export interface Node { /** * 設定世界座標 - * @param worldPoint + * @param worldPoint */ SetWorldPosition(worldPoint: cc.Vec2): void; /** @@ -12,7 +12,7 @@ declare namespace cc { GetWorldPosition(): cc.Vec2; /** * 設定長寬 - * @param size + * @param size */ SetSizeDelta(size: cc.Vec2); /** @@ -21,7 +21,7 @@ declare namespace cc { GetSizeDelta(): cc.Vec2; /** * 增加一個子物件 - * @param childObj + * @param childObj */ ExAddChild(childObj: cc.Prefab | cc.Node, childActive?: boolean): cc.Node;