import { _decorator, AssetManager, assetManager, Component, Node, resources, Sprite, SpriteFrame } from ‘cc’;
import { EDITOR } from ‘cc/env’;
const { ccclass, property, executeInEditMode } = _decorator;
function extractResourcesPath(dbUrl: string): string {
const prefix = 'db://assets/resources/';
if (!dbUrl.startsWith(prefix))
return '';
return dbUrl
.slice(prefix.length) // 去前缀
.replace(/@.+$/, '') // 去 sub-asset
.replace(/\.[^/.]+$/, ''); // 去扩展名
}
@ccclass(‘SpriteLoader’)
@executeInEditMode
export class SpriteLoader extends Sprite {
@property({ type: SpriteFrame, serializable: false , visible:false })
protected _spriteFrame: SpriteFrame | null = null;
@property({ type: String, serializable: true, readonly: true })
public mSpriteUrl: string;
@property({ type: String, serializable: true, readonly: true })
public mSpriteUUID: string;
@property({ type: SpriteFrame, serializable: false })
get spriteFrame(): SpriteFrame {
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Sprite.prototype), 'spriteFrame') || Object.getOwnPropertyDescriptor(Sprite.prototype, 'spriteFrame');
return descriptor?.get?.call(this);
}
set spriteFrame(value: SpriteFrame) {
if (EDITOR) {
this.SetSpriteData( value );
} else {
console.warn("非编辑器模式,不能进行此值的设置");
}
}
public SetSpriteData(spriteFrame: SpriteFrame) {
if (spriteFrame == undefined)
this.mSpriteUUID = "";
else
this.mSpriteUUID = spriteFrame._uuid;//获取到位移UUID
if (EDITOR) {
Editor.Message.request('asset-db', 'query-url', spriteFrame._uuid).then((data1) => {
let resourcesPath: string = extractResourcesPath(data1);
this.url = resourcesPath;
this.UpdateSpriteFrame();//立即更新图片资源
});
} else {
this.UpdateSpriteFrame();//立即更新图片资源
}
}
@property({ type: String })
public get url(): string {
return this.mSpriteUrl;
}
public set url(path: string) {
this.mSpriteUrl = path;
this.UpdateSpriteFrame();
}
public get assetUUID(): string { return this.mSpriteUUID; }
public set assetUUID(uuid: string) {
this.mSpriteUUID = uuid;
}
private InitSpriteFrame() {
this.UpdateSpriteFrame();
}
private UpdateSpriteFrame() {
if (EDITOR) {
assetManager.loadAny({ uuid: this.mSpriteUUID, }, (err, data) => {
console.log(err);
console.log(data);
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Sprite.prototype), 'spriteFrame') || Object.getOwnPropertyDescriptor(Sprite.prototype, 'spriteFrame');
descriptor?.set?.call(this,data);//调用父类构造
});
} else {
setTimeout( ()=>{
resources.load( `${this.mSpriteUrl}/spriteFrame` , SpriteFrame , ( err , data )=>{
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Sprite.prototype), 'spriteFrame') || Object.getOwnPropertyDescriptor(Sprite.prototype, 'spriteFrame');
descriptor?.set?.call(this,data);//调用父类构造
});
} , 3000 );
}
}
protected onLoad(): void {
super.onLoad();
this.InitSpriteFrame();//初始化精灵节点
this.url = "GameResource/Font/登录界面";
}
public onDestroy(): void {
super.onDestroy();
}
}
代码差不多就是这个样子,可以直接跑。
思路就是,不再对Sprite的spriteFrame 进行序列化存储。 而是改为 编辑器以UUID作为 图片参数 , 运行模式以 图片路径作为参数。 来动态加载图片 ,这样应该能够很好的提升预制体打开速度