Skip to content

Commit 7a4b4ee

Browse files
committed
stable version for wang
1 parent 6751eb5 commit 7a4b4ee

10 files changed

Lines changed: 4376 additions & 47 deletions

File tree

app/(tabs)/main.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const MainUIScreen: React.FC<MainUIScreenProps> = () => {
8686
// 🔥 新增:调试信息面板
8787
const [debugPanelVisible, setDebugPanelVisible] = useState(false);
8888
// 合并为单一对象,确保 modelName 和 modelUrl 同步更新,避免两次 setState 触发两次 useLive2D effect
89-
const [live2dModel, setLive2dModel] = useState<{ name: string; url: string | undefined }>({
89+
const [live2dModel, setLive2dModel] = useState<{ name: string; url: string | undefined; itemId?: string }>({
9090
name: 'mao_pro',
9191
url: undefined,
9292
});
@@ -203,6 +203,7 @@ const MainUIScreen: React.FC<MainUIScreenProps> = () => {
203203
setLive2dModel({
204204
name: modelRes.model_info.name,
205205
url: modelUrl,
206+
itemId: modelRes.model_info.item_id,
206207
});
207208
} else {
208209
console.warn('🎨 [syncLive2dModel] API 返回但无 model_info:', modelRes);
@@ -595,6 +596,7 @@ const MainUIScreen: React.FC<MainUIScreenProps> = () => {
595596
const live2d = useLive2D({
596597
modelName: live2dModel.name,
597598
modelUrl: live2dModel.url,
599+
modelItemId: live2dModel.itemId,
598600
backendHost: config.host,
599601
backendPort: config.port,
600602
// 由页面 focus 生命周期触发加载;避免 autoLoad + focus 双重触发导致重复加载
@@ -640,13 +642,14 @@ const MainUIScreen: React.FC<MainUIScreenProps> = () => {
640642
);
641643

642644
// 角色切换后 modelUrl 变化时,页面已聚焦无法靠 useFocusEffect 触发,需单独监听
643-
// 先显式 unload 再 load,确保 modelPath = undefined 这一帧被渲染,原生层 clearModel() 被调用
645+
// 注意:live2dModel 变化时 useLive2D 已因 modelUrl 变化重建 service(新 service 天然干净)
646+
// 不需要 unloadModel,直接 loadModel 即可;用 ref 确保拿到新 service 的最新引用
647+
const loadModelRef = useRef(live2d.loadModel);
648+
loadModelRef.current = live2d.loadModel;
644649
useEffect(() => {
645650
if (!isPageFocused || !live2dModel.url) return;
646-
live2d.unloadModel();
647-
// 下一帧再 load,确保 unload 的 state 变化(modelPath = undefined)先渲染到原生层
648651
const timer = setTimeout(() => {
649-
live2d.loadModel();
652+
loadModelRef.current();
650653
}, 0);
651654
return () => clearTimeout(timer);
652655
}, [live2dModel]); // eslint-disable-line react-hooks/exhaustive-deps
@@ -1578,12 +1581,27 @@ ${t('serverInfo.character')}: ${config.characterName || t('main.character.noChar
15781581
</ScrollView>
15791582
<TouchableOpacity
15801583
style={{
1581-
backgroundColor: '#40c5f1',
1584+
backgroundColor: '#e05555',
15821585
borderRadius: 8,
15831586
paddingVertical: 12,
15841587
alignItems: 'center',
15851588
marginTop: 15,
15861589
}}
1590+
onPress={async () => {
1591+
await live2d.live2dService?.clearModelCache();
1592+
setDebugPanelVisible(false);
1593+
}}
1594+
>
1595+
<Text style={{ color: '#fff', fontWeight: 'bold' }}>🗑️ 清除模型缓存</Text>
1596+
</TouchableOpacity>
1597+
<TouchableOpacity
1598+
style={{
1599+
backgroundColor: '#40c5f1',
1600+
borderRadius: 8,
1601+
paddingVertical: 12,
1602+
alignItems: 'center',
1603+
marginTop: 10,
1604+
}}
15871605
onPress={() => setDebugPanelVisible(false)}
15881606
>
15891607
<Text style={{ color: '#fff', fontWeight: 'bold' }}>{t('common.close')}</Text>

0 commit comments

Comments
 (0)