【creator3.0】示例代码调整

This commit is contained in:
wyb10a10
2021-03-20 15:57:57 +08:00
parent 5edd368224
commit 75c2a7e2e8
20 changed files with 392 additions and 360 deletions

View File

@@ -1,72 +1,79 @@
import { assetManager } from "cc";
import { Sprite } from "cc";
import { SpriteFrame } from "cc";
import { Component, Node, Label, Asset, Prefab, _decorator, instantiate, resources } from "cc";
import ResLoader, { resLoader } from "../res/ResLoader";
const { ccclass, property } = cc._decorator;
const { ccclass, property } = _decorator;
@ccclass
export default class NetExample extends cc.Component {
@property(cc.Node)
attachNode: cc.Node = null;
@property(cc.Label)
dumpLabel: cc.Label = null;
ress: cc.Asset[] = null;
remoteRes: cc.Asset = null;
export default class NetExample extends Component {
@property(Node)
attachNode: Node | null = null;
@property(Label)
dumpLabel: Label | null = null;
ress: Asset[] | null = null;
remoteRes: Asset | null = null;
start() {
}
onLoadRes() {
cc.loader.loadRes("prefabDir/HelloWorld", cc.Prefab, (error: Error, prefab: cc.Prefab) => {
if (!error) {
cc.instantiate(prefab).parent = this.attachNode;
}
});
// 动态加载资源
resources.load("prefabDir/HelloWorld", Prefab,
(error, prefab) => {
if (!error) {
instantiate(prefab).parent = this.attachNode;
}
});
}
onUnloadRes() {
this.attachNode.removeAllChildren(true);
cc.loader.releaseRes("prefabDir/HelloWorld");
// 释放动态加载的资源
this.attachNode!.removeAllChildren();
resources.release("prefabDir/HelloWorld");
}
onMyLoadRes() {
ResLoader.loadDir("prefabDir", cc.Prefab, (error: Error, prefabs: cc.Prefab[]) => {
resLoader.loadDir("prefabDir", Prefab, (error, prefabs) => {
if (!error) {
this.ress = prefabs;
for (let i = 0; i < prefabs.length; ++i) {
cc.instantiate(prefabs[i]).parent = this.attachNode;
instantiate(prefabs[i]).parent = this.attachNode;
}
}
});
}
onMyUnloadRes() {
this.attachNode.removeAllChildren(true);
this.attachNode!.removeAllChildren();
if (this.ress) {
for(let item of this.ress) {
ResLoader.release(item);
for (let item of this.ress) {
item.decRef(true);
}
this.ress = null;
}
}
onLoadRemote() {
ResLoader.load("http://tools.itharbors.com/christmas/res/tree.png", (err, res) => {
resLoader.load("http://tools.itharbors.com/christmas/res/tree.png", (err, res) => {
if (err || !res) return;
this.remoteRes = res;
let spriteFrame = new cc.SpriteFrame(res);
let node = new cc.Node("tmp");
let sprite = node.addComponent(cc.Sprite);
let spriteFrame = new SpriteFrame();
spriteFrame.texture = res;
let node = new Node("tmp");
let sprite = node.addComponent(Sprite);
sprite.spriteFrame = spriteFrame;
node.parent = this.attachNode;
})
}
onUnloadRemote() {
this.attachNode.removeAllChildren(true);
this.remoteRes.decRef();
this.attachNode!.removeAllChildren();
this.remoteRes!.decRef();
}
onDump() {
let Loader:any = cc.loader;
this.dumpLabel.string = `当前资源总数:${Object.keys(Loader._cache).length}`;
this.dumpLabel!.string = `当前资源总数:${assetManager.assets.count}`;
}
}