| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import { AppKernel } from "@scripts/app/AppKernel";
- import { AssetSlot } from "@scripts/components/AssetSlot";
- import { BUNDLE_ID } from "@scripts/define/ResDefine";
- import { Sprite, SpriteFrame } from "cc";
- const GoodsIconRes = "common/goodsIcon/img_icon";
- /**
- * 动态加载spriteFrame path和bundle都是空 则为sprite.spriteFrame = null;
- * @param sprite
- * @param path
- * @param bundle
- * @returns
- */
- export async function setSpriteFrame(sprite: Sprite, path?: string, bundle?: string) {
- const node = sprite.node;
- const slot = node.getComponent(AssetSlot) || node.addComponent(AssetSlot);
- if (!path && !bundle) {
- slot.changeAsset(null);
- sprite.spriteFrame = null;
- return;
- }
- let res: SpriteFrame = null;
- const resources = AppKernel.resources;
- if (path.startsWith("http")) {
- res = await resources.loadRemoteSp(path);
- } else {
- if (!path.endsWith("/spriteFrame")) path += "/spriteFrame";
- res = await resources.load(bundle || BUNDLE_ID.Lobby, path, SpriteFrame);
- }
- if (!res) return;
- if (!sprite.isValid) {
- resources.releaseRes(res);
- return;
- }
- res.addRef();
- sprite.spriteFrame = res;
- slot.changeAsset(res);
- return res;
- }
- export async function setGoodsIcon(sprite: Sprite, type: number, isGCOnly = false) {
- const iconPath = isGCOnly ? `${GoodsIconRes}gc` : `${GoodsIconRes}${type}`;
- return setSpriteFrame(sprite, `${iconPath}`);
- }
|