Created
August 13, 2019 11:37
-
-
Save joonjoonjoon/fd2526ef62896f8c13d40f8bec76b387 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
using I2.Loc; | |
using TMPro; | |
using TMPro.EditorUtilities; | |
using DTLocalization.Internal; | |
public class TmproI2ExtensionFontAtlasGenerator : MonoBehaviour { | |
[MenuItem("Smoof/Regenerate Font Atlas", false, 155)] | |
public static void RegenerateFontAtlases() | |
{ | |
Dictionary<string, string> fonts = new Dictionary<string, string>(); | |
Dictionary<string, string> fontAtlases = new Dictionary<string, string>(); | |
for (int i = 0; i < LocalizationManager.GetTermData("Font").Languages.Length; i++) | |
{ | |
var fontPath = LocalizationManager.GetTermData("Font").Languages[i] + ""; | |
if(!fonts.ContainsKey(fontPath)) fonts[fontPath] = ""; | |
fonts[fontPath] += GetCharSet(i); | |
} | |
for (int i = 0; i < fonts.Keys.Count; i++) | |
{ | |
var key = fonts.Keys.ToArray()[i]; | |
var value = fonts[key]; | |
var path = "Assets/" + key; | |
Debug.Log("Creating atlas for " + key); | |
Debug.Log("Charset: " + value); | |
Debug.Log("Trying path: " + path); | |
var fontAsset = AssetDatabase.LoadAssetAtPath(path, typeof(Font)) as Font; | |
// NB: Fonts should not be in resources because build size! | |
if(fontAsset == null) | |
{ | |
Debug.Log("skipping... THIS IS BAD"); | |
continue; | |
} | |
else | |
{ | |
Debug.Log("Baking asset: "); | |
var outputFileNoExtension = "Font Atlases/" + fontAsset.name + " SDF (auto)"; | |
var outputFilePath = "Assets/Resources/" + outputFileNoExtension + ".asset"; | |
fontAtlases[key] = outputFileNoExtension; | |
TMPFontAssetBaker.Bake(fontAsset, true, 10, 7, TMPFontPackingModes.Fast, 1024,1024,FaceStyles.Normal, 1,RenderModes.DistanceField16, value, outputFilePath); | |
} | |
Debug.Log("Done!"); | |
} | |
RescueMaterials(); | |
for (int i = 0; i < LocalizationManager.GetTermData("Font").Languages.Length; i++) | |
{ | |
var fontPath = LocalizationManager.GetTermData("Font").Languages[i] + ""; | |
Debug.Log(fontPath); | |
LocalizationManager.GetTermData("Font Atlas").Languages[i] = fontAtlases[fontPath]; | |
} | |
} | |
public static void RescueMaterials() | |
{ | |
for (int i = 0; i < LocalizationManager.GetAllLanguages().Count; i++) | |
{ | |
var fontPath = LocalizationManager.GetTermData("Font Atlas").Languages[i] + ""; | |
var atlas = Resources.Load(fontPath, typeof(TMP_FontAsset)) as TMP_FontAsset; | |
var blackMat = Resources.Load(fontPath + " Black", typeof(Material)) as Material; | |
blackMat.SetTexture("_MainTex", atlas.material.GetTexture("_MainTex")); | |
EditorUtility.SetDirty(blackMat); | |
var defaultMat = Resources.Load(fontPath + " Default", typeof(Material)) as Material; | |
defaultMat.SetTexture("_MainTex", atlas.material.GetTexture("_MainTex")); | |
EditorUtility.SetDirty(defaultMat); | |
} | |
} | |
static string GetCharSet(int languageID) | |
{ | |
var sb = new List<char> (); | |
var source = LocalizationManager.Sources[0]; | |
foreach (var termData in source.mTerms) | |
{ | |
bool isRTL = LocalizationManager.IsRTL(source.mLanguages[languageID].Code); | |
AppendToCharSet(sb, termData.Languages[languageID], isRTL); | |
} | |
var charset = ""; | |
foreach (var c in sb.Distinct().ToArray()) | |
{ | |
charset += c; | |
} | |
return charset; | |
} | |
static void AppendToCharSet(List<char> sb, string text, bool isRTL ) | |
{ | |
if (string.IsNullOrEmpty (text)) | |
return; | |
text = RemoveTagsPrefix(text, "[i2p_"); | |
text = RemoveTagsPrefix(text, "[i2s_"); | |
if (isRTL) | |
text = RTLFixer.Fix( text ); | |
foreach (char c in text) | |
{ | |
sb.Add(char.ToLowerInvariant(c)); | |
sb.Add(char.ToUpperInvariant(c)); | |
} | |
} | |
static string RemoveTagsPrefix(string text, string tagPrefix) | |
{ | |
int idx = 0; | |
while (idx < text.Length) | |
{ | |
idx = text.IndexOf(tagPrefix); | |
if (idx < 0) | |
break; | |
int idx2 = text.IndexOf(']', idx); | |
if (idx2 < 0) | |
break; | |
text = text.Remove(idx, idx2 - idx+1); | |
} | |
return text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment