-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetImage.ts
50 lines (37 loc) · 994 Bytes
/
NetImage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { ccclass, property } = cc._decorator;
@ccclass
export default class NetImage extends cc.Node {
@property
_src: string = "";
@property
size: cc.Size = new cc.Size(0, 0);
sprite: cc.Sprite;
_spriteFrame: cc.SpriteFrame;
set src(src: string) {
this._src = src;
if (!this.sprite) {
this.sprite = this.addComponent(cc.Sprite);
}
this._loadFromUrl(this._src);
}
get src() {
return this._src;
}
onload?: () => any;
private _loadFromUrl(src: string) {
cc.loader.load({ url: src, type: "png" }, (err, texture: cc.Texture2D) => {
if (err) {
cc.log(err);
return;
}
this._spriteFrame = new cc.SpriteFrame(texture);
this.sprite.spriteFrame = this._spriteFrame;
if (this.size.width === 0 && this.size.height === 0) {
this.setContentSize(this._spriteFrame.getRect());
} else {
this.setContentSize(this.size);
}
this.onload && this.onload();
});
}
}