Files
oops-plugin-framework/assets/libs/gui/language/LanguageLabel.ts
dgflash f2fe9d47b6 1. 存储模块全面优化,修复跨平台兼容性问题,完美支持所有Unicode字符
2. 存储模块性能提升,添加LRU缓存、批量操作支持,优化内存使用
3. 多语言模块性能与内存管理优化,组件查询性能提升
4. 时间模块类型安全与性能优化,使用泛型替代any,对象池机制减少内存分配
5. 事件系统修复双重注册、重复注册等严重问题,实现EventData对象池减少GC压力
6. RandomManager修复4个逻辑BUG,包括边界问题和越界问题
7. 音频模块内存与性能优化,避免重复加载,优化数据结构,添加完整清理机制
8. CCView与CCViewVM合并,支持按需启用MVVM
9. Collection模块优化,AsyncQueue添加队列容量限制,Collection查询性能提升
10. ECS系统全面优化,对象池复用减少内存分配,循环性能提升
11. 优化MVVM组件性能
2026-01-09 21:54:05 +08:00

166 lines
4.7 KiB
TypeScript
Raw 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 type { TTFFont } from 'cc';
import { CCString, Component, Label, RichText, _decorator, warn } from 'cc';
import { EDITOR } from 'cc/env';
import { LanguageData } from './LanguageData';
const { ccclass, property, menu } = _decorator;
@ccclass('LangLabelParamsItem')
export class LangLabelParamsItem {
@property
key = '';
@property
value = '';
}
/** 文本多语言 */
@ccclass('LanguageLabel')
@menu('OopsFramework/Language/LanguageLabel (文本多语言)')
export class LanguageLabel extends Component {
@property({
type: LangLabelParamsItem,
displayName: 'params'
})
private _params: Array<LangLabelParamsItem> = [];
@property({
type: LangLabelParamsItem,
displayName: 'params'
})
set params(value: Array<LangLabelParamsItem>) {
this._params = value;
if (!EDITOR) {
this._needUpdate = true;
}
}
get params(): Array<LangLabelParamsItem> {
return this._params || [];
}
@property({ serializable: true })
private _dataID = '';
@property({ type: CCString, serializable: true })
get dataID(): string {
return this._dataID || '';
}
set dataID(value: string) {
this._dataID = value;
if (!EDITOR) {
this._needUpdate = true;
}
}
get string(): string {
let _string = LanguageData.getLangByID(this._dataID);
if (_string && this._params.length > 0) {
this._params.forEach((item: LangLabelParamsItem) => {
_string = _string.replace(`%{${item.key}}`, item.value);
});
}
if (!_string) {
warn('[LanguageLabel] 未找到语言标识使用dataID替换');
_string = this._dataID;
}
return _string;
}
/** 更新语言 */
language() {
this._needUpdate = true;
}
/** 初始字体尺寸 */
initFontSize = 0;
/** 缓存的Label组件引用 */
private _labelCache: Label | null = null;
/** 缓存的RichText组件引用 */
private _richTextCache: RichText | null = null;
/** 是否已初始化组件缓存 */
private _componentInitialized = false;
private _needUpdate = false;
onLoad() {
this._initComponents();
this._needUpdate = true;
}
/** 初始化并缓存组件引用 */
private _initComponents() {
if (this._componentInitialized) return;
this._labelCache = this.getComponent(Label);
this._richTextCache = this.getComponent(RichText);
this._componentInitialized = true;
if (!this._labelCache && !this._richTextCache) {
warn('[LanguageLabel] 该节点没有cc.Label || cc.RichText组件');
}
}
/**
* 修改多语言参数,采用惰性求值策略
* @param key 对于i18n表里面的key值
* @param value 替换的文本
*/
setVars(key: string, value: string) {
let haskey = false;
// 优化:找到后立即退出循环
for (let i = 0; i < this._params.length; i++) {
const element: LangLabelParamsItem = this._params[i];
if (element.key === key) {
element.value = value;
haskey = true;
break; // 找到后立即退出
}
}
if (!haskey) {
const ii = new LangLabelParamsItem();
ii.key = key;
ii.value = value;
this._params.push(ii);
}
this._needUpdate = true;
}
update() {
if (this._needUpdate) {
this.updateContent();
this._needUpdate = false;
}
}
updateContent() {
// 确保组件已初始化
if (!this._componentInitialized) {
this._initComponents();
}
const font: TTFFont | null = LanguageData.font;
// 使用缓存的组件引用避免重复调用getComponent
if (this._labelCache) {
if (font) {
this._labelCache.font = font;
}
this._labelCache.string = this.string;
this.initFontSize = this._labelCache.fontSize;
}
else if (this._richTextCache) {
if (font) {
this._richTextCache.font = font;
}
this._richTextCache.string = this.string;
this.initFontSize = this._richTextCache.fontSize;
}
}
onDestroy() {
// 清理缓存引用,帮助垃圾回收
this._labelCache = null;
this._richTextCache = null;
this._params = [];
}
}