浏览代码

Localize TMP font

LocalizeTMProFontEvent.cs - Component to localize a TMP_FontAsset
LocalizeComponent_TMProFont.cs - TextmeshProUGUI context menu "Localize String And Font"
/main
Brett 4 年前
当前提交
53a29ad3
共有 4 个文件被更改,包括 113 次插入0 次删除
  1. 69
      UOP1_Project/Assets/Scripts/Localization/LocalizeComponent_TMProFont.cs
  2. 11
      UOP1_Project/Assets/Scripts/Localization/LocalizeComponent_TMProFont.cs.meta
  3. 22
      UOP1_Project/Assets/Scripts/Localization/LocalizeTMProFontEvent.cs
  4. 11
      UOP1_Project/Assets/Scripts/Localization/LocalizeTMProFontEvent.cs.meta

69
UOP1_Project/Assets/Scripts/Localization/LocalizeComponent_TMProFont.cs


using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Localization.Components;
namespace UnityEditor.Localization.Plugins.TMPro
{
internal static class LocalizeComponent_TMProFont
{
[MenuItem("CONTEXT/TextMeshProUGUI/Localize String And Font")]
static void LocalizeTMProText(MenuCommand command)
{
var target = command.context as TextMeshProUGUI;
SetupForStringLocalization(target);
SetupForFontLocalization(target);
}
public static MonoBehaviour SetupForStringLocalization(TextMeshProUGUI target)
{
//Avoid adding the component multiple times
if (target.GetComponent<LocalizeStringEvent>() != null)
return null;
var comp = Undo.AddComponent(target.gameObject, typeof(LocalizeStringEvent)) as LocalizeStringEvent;
var setStringMethod = target.GetType().GetProperty("text").GetSetMethod();
var methodDelegate = System.Delegate.CreateDelegate(typeof(UnityAction<string>), target, setStringMethod) as UnityAction<string>;
Events.UnityEventTools.AddPersistentListener(comp.OnUpdateString, methodDelegate);
const int kMatchThreshold = 5;
var foundKey = LocalizationEditorSettings.FindSimilarKey(target.text);
if (foundKey.collection != null && foundKey.matchDistance < kMatchThreshold)
{
comp.StringReference.TableEntryReference = foundKey.entry.Id;
comp.StringReference.TableReference = foundKey.collection.TableCollectionNameReference;
}
return null;
}
public static MonoBehaviour SetupForFontLocalization(TextMeshProUGUI target)
{
//Avoid adding the component multiple times
if (target.GetComponent<LocalizeTMProFontEvent>() != null)
return null;
var comp = Undo.AddComponent(target.gameObject, typeof(LocalizeTMProFontEvent)) as LocalizeTMProFontEvent;
var setFontMethod = target.GetType().GetProperty("font").GetSetMethod();
var methodDelegate = System.Delegate.CreateDelegate(typeof(UnityAction<TMP_FontAsset>), target, setFontMethod) as UnityAction<TMP_FontAsset>;
Events.UnityEventTools.AddPersistentListener(comp.OnUpdateAsset, methodDelegate);
//Find font table and set it up automatically
var collections = LocalizationEditorSettings.GetAssetTableCollections();
foreach (var tableCollection in collections)
{
if (tableCollection.name == "Fonts")
{
comp.AssetReference.TableReference = tableCollection.TableCollectionNameReference;
foreach (var entry in tableCollection.SharedData.Entries)
{
if (entry.Key == "font")
comp.AssetReference.TableEntryReference = entry.Id;
}
}
}
return null;
}
}
}

11
UOP1_Project/Assets/Scripts/Localization/LocalizeComponent_TMProFont.cs.meta


fileFormatVersion: 2
guid: a568b6cd30782a04d891cb0bca6a64a1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

22
UOP1_Project/Assets/Scripts/Localization/LocalizeTMProFontEvent.cs


using System;
using TMPro;
using UnityEngine.Events;
namespace UnityEngine.Localization.Components
{
/// <summary>
/// Component that can be used to Localize a TMP_FontAsset asset.
/// </summary>
[AddComponentMenu("Localization/Asset/Localize TMPro Font Event")]
public class LocalizeTMProFontEvent : LocalizedAssetEvent<TMP_FontAsset, LocalizedTMProFont, UnityEventFont>
{
}
[Serializable]
public class LocalizedTMProFont : LocalizedAsset<TMP_FontAsset>{ }
[Serializable]
public class UnityEventFont : UnityEvent<TMP_FontAsset> { }
}

11
UOP1_Project/Assets/Scripts/Localization/LocalizeTMProFontEvent.cs.meta


fileFormatVersion: 2
guid: d4e8edcf3197e7e409c5a72a3a934676
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存