fix: 修复 Unity 环境背景和字体问题

- 添加 Environment 对象和 EnvironmentManager 组件到场景
- 重新生成 SourceHanSansSC-Regular SDF 字体资产
- 添加 FixTMPFont 和 RegenerateChineseFont 编辑器工具
This commit is contained in:
empty
2026-01-01 19:22:24 +08:00
parent 3e89a17b69
commit 0cd7c9c4d9
7 changed files with 687 additions and 19387 deletions

View File

@@ -0,0 +1,101 @@
using UnityEngine;
using UnityEditor;
using TMPro;
public class FixTMPFont : EditorWindow
{
[MenuItem("Tools/Fix TMP Font Assets")]
public static void FixFonts()
{
// Find all TMP font assets
string[] guids = AssetDatabase.FindAssets("t:TMP_FontAsset");
int fixedCount = 0;
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
TMP_FontAsset font = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path);
if (font == null) continue;
bool needsSave = false;
// Check if material is missing
if (font.material == null)
{
Debug.LogWarning($"[FixTMPFont] Font '{font.name}' has missing material. Attempting to fix...");
// Try to find a sub-asset material
Object[] subAssets = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
foreach (Object subAsset in subAssets)
{
if (subAsset is Material mat)
{
Debug.Log($"[FixTMPFont] Found sub-asset material: {mat.name}");
font.material = mat;
needsSave = true;
break;
}
}
// If still no material, create one
if (font.material == null && font.atlasTexture != null)
{
Debug.Log($"[FixTMPFont] Creating new material for '{font.name}'");
Shader shader = Shader.Find("TextMeshPro/Distance Field");
if (shader != null)
{
Material newMat = new Material(shader);
newMat.name = font.name + " Atlas Material";
newMat.SetTexture("_MainTex", font.atlasTexture);
AssetDatabase.AddObjectToAsset(newMat, font);
font.material = newMat;
needsSave = true;
Debug.Log($"[FixTMPFont] Created and assigned new material for '{font.name}'");
}
else
{
Debug.LogError("[FixTMPFont] Could not find TextMeshPro/Distance Field shader!");
}
}
}
// Verify atlas texture
if (font.atlasTexture == null)
{
Debug.LogError($"[FixTMPFont] Font '{font.name}' has no atlas texture - needs to be regenerated!");
}
if (needsSave)
{
EditorUtility.SetDirty(font);
fixedCount++;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"[FixTMPFont] Done! Fixed {fixedCount} font asset(s).");
EditorUtility.DisplayDialog("Fix TMP Fonts", $"Fixed {fixedCount} font asset(s). Check console for details.", "OK");
}
[MenuItem("Tools/Regenerate Chinese Font")]
public static void RegenerateChinese()
{
string fontPath = "Assets/Fonts/SourceHanSansSC-Regular.otf";
Font sourceFont = AssetDatabase.LoadAssetAtPath<Font>(fontPath);
if (sourceFont == null)
{
Debug.LogError($"[FixTMPFont] Could not load font at '{fontPath}'");
return;
}
Debug.Log($"[FixTMPFont] Source font loaded: {sourceFont.name}");
Debug.Log("[FixTMPFont] Please use Window > TextMeshPro > Font Asset Creator to regenerate the font.");
// Open the Font Asset Creator
EditorApplication.ExecuteMenuItem("Window/TextMeshPro/Font Asset Creator");
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6708bf6d0bfc548ce85411093037126b

View File

@@ -0,0 +1,98 @@
using UnityEngine;
using UnityEditor;
using TMPro;
using System.IO;
public class RegenerateChineseFont : EditorWindow
{
[MenuItem("Tools/Regenerate Chinese Font Asset")]
public static void Regenerate()
{
string sourceFontPath = "Assets/Fonts/SourceHanSansSC-Regular.otf";
string outputPath = "Assets/Fonts/SourceHanSansSC-Regular SDF.asset";
// Load source font
Font sourceFont = AssetDatabase.LoadAssetAtPath<Font>(sourceFontPath);
if (sourceFont == null)
{
Debug.LogError($"[RegenerateFont] Source font not found at: {sourceFontPath}");
return;
}
Debug.Log($"[RegenerateFont] Loaded source font: {sourceFont.name}");
// Create font asset using TMP's API
TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(sourceFont);
if (fontAsset == null)
{
Debug.LogError("[RegenerateFont] Failed to create font asset!");
return;
}
// Configure the font asset for Chinese characters
fontAsset.name = "SourceHanSansSC-Regular SDF";
// Save the asset
if (File.Exists(outputPath))
{
AssetDatabase.DeleteAsset(outputPath);
}
AssetDatabase.CreateAsset(fontAsset, outputPath);
// Save the atlas texture as a sub-asset
if (fontAsset.atlasTexture != null)
{
fontAsset.atlasTexture.name = "SourceHanSansSC-Regular SDF Atlas";
AssetDatabase.AddObjectToAsset(fontAsset.atlasTexture, fontAsset);
}
// Save the material as a sub-asset
if (fontAsset.material != null)
{
fontAsset.material.name = "SourceHanSansSC-Regular SDF Material";
AssetDatabase.AddObjectToAsset(fontAsset.material, fontAsset);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"[RegenerateFont] Font asset created successfully at: {outputPath}");
// Setup as fallback for TMP Settings
SetupFallbackFont(fontAsset);
EditorUtility.DisplayDialog("Regenerate Font",
"Chinese font asset regenerated successfully!\n\nNote: For full Chinese character support, use Window > TextMeshPro > Font Asset Creator to generate with a specific character set.",
"OK");
}
private static void SetupFallbackFont(TMP_FontAsset chineseFont)
{
// Get TMP Settings
string settingsPath = "Assets/TextMesh Pro/Resources/TMP Settings.asset";
TMP_Settings settings = AssetDatabase.LoadAssetAtPath<TMP_Settings>(settingsPath);
if (settings == null)
{
Debug.LogWarning("[RegenerateFont] TMP Settings not found. Skipping fallback setup.");
return;
}
// Add Chinese font as fallback to default font
string defaultFontPath = "Assets/TextMesh Pro/Resources/Fonts & Materials/LiberationSans SDF.asset";
TMP_FontAsset defaultFont = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(defaultFontPath);
if (defaultFont != null && defaultFont.fallbackFontAssetTable != null)
{
if (!defaultFont.fallbackFontAssetTable.Contains(chineseFont))
{
defaultFont.fallbackFontAssetTable.Add(chineseFont);
EditorUtility.SetDirty(defaultFont);
AssetDatabase.SaveAssets();
Debug.Log("[RegenerateFont] Added Chinese font as fallback to default font.");
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5a690ec5d70524b1bbf892548be212bd