Files
cocos/assets/scripts/ui/UIStyleAssets.ts
刘宇飞 d393302388 Complete Cocos Creator port with level bundles, themes, and tooling.
Adds level prefabs, theme assets, audio, extensions, and deployment scripts for the Unity WebGL migration.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 15:30:58 +08:00

91 lines
3.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ImageAsset, resources, SpriteFrame, Texture2D } from 'cc';
import { Direction } from '../core/Define';
import { ensureSpriteFrameSize } from '../visual/EntityDisplayRefs';
import { ensureResourcesBundle } from '../core/ResourcesBundle';
import { resolvePlayerTexturePaths, EntityVisualOptions } from '../visual/EntityTextureResolver';
import { VisualAssets } from '../visual/VisualAssets';
import {
getThemeHudIconCandidates,
getThemePortraitPath,
ThemeHudIconKey,
} from '../theme/ThemeRegistry';
export type UIIconKey = ThemeHudIconKey;
const frameCache = new Map<string, SpriteFrame>();
function loadSpriteAt(path: string): Promise<SpriteFrame | null> {
const cached = frameCache.get(path);
if (cached) return Promise.resolve(cached);
return ensureResourcesBundle().then(() => new Promise((resolve) => {
resources.load(`${path}/spriteFrame`, SpriteFrame, (err, sf) => {
if (!err && sf) {
frameCache.set(path, sf);
resolve(sf);
return;
}
resources.load(path, SpriteFrame, (err2, sf2) => {
if (!err2 && sf2) {
frameCache.set(path, sf2);
resolve(sf2);
return;
}
resources.load(path, ImageAsset, (err3, img) => {
if (!err3 && img) {
const tex = new Texture2D();
tex.image = img;
const frame = new SpriteFrame();
frame.texture = tex;
ensureSpriteFrameSize(frame, img.width, img.height);
frameCache.set(path, frame);
resolve(frame);
return;
}
resolve(null);
});
});
});
}));
}
export function getUIIconPath(uiStyle: string | undefined, icon: UIIconKey): string {
const paths = getThemeHudIconCandidates(uiStyle, icon);
if (!paths.length) throw new Error(`[UIStyleAssets] 缺少图标 ${icon}`);
return paths[0];
}
export async function loadUIIcon(
uiStyle: string | undefined,
icon: UIIconKey,
): Promise<SpriteFrame | null> {
for (const path of getThemeHudIconCandidates(uiStyle, icon)) {
const sf = await loadSpriteAt(path);
if (sf) return sf;
}
console.warn(`[UIStyleAssets] 贴图加载失败: ${icon} (${uiStyle ?? 'silu'})`);
return null;
}
export function clearUIIconCache() {
frameCache.clear();
}
/** 地图左上角角色肖像(优先 entities.portrait否则 playerFront */
export async function loadThemeCharacterPortrait(
options: EntityVisualOptions = {},
): Promise<SpriteFrame | null> {
const theme = options.theme;
const portrait = getThemePortraitPath(theme);
if (portrait) {
const sf = await VisualAssets.loadTexturePath(portrait);
if (sf) return sf;
}
for (const path of resolvePlayerTexturePaths(Direction.South, options)) {
const sf = await VisualAssets.loadTexturePath(path);
if (sf) return sf;
}
console.warn(`[UIStyleAssets] 角色肖像加载失败 (${theme ?? 'silu'})`);
return null;
}