/// <summary> /// Creates a <see cref="TMP_FontAsset"/> from a Unity <see cref="Font"/>. /// </summary> /// <remarks> /// The <see cref="TMP_FontAsset"/> returned is not usable for UI text. Use <see cref="CreateFixedUIFontClone(TMP_FontAsset)"/> /// to get a usable font. /// </remarks> /// <param name="font">the Unity font to use</param> /// <returns>the new <see cref="TMP_FontAsset"/></returns> public static TMP_FontAsset CreateTMPFont(Font font, string nameOverride = null) { var tmpFont = TMP_FontAsset.CreateFontAsset(font); tmpFont.SetName(nameOverride ?? font.name); return(tmpFont); }
public static TMP_FontAsset TMPFontFromUnityFont(Font font) { var asset = TMP_FontAsset.CreateFontAsset(font); asset.name = font.name; asset.hashCode = TMP_TextUtilities.GetSimpleHashCode(asset.name); return(asset); }
public static TMP_FontAsset CreateFontAsset(Object target) { // Make sure the selection is a font file if (target == null || target.GetType() != typeof(Font)) { Debug.LogWarning("A Font file must first be selected in order to create a Font Asset."); return(null); } Font sourceFont = (Font)target; string sourceFontFilePath = AssetDatabase.GetAssetPath(target); string folderPath = Path.GetDirectoryName(sourceFontFilePath); string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath); string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " SDF.asset"); //// Create new TM Font Asset. TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(sourceFont);; AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName); // Initialize array for the font atlas textures. fontAsset.atlasTextures = new Texture2D[1]; // Create atlas texture of size zero. Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false); texture.name = assetName + " Atlas"; fontAsset.atlasTextures[0] = texture; AssetDatabase.AddObjectToAsset(texture, fontAsset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); Material tmp_material = new Material(default_Shader); tmp_material.name = texture.name + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, fontAsset.atlasWidth); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, fontAsset.atlasHeight); tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding); tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); // Not sure if this is still necessary in newer versions of Unity. EditorUtility.SetDirty(fontAsset); AssetDatabase.SaveAssets(); return(fontAsset); }
TMP_FontAsset ReadFont(string code) { string languageDirectoryPath = LanguageFilePath.LanguageDirectoryPath(code); var fontFiles = Directory.EnumerateFiles(languageDirectoryPath, "*.otf").Union(Directory.EnumerateFiles(languageDirectoryPath, "*.ttf")).Union(Directory.EnumerateFiles(languageDirectoryPath, "*.ttc")); var fontFile = fontFiles.FirstOrDefault(); if (string.IsNullOrEmpty(fontFile)) { return(null); } Font font = new Font(fontFile); TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(font); return(fontAsset); }
public TMP_FontAsset LoadFont(string ourFont) { fontPath = Path.Combine(Paths.BepInExRootPath, "plugins", "ClonFont", "Fonts", ourFont + ".ttf"); fontFile = new FileInfo(fontPath); TMP_FontAsset customFontAsset; if (fontFile.Exists && ourFont.ToLower() != "default") { customFont = new Font(fontPath); customFontAsset = TMP_FontAsset.CreateFontAsset(customFont); return(customFontAsset); } else { return(null); } }
public BloomFontProvider(HSVConfig hsvConfig) { _hsvConfig = hsvConfig; _cachedTekoFont = new Lazy <TMP_FontAsset>(() => Resources.FindObjectsOfTypeAll <TMP_FontAsset>().First(x => x.name.Contains("Teko-Medium SDF")), LazyThreadSafetyMode.ExecutionAndPublication); _bloomTekoFont = new Lazy <TMP_FontAsset>(() => { var distanceFieldShader = Resources.FindObjectsOfTypeAll <Shader>().First(x => x.name.Contains("TextMeshPro/Distance Field")); var bloomTekoFont = TMP_FontAsset.CreateFontAsset(_cachedTekoFont.Value.sourceFontFile); bloomTekoFont.name = "Teko-Medium SDF (Bloom)"; bloomTekoFont.material.shader = distanceFieldShader; return(bloomTekoFont); }, LazyThreadSafetyMode.ExecutionAndPublication); }
private TextMeshProUGUI CreateTmpText(Transform parent, string labelName, int height) { var o = new GameObject(labelName, new Type[] { typeof(TextMeshProUGUI) }); o.transform.SetParent(parent); o.transform.SetSiblingIndex(height); o.transform.localPosition = new Vector3(0, 0); TextMeshProUGUI text = o.GetComponent <TextMeshProUGUI>(); text.font = TMP_FontAsset.CreateFontAsset(uiFont); text.alignment = TextAlignmentOptions.MidlineJustified; text.color = Color.white; return(text); }
// Sets up all the languages internal static void Setup() { // Constructs new fonts for the game newFont = TMP_FontAsset.CreateFontAsset(Packs.Global.Get <Font>("CustomFont")); newFont.name = "CustomFont"; newFontHebrew = TMP_FontAsset.CreateFontAsset(Packs.Global.Get <Font>("CustomFontHebrew")); newFontHebrew.name = "CustomFontHebrew"; newFontArmenian = TMP_FontAsset.CreateFontAsset(Packs.Global.Get <Font>("CustomFontArmenian")); newFontArmenian.name = "CustomFontArmenian"; // Creates language fallback for language symbols // Marks languages as RTL to adjust them then loading LanguageController.AddRTLSupport(Enums.Langs.HE); // Corrects the language display on the language selection LangTranslations(null); LanguageController.TranslationReset += LangTranslations; }
void Start() { TMP_FontAsset fontAsset = null; // Create Dynamic Font Asset for the given font file. switch (Benchmark) { case BenchmarkType.TMP_SDF_MOBILE: fontAsset = TMP_FontAsset.CreateFontAsset(SourceFontFile, 90, 9, GlyphRenderMode.SDFAA, 256, 256, AtlasPopulationMode.Dynamic); break; case BenchmarkType.TMP_SDF__MOBILE_SSD: fontAsset = TMP_FontAsset.CreateFontAsset(SourceFontFile, 90, 9, GlyphRenderMode.SDFAA, 256, 256, AtlasPopulationMode.Dynamic); fontAsset.material.shader = Shader.Find("TextMeshPro/Mobile/Distance Field SSD"); break; case BenchmarkType.TMP_SDF: fontAsset = TMP_FontAsset.CreateFontAsset(SourceFontFile, 90, 9, GlyphRenderMode.SDFAA, 256, 256, AtlasPopulationMode.Dynamic); fontAsset.material.shader = Shader.Find("TextMeshPro/Distance Field"); break; case BenchmarkType.TMP_BITMAP_MOBILE: fontAsset = TMP_FontAsset.CreateFontAsset(SourceFontFile, 90, 9, GlyphRenderMode.SMOOTH, 256, 256, AtlasPopulationMode.Dynamic); break; } for (int i = 0; i < NumberOfSamples; i++) { switch (Benchmark) { case BenchmarkType.TMP_SDF_MOBILE: case BenchmarkType.TMP_SDF__MOBILE_SSD: case BenchmarkType.TMP_SDF: case BenchmarkType.TMP_BITMAP_MOBILE: { GameObject go = new GameObject(); go.transform.position = new Vector3(0, 1.2f, 0); TextMeshPro textComponent = go.AddComponent <TextMeshPro>(); textComponent.font = fontAsset; textComponent.fontSize = 128; textComponent.text = "@"; textComponent.alignment = TextAlignmentOptions.Center; textComponent.color = new Color32(255, 255, 0, 255); if (Benchmark == BenchmarkType.TMP_BITMAP_MOBILE) { textComponent.fontSize = 132; } } break; case BenchmarkType.TEXTMESH_BITMAP: { GameObject go = new GameObject(); go.transform.position = new Vector3(0, 1.2f, 0); TextMesh textMesh = go.AddComponent <TextMesh>(); textMesh.GetComponent <Renderer>().sharedMaterial = SourceFontFile.material; textMesh.font = SourceFontFile; textMesh.anchor = TextAnchor.MiddleCenter; textMesh.fontSize = 130; textMesh.color = new Color32(255, 255, 0, 255); textMesh.text = "@"; } break; } } }
public static TMP_FontAsset ToTMP_FontAsset(this Font font) { return(TMP_FontAsset.CreateFontAsset(font)); }