Files

103 lines
5.6 KiB
C#

#if UNITY_EDITOR
using System;
using System.IO;
using System.Linq;
using AOT.Boot;
using AOT.UI;
using TMPro;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;
namespace AIGame.EditorTools
{
public static class BuildBootLoading
{
public const string PrefabPath = "Assets/AOT/UI/UI_BootLoading.prefab";
public const string FontPath = "Assets/AOT/UI/BootChinese.asset";
const string BootScene = "Assets/Scenes/Boot.unity";
[MenuItem("AIGame/Boot/Build Loading Screen")]
public static string Build()
{
if (EditorApplication.isPlaying) throw new InvalidOperationException("Stop Play Mode first.");
for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCount; i++)
if (UnityEngine.SceneManagement.SceneManager.GetSceneAt(i).isDirty)
throw new InvalidOperationException("Save or discard existing scene changes first.");
BuildFont();
string json = Path.GetFullPath(Path.Combine(Application.dataPath, "../../Doc/UIPrefabCreater/UI_BootLoading.json"));
var path = JsonUIPrefabBuilder.BuildFromJsonFile(json);
if (path != PrefabPath) throw new InvalidOperationException("Loading JSON generation failed.");
var setup = EditorSceneManager.GetSceneManagerSetup();
try
{
var scene = EditorSceneManager.OpenScene(BootScene, OpenSceneMode.Single);
var driver = scene.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren<BootDriver>(true)).Single();
var so = new SerializedObject(driver);
so.FindProperty("loadingPrefab").objectReferenceValue = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath).GetComponent<BootPatchView>();
so.ApplyModifiedPropertiesWithoutUndo();
EditorSceneManager.MarkSceneDirty(scene);
EditorSceneManager.SaveScene(scene);
}
finally { EditorSceneManager.RestoreSceneManagerSetup(setup); }
if (!Application.HasProLicense()) throw new InvalidOperationException("Splash configuration requires a supported Unity license.");
PlayerSettings.SplashScreen.show = false;
PlayerSettings.SplashScreen.showUnityLogo = false;
AssetDatabase.SaveAssets();
return "PASS: JSON loading prefab, static Chinese glyphs, Boot reference and splash disabled; Player not rebuilt.";
}
public static string ValidateConfigured()
{
if (PlayerSettings.SplashScreen.show) return "启动 Splash 仍开启,请先执行 AIGame/Boot/Build Loading Screen";
if (!AssetDatabase.GetDependencies(BootScene, true).Contains(PrefabPath))
return "Boot 尚未引用启动 Prefab,请先执行 AIGame/Boot/Build Loading Screen";
return null;
}
static void BuildFont()
{
if (!AssetDatabase.IsValidFolder("Assets/AOT/UI")) AssetDatabase.CreateFolder("Assets/AOT", "UI");
const string words = "萌友防线小镇营地集结伙伴·开启冒险首次进入需要下载资源请稍候正在准备…,。检查更新系统布置就绪即将出发加载失败重新游戏连接网络等待秒完成本热代码主界面包回退编辑器直通跳过补丁请求版本超时";
string characters = new string((words + new string(Enumerable.Range(32, 95).Select(i => (char)i).ToArray())).Distinct().ToArray());
var existing = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(FontPath);
if (existing != null)
{
if (!existing.HasCharacters(characters))
{
// Extend the authored subset without changing its GUID or runtime mode.
existing.atlasPopulationMode = AtlasPopulationMode.Dynamic;
try
{
if (!existing.TryAddCharacters(characters, out string missingCharacters))
throw new InvalidOperationException("Boot font missing: " + missingCharacters);
}
finally { existing.atlasPopulationMode = AtlasPopulationMode.Static; }
EditorUtility.SetDirty(existing);
foreach (var texture in existing.atlasTextures) EditorUtility.SetDirty(texture);
AssetDatabase.SaveAssetIfDirty(existing);
}
return;
}
var source = AssetDatabase.LoadAssetAtPath<Font>("Assets/Bundles/UI/Font/siyuan_CN_Bold.otf");
var font = TMP_FontAsset.CreateFontAsset(source, 58, 6, GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic, false);
if (!font.TryAddCharacters(characters, out string missing)) throw new InvalidOperationException("Boot font missing: " + missing);
font.atlasPopulationMode = AtlasPopulationMode.Static;
font.fallbackFontAssetTable = new System.Collections.Generic.List<TMP_FontAsset>();
font.name = "BootChinese";
AssetDatabase.CreateAsset(font, FontPath);
font.material.name = "BootChinese Material";
AssetDatabase.AddObjectToAsset(font.material, font);
foreach (var texture in font.atlasTextures)
{
texture.name = "BootChinese Atlas";
AssetDatabase.AddObjectToAsset(texture, font);
}
EditorUtility.SetDirty(font);
AssetDatabase.SaveAssetIfDirty(font);
}
}
}
#endif