public void AddOrUpdateFont(tk2dFont font) { #if UNITY_EDITOR string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(font)); PruneGenericList(ref fontIndex); tk2dGenericIndexItem item = null; foreach (tk2dGenericIndexItem v in fontIndex) { if (v.assetGUID == guid) { item = v; break; } } if (item == null) // not found { item = new tk2dGenericIndexItem(guid); fontIndex.Add(item); } item.loadable = font.loadable; item.managed = (font.data == null) ? false : font.data.managedFont; item.dataGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(font.data)); #endif }
void DoGetFont() { if (_textMesh == null) { LogWarning("Missing tk2dTextMesh component: " + _textMesh.gameObject.name); return; } GameObject go = font.Value; if (go == null) { return; } tk2dFont _font = go.GetComponent <tk2dFont>(); if (_font == null) { return; } //unsupported yet, I don't think it's possible currently to get the font associated with a fontdata. Need to enquire with the guys. }
static void DoBMFontCreate() { string path = tk2dEditorUtility.CreateNewPrefab("Font"); if (path.Length != 0) { GameObject go = new GameObject(); tk2dFont font = go.AddComponent <tk2dFont>(); font.manageMaterial = true; font.version = tk2dFont.CURRENT_VERSION; if (tk2dCamera.Editor__Inst != null) { font.sizeDef.CopyFrom(tk2dSpriteCollectionSize.ForTk2dCamera(tk2dCamera.Editor__Inst)); } tk2dEditorUtility.SetGameObjectActive(go, false); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(path); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #else Object p = PrefabUtility.CreateEmptyPrefab(path); PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #endif GameObject.DestroyImmediate(go); // Select object Selection.activeObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); } }
public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation) { //--[ Localize Font Object ]---------- tk2dFont newFont = cmp.GetSecondaryTranslatedObj <tk2dFont>(ref mainTranslation, ref secondaryTranslation); if (newFont != null && mTarget.font != newFont) { mTarget.font = newFont.data; } if (mInitializeAlignment) { mInitializeAlignment = false; mOriginalAlignment = mTarget.anchor; } if (mainTranslation != null && mTarget.text != mainTranslation) { if (Localize.CurrentLocalizeComponent.CorrectAlignmentForRTL) { int align = (int)mTarget.anchor; if (align % 3 == 0) { mTarget.anchor = LocalizationManager.IsRight2Left ? mTarget.anchor + 2 : mOriginalAlignment; } else if (align % 3 == 2) { mTarget.anchor = LocalizationManager.IsRight2Left ? mTarget.anchor - 2 : mOriginalAlignment; } } mTarget.text = mainTranslation; } }
void DoSetFont() { if (_textMesh == null) { LogWarning("Missing tk2dTextMesh component: " + _textMesh.gameObject.name); return; } GameObject go = font.Value; if (go == null) { return; } tk2dFont _font = go.GetComponent <tk2dFont>(); if (_font == null) { return; } _textMesh.font = _font.data; _textMesh.renderer.material = _font.material; _textMesh.Init(true); }
public void CopyFrom(tk2dSpriteCollectionFont src) { active = src.active; bmFont = src.bmFont; texture = src.texture; dupeCaps = src.dupeCaps; flipTextureY = src.flipTextureY; charPadX = src.charPadX; data = src.data; editorData = src.editorData; }
public void AddFont(tk2dFont sc) { fonts.RemoveAll(item => item == null); foreach (var v in fonts) { if (v == sc) { return; } } fonts.Add(sc); }
bool ParseBMFont(string path, tk2dFontData fontData, tk2dFont source) { float scale = 2.0f * source.sizeDef.OrthoSize / source.sizeDef.TargetHeight; tk2dEditor.Font.Info fontInfo = tk2dEditor.Font.Builder.ParseBMFont(path); if (fontInfo != null) { return(tk2dEditor.Font.Builder.BuildFont(fontInfo, fontData, scale, source.charPadX, source.dupeCaps, source.flipTextureY, source.gradientTexture, source.gradientCount)); } else { return(false); } }
public override void OnInspectorGUI() { tk2dFont gen = (tk2dFont)target; EditorGUILayout.BeginVertical(); DrawDefaultInspector(); if (GUILayout.Button("Commit...")) { if (gen.bmFont == null || gen.texture == null || gen.material == null) { EditorUtility.DisplayDialog("BMFont", "Need an bmFont, texture and material bound to work", "Ok"); return; } if (gen.data == null) { string bmFontPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "data.prefab"); GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); go.active = false; Object p = EditorUtility.CreateEmptyPrefab(bmFontPath); EditorUtility.ReplacePrefab(go, p); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); gen.data = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(tk2dFontData)) as tk2dFontData; } ParseBMFont(AssetDatabase.GetAssetPath(gen.bmFont), gen.data, gen); gen.data.material = gen.material; // Rebuild assets already present in the scene tk2dTextMesh[] sprs = Resources.FindObjectsOfTypeAll(typeof(tk2dTextMesh)) as tk2dTextMesh[]; foreach (tk2dTextMesh spr in sprs) { spr.Init(); } EditorUtility.SetDirty(gen); EditorUtility.SetDirty(gen.data); } EditorGUILayout.EndVertical(); }
public void DoLocalize_tk2dTextMesh(string MainTranslation, string SecondaryTranslation) { //--[ Localize Font Object ]---------- tk2dFont newFont = GetSecondaryTranslatedObj <tk2dFont>(ref MainTranslation, ref SecondaryTranslation); if (newFont != null && mTarget_tk2dTextMesh.font != newFont) { mTarget_tk2dTextMesh.font = newFont.data; } if (!string.IsNullOrEmpty(MainTranslation) && mTarget_tk2dTextMesh.text != MainTranslation) { mTarget_tk2dTextMesh.text = MainTranslation; } }
public void CopyFrom(tk2dSpriteCollectionFont src) { this.active = src.active; this.bmFont = src.bmFont; this.texture = src.texture; this.dupeCaps = src.dupeCaps; this.flipTextureY = src.flipTextureY; this.charPadX = src.charPadX; this.data = src.data; this.editorData = src.editorData; this.materialId = src.materialId; this.gradientCount = src.gradientCount; this.gradientTexture = src.gradientTexture; this.useGradient = src.useGradient; }
public void CopyFrom(tk2dSpriteCollectionFont src) { active = src.active; bmFont = src.bmFont; texture = src.texture; dupeCaps = src.dupeCaps; flipTextureY = src.flipTextureY; charPadX = src.charPadX; data = src.data; editorData = src.editorData; materialId = src.materialId; gradientCount = src.gradientCount; gradientTexture = src.gradientTexture; useGradient = src.useGradient; }
static void DoBMFontCreate() { string path = tk2dEditorUtility.CreateNewPrefab("Font"); if (path != null) { GameObject go = new GameObject(); tk2dFont font = go.AddComponent <tk2dFont>(); font.manageMaterial = true; go.active = false; Object p = EditorUtility.CreateEmptyPrefab(path); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(go); tk2dEditorUtility.GetOrCreateIndex().AddFont(AssetDatabase.LoadAssetAtPath(path, typeof(tk2dFont)) as tk2dFont); tk2dEditorUtility.CommitIndex(); } }
public void DoLocalize_tk2dTextMesh(string MainTranslation, string SecondaryTranslation) { //--[ Localize Font Object ]---------- tk2dFont newFont = GetSecondaryTranslatedObj <tk2dFont>(ref MainTranslation, ref SecondaryTranslation); if (newFont != null && mTarget_tk2dTextMesh.font != newFont) { mTarget_tk2dTextMesh.font = newFont.data; } if (mInitializeAlignment) { mInitializeAlignment = false; mOriginalAlignmentTk2d = mTarget_tk2dTextMesh.anchor; } if (!string.IsNullOrEmpty(MainTranslation) && mTarget_tk2dTextMesh.text != MainTranslation) { if (Localize.CurrentLocalizeComponent.CorrectAlignmentForRTL) { int align = (int)mTarget_tk2dTextMesh.anchor; if (align % 3 == 0) { mTarget_tk2dTextMesh.anchor = LocalizationManager.IsRight2Left ? mTarget_tk2dTextMesh.anchor + 2 : mOriginalAlignmentTk2d; } else if (align % 3 == 2) { mTarget_tk2dTextMesh.anchor = LocalizationManager.IsRight2Left ? mTarget_tk2dTextMesh.anchor - 2 : mOriginalAlignmentTk2d; } } mTarget_tk2dTextMesh.text = MainTranslation; } }
static void DoBMFontCreate() { string path = tk2dEditorUtility.CreateNewPrefab("Font"); if (path.Length != 0) { GameObject go = new GameObject(); tk2dFont font = go.AddComponent <tk2dFont>(); font.manageMaterial = true; font.version = tk2dFont.CURRENT_VERSION; if (tk2dCamera.Editor__Inst != null) { font.sizeDef.CopyFrom(tk2dSpriteCollectionSize.ForTk2dCamera(tk2dCamera.Editor__Inst)); } tk2dEditorUtility.SetGameObjectActive(go, false); PrefabUtility.SaveAsPrefabAsset(go, path); GameObject.DestroyImmediate(go); // Select object Selection.activeObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); } }
static void DoBMFontCreate() { string path = tk2dEditorUtility.CreateNewPrefab("Font"); if (path.Length != 0) { GameObject go = new GameObject(); tk2dFont font = go.AddComponent <tk2dFont>(); font.manageMaterial = true; tk2dEditorUtility.SetGameObjectActive(go, false); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(path); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #else Object p = PrefabUtility.CreateEmptyPrefab(path); PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #endif GameObject.DestroyImmediate(go); // Select object Selection.activeObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); } }
public override void OnInspectorGUI() { tk2dFont gen = (tk2dFont)target; if (gen.proxyFont) { GUILayout.Label("This font is managed by a Sprite Collection"); return; } gen.Upgrade(); EditorGUILayout.BeginVertical(); DrawDefaultInspector(); tk2dGuiUtility.SpriteCollectionSize(gen.sizeDef); // Warning when texture is compressed if (gen.texture != null) { Texture2D tex = (Texture2D)gen.texture; if (tex && IsTextureCompressed(tex)) { int buttonPressed; if ((buttonPressed = tk2dGuiUtility.InfoBoxWithButtons( "Font texture appears to be compressed. " + "Quality will be lost and the texture may appear blocky in game.\n" + "Do you wish to change the format?", tk2dGuiUtility.WarningLevel.Warning, new string[] { "16bit", "Truecolor" } )) != -1) { if (buttonPressed == 0) { ConvertTextureToFormat(tex, TextureImporterFormat.Automatic16bit); } else { ConvertTextureToFormat(tex, TextureImporterFormat.AutomaticTruecolor); } } } } // Warning when gradient texture is compressed if (gen.gradientTexture != null && (gen.gradientTexture.format != TextureFormat.ARGB32 && gen.gradientTexture.format != TextureFormat.RGB24 && gen.gradientTexture.format != TextureFormat.RGBA32)) { if (tk2dGuiUtility.InfoBoxWithButtons( "The gradient texture should be truecolor for best quality. " + "Current format is " + gen.gradientTexture.format.ToString() + ".", tk2dGuiUtility.WarningLevel.Warning, new string[] { "Fix" } ) != -1) { ConvertTextureToFormat(gen.gradientTexture, TextureImporterFormat.AutomaticTruecolor); } } if (GUILayout.Button("Commit...")) { if (gen.bmFont == null || gen.texture == null) { EditorUtility.DisplayDialog("BMFont", "Need an bmFont and texture bound to work", "Ok"); return; } if (gen.material == null) { gen.material = new Material(GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked)); string materialPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "material.mat"); AssetDatabase.CreateAsset(gen.material, materialPath); } if (gen.data == null) { string bmFontPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "data.prefab"); GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); tk2dEditorUtility.SetGameObjectActive(go, false); Object p = PrefabUtility.CreateEmptyPrefab(bmFontPath); PrefabUtility.ReplacePrefab(go, p); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); gen.data = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(tk2dFontData)) as tk2dFontData; } ParseBMFont(AssetDatabase.GetAssetPath(gen.bmFont), gen.data, gen); if (gen.manageMaterial) { Shader s = GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked); if (gen.material.shader != s) { gen.material.shader = s; EditorUtility.SetDirty(gen.material); } if (gen.material.mainTexture != gen.texture) { gen.material.mainTexture = gen.texture; EditorUtility.SetDirty(gen.material); } if (gen.gradientTexture != null && gen.gradientTexture != gen.material.GetTexture("_GradientTex")) { gen.material.SetTexture("_GradientTex", gen.gradientTexture); EditorUtility.SetDirty(gen.material); } } gen.data.version = tk2dFontData.CURRENT_VERSION; gen.data.material = gen.material; gen.data.textureGradients = gen.gradientTexture != null; gen.data.gradientCount = gen.gradientCount; gen.data.gradientTexture = gen.gradientTexture; gen.data.invOrthoSize = 1.0f / gen.sizeDef.OrthoSize; gen.data.halfTargetHeight = gen.sizeDef.TargetHeight * 0.5f; // Rebuild assets already present in the scene tk2dTextMesh[] sprs = Resources.FindObjectsOfTypeAll(typeof(tk2dTextMesh)) as tk2dTextMesh[]; foreach (tk2dTextMesh spr in sprs) { spr.ForceBuild(); } EditorUtility.SetDirty(gen); EditorUtility.SetDirty(gen.data); // update index tk2dEditorUtility.GetOrCreateIndex().AddOrUpdateFont(gen); tk2dEditorUtility.CommitIndex(); } EditorGUILayout.EndVertical(); GUILayout.Space(64); }
bool ParseBMFont(string path, tk2dFontData bmFont, tk2dFont source) { IntFontInfo fontInfo = null; try { XmlDocument doc = new XmlDocument(); doc.Load(path); fontInfo = ParseBMFontXml(doc); } catch { fontInfo = ParseBMFontText(path); } if (fontInfo == null || fontInfo.chars.Count == 0) return false; float texWidth = fontInfo.scaleW; float texHeight = fontInfo.scaleH; float lineHeight = fontInfo.lineHeight; float scale = 2.0f * source.targetOrthoSize / source.targetHeight; bmFont.lineHeight = lineHeight * scale; // Get largest index int numCharacters = 0; foreach (var theChar in fontInfo.chars) { if (theChar.id > numCharacters) numCharacters = theChar.id; } tk2dFontChar[] chars = new tk2dFontChar[numCharacters]; int minChar = 65536; int maxCharWithinBounds = 0; int numLocalChars = 0; float largestWidth = 0.0f; foreach (var theChar in fontInfo.chars) { tk2dFontChar thisChar = new tk2dFontChar(); int id = theChar.id; int x = theChar.x; int y = theChar.y; int width = theChar.width; int height = theChar.height; int xoffset = theChar.xoffset; int yoffset = theChar.yoffset; int xadvance = theChar.xadvance + source.charPadX; // precompute required data float px = xoffset * scale; float py = (lineHeight - yoffset) * scale; thisChar.p0 = new Vector3(px, py, 0); thisChar.p1 = new Vector3(px + width * scale, py - height * scale, 0); if (source.flipTextureY) { thisChar.uv0 = new Vector2(x / texWidth, y / texHeight); thisChar.uv1 = new Vector2(thisChar.uv0.x + width / texWidth, thisChar.uv0.y + height / texHeight); } else { thisChar.uv0 = new Vector2(x / texWidth, 1.0f - y / texHeight); thisChar.uv1 = new Vector2(thisChar.uv0.x + width / texWidth, thisChar.uv0.y - height / texHeight); } thisChar.advance = xadvance * scale; largestWidth = Mathf.Max(thisChar.advance, largestWidth); // Needs gradient data if (source.gradientTexture != null) { // build it up assuming the first gradient float x0 = (float)(0.0f / source.gradientCount); float x1 = (float)(1.0f / source.gradientCount); float y0 = 1.0f; float y1 = 0.0f; // align to glyph if necessary thisChar.gradientUv = new Vector2[4]; thisChar.gradientUv[0] = new Vector2(x0, y0); thisChar.gradientUv[1] = new Vector2(x1, y0); thisChar.gradientUv[2] = new Vector2(x0, y1); thisChar.gradientUv[3] = new Vector2(x1, y1); } if (id < numCharacters) { maxCharWithinBounds = (id > maxCharWithinBounds) ? id : maxCharWithinBounds; minChar = (id < minChar) ? id : minChar; chars[id] = thisChar; ++numLocalChars; } } if (source.dupeCaps) { for (int uc = 'A'; uc <= 'Z'; ++uc) { int lc = uc + ('a' - 'A'); if (chars[lc] == null) chars[lc] = chars[uc]; else if (chars[uc] == null) chars[uc] = chars[lc]; } } // share null char, same pointer var nullChar = new tk2dFontChar(); bmFont.largestWidth = largestWidth; bmFont.chars = new tk2dFontChar[numCharacters]; for (int i = 0; i < numCharacters; ++i) { bmFont.chars[i] = chars[i]; if (bmFont.chars[i] == null) { bmFont.chars[i] = nullChar; // zero everything, null char } } // kerning bmFont.kerning = new tk2dFontKerning[fontInfo.kernings.Count]; for (int i = 0; i < bmFont.kerning.Length; ++i) { tk2dFontKerning kerning = new tk2dFontKerning(); kerning.c0 = fontInfo.kernings[i].first; kerning.c1 = fontInfo.kernings[i].second; kerning.amount = fontInfo.kernings[i].amount * scale; bmFont.kerning[i] = kerning; } return true; }
public bool Draw(List <SpriteCollectionEditorEntry> selectedEntries) { if (selectedEntries.Count == 0 || selectedEntries[0].type != SpriteCollectionEditorEntry.Type.Font) { return(false); } var entry = selectedEntries[selectedEntries.Count - 1]; var font = SpriteCollection.fonts[entry.index]; bool doDelete = false; GUILayout.BeginHorizontal(); // Body GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true)); fontTextureScrollBar = GUILayout.BeginScrollView(fontTextureScrollBar); if (font.texture != null) { font.texture.filterMode = FilterMode.Point; int border = 16; Rect rect = GUILayoutUtility.GetRect(border + font.texture.width, border + font.texture.height, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); tk2dGrid.Draw(rect); GUI.Label(new Rect(border + rect.x, border + rect.y, font.texture.width, font.texture.height), font.texture); } GUILayout.EndScrollView(); GUILayout.EndVertical(); // Inspector EditorGUIUtility.LookLikeControls(100.0f, 100.0f); fontEditorScrollBar = GUILayout.BeginScrollView(fontEditorScrollBar, GUILayout.ExpandHeight(true), GUILayout.Width(host.InspectorWidth)); // Header GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorHeaderBG, GUILayout.ExpandWidth(true)); Object newBmFont = EditorGUILayout.ObjectField("BM Font", font.bmFont, typeof(Object), false); if (newBmFont != font.bmFont) { font.texture = null; entry.name = "Empty"; font.bmFont = newBmFont; if (newBmFont != null) { string bmFontPath = AssetDatabase.GetAssetPath(newBmFont); tk2dEditor.Font.Info fontInfo = tk2dEditor.Font.Builder.ParseBMFont(bmFontPath); if (fontInfo != null && fontInfo.texturePaths.Length > 0) { string path = System.IO.Path.GetDirectoryName(bmFontPath).Replace('\\', '/') + "/" + System.IO.Path.GetFileName(fontInfo.texturePaths[0]);; font.texture = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; } entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } } GUILayout.BeginHorizontal(); Texture2D newTexture = EditorGUILayout.ObjectField("Font Texture", font.texture, typeof(Texture2D), false) as Texture2D; if (newTexture != font.texture) { font.texture = newTexture; entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } GUILayout.FlexibleSpace(); if (GUILayout.Button("Delete", EditorStyles.miniButton)) { doDelete = true; } GUILayout.EndHorizontal(); GUILayout.EndVertical(); // Rest of inspector GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true)); if (font.texture != null) { string assetPath = AssetDatabase.GetAssetPath(font.texture); if (assetPath.Length > 0) { // make sure the source texture is npot and readable, and uncompressed if (!tk2dSpriteCollectionBuilder.IsTextureImporterSetUp(assetPath)) { if (tk2dGuiUtility.InfoBoxWithButtons( "The texture importer needs to be reconfigured to be used as a font texture source. " + "Please note that this will globally change this texture importer. ", tk2dGuiUtility.WarningLevel.Info, "Set up") != -1) { tk2dSpriteCollectionBuilder.ConfigureSpriteTextureImporter(assetPath); AssetDatabase.ImportAsset(assetPath); } } } } if (SpriteCollection.AllowAltMaterials && SpriteCollection.altMaterials.Length > 1) { List <int> altMaterialIndices = new List <int>(); List <string> altMaterialNames = new List <string>(); for (int i = 0; i < SpriteCollection.altMaterials.Length; ++i) { var mat = SpriteCollection.altMaterials[i]; if (mat == null) { continue; } altMaterialIndices.Add(i); altMaterialNames.Add(mat.name); } font.materialId = EditorGUILayout.IntPopup("Material", font.materialId, altMaterialNames.ToArray(), altMaterialIndices.ToArray()); } if (font.data == null || font.editorData == null) { if (tk2dGuiUtility.InfoBoxWithButtons( "A data object is required to build a font. " + "Please create one or drag an existing data object into the inspector slot.\n", tk2dGuiUtility.WarningLevel.Info, "Create") != -1) { // make data folder string root = SpriteCollection.GetOrCreateDataPath(); string name = font.bmFont?font.bmFont.name:"Unknown Font"; string editorDataPath = tk2dGuiUtility.SaveFileInProject("Save Font Data", root, name, "prefab"); if (editorDataPath.Length > 0) { int prefabOffset = editorDataPath.ToLower().IndexOf(".prefab"); string dataObjectPath = editorDataPath.Substring(0, prefabOffset) + " data.prefab"; // Create data object { GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); tk2dEditorUtility.SetGameObjectActive(go, false); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(dataObjectPath); EditorUtility.ReplacePrefab(go, p); #else Object p = PrefabUtility.CreateEmptyPrefab(dataObjectPath); PrefabUtility.ReplacePrefab(go, p); #endif GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); font.data = AssetDatabase.LoadAssetAtPath(dataObjectPath, typeof(tk2dFontData)) as tk2dFontData; } // Create editor object { GameObject go = new GameObject(); tk2dFont f = go.AddComponent <tk2dFont>(); f.proxyFont = true; f.data = font.data; tk2dEditorUtility.SetGameObjectActive(go, false); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(editorDataPath); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #else Object p = PrefabUtility.CreateEmptyPrefab(editorDataPath); PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #endif GameObject.DestroyImmediate(go); tk2dFont loadedFont = AssetDatabase.LoadAssetAtPath(editorDataPath, typeof(tk2dFont)) as tk2dFont; tk2dEditorUtility.GetOrCreateIndex().AddOrUpdateFont(loadedFont); tk2dEditorUtility.CommitIndex(); font.editorData = AssetDatabase.LoadAssetAtPath(editorDataPath, typeof(tk2dFont)) as tk2dFont; } entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } } } else { font.editorData = EditorGUILayout.ObjectField("Editor Data", font.editorData, typeof(tk2dFont), false) as tk2dFont; font.data = EditorGUILayout.ObjectField("Font Data", font.data, typeof(tk2dFontData), false) as tk2dFontData; } if (font.data && font.editorData) { font.useGradient = EditorGUILayout.Toggle("Use Gradient", font.useGradient); if (font.useGradient) { EditorGUI.indentLevel++; Texture2D tex = EditorGUILayout.ObjectField("Gradient Tex", font.gradientTexture, typeof(Texture2D), false) as Texture2D; if (font.gradientTexture != tex) { font.gradientTexture = tex; List <Material> materials = new List <Material>(); materials.Add(SpriteCollection.altMaterials[font.materialId]); for (int j = 0; j < SpriteCollection.platforms.Count; ++j) { if (!SpriteCollection.platforms[j].Valid) { continue; } tk2dSpriteCollection data = SpriteCollection.platforms[j].spriteCollection; materials.Add(data.altMaterials[font.materialId]); } for (int j = 0; j < materials.Count; ++j) { if (!materials[j].HasProperty("_GradientTex")) { Debug.LogError(string.Format("Cant find parameter '_GradientTex' in material '{0}'", materials[j].name)); } else if (materials[j].GetTexture("_GradientTex") != tex) { materials[j].SetTexture("_GradientTex", font.gradientTexture); EditorUtility.SetDirty(materials[j]); } } } font.gradientCount = EditorGUILayout.IntField("Gradient Count", font.gradientCount); EditorGUI.indentLevel--; } } //font.dupeCaps = EditorGUILayout.Toggle("Dupe caps", font.dupeCaps); font.flipTextureY = EditorGUILayout.Toggle("Flip Texture Y", font.flipTextureY); font.charPadX = EditorGUILayout.IntField("Char Pad X", font.charPadX); GUILayout.EndVertical(); GUILayout.EndScrollView(); // make dragable tk2dPreferences.inst.spriteCollectionInspectorWidth -= (int)tk2dGuiUtility.DragableHandle(4819284, GUILayoutUtility.GetLastRect(), 0, tk2dGuiUtility.DragDirection.Horizontal); GUILayout.EndHorizontal(); if (doDelete && EditorUtility.DisplayDialog("Delete sprite", "Are you sure you want to delete the selected font?", "Yes", "No")) { font.active = false; font.bmFont = null; font.data = null; font.texture = null; SpriteCollection.Trim(); host.OnSpriteCollectionChanged(false); } return(true); }
public override void OnInspectorGUI() { tk2dFont gen = (tk2dFont)target; if (gen.proxyFont) { GUILayout.Label("This font is managed by a Sprite Collection"); return; } gen.Upgrade(); EditorGUILayout.BeginVertical(); DrawDefaultInspector(); tk2dGuiUtility.SpriteCollectionSize(gen.sizeDef); // Warning when texture is compressed if (gen.texture != null) { Texture2D tex = (Texture2D)gen.texture; if (tex && IsTextureCompressed(tex)) { int buttonPressed; if ((buttonPressed = tk2dGuiUtility.InfoBoxWithButtons( "Font texture appears to be compressed. " + "Quality will be lost and the texture may appear blocky in game.\n" + "Do you wish to change the format?", tk2dGuiUtility.WarningLevel.Warning, new string[] { "Truecolor" } )) != -1) { if (buttonPressed == 0) { ConvertTextureToUncompressed(tex); } } } } // Warning when gradient texture is compressed if (gen.gradientTexture != null && (gen.gradientTexture.format != TextureFormat.ARGB32 && gen.gradientTexture.format != TextureFormat.RGB24 && gen.gradientTexture.format != TextureFormat.RGBA32)) { if (tk2dGuiUtility.InfoBoxWithButtons( "The gradient texture should be truecolor for best quality. " + "Current format is " + gen.gradientTexture.format.ToString() + ".", tk2dGuiUtility.WarningLevel.Warning, new string[] { "Fix" } ) != -1) { ConvertTextureToUncompressed(gen.gradientTexture); } } string message = @"Due to changes in the prefab system in Unity 2018.3, the commit functionality has been moved." + "Exit prefab edit mode, select your font collection and click 2D Toolikt / Commit Font in the main menu"; tk2dGuiUtility.InfoBox(message, tk2dGuiUtility.WarningLevel.Warning); GUI.enabled = false; if (GUILayout.Button("Commit...")) { } GUI.enabled = true; EditorGUILayout.EndVertical(); GUILayout.Space(64); }
public void AddFont(tk2dFont sc) { fonts.RemoveAll(item => item == null); foreach (var v in fonts) if (v == sc) return; fonts.Add(sc); }
public bool Draw(List <SpriteCollectionEditorEntry> selectedEntries) { if (selectedEntries.Count == 0 || selectedEntries[0].type != SpriteCollectionEditorEntry.Type.Font) { return(false); } var entry = selectedEntries[selectedEntries.Count - 1]; var font = SpriteCollection.fonts[entry.index]; bool doDelete = false; GUILayout.BeginHorizontal(); // Body GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true)); if (font.texture != null) { font.texture.filterMode = FilterMode.Point; GUILayout.Label(font.texture); } GUILayout.EndVertical(); // Inspector EditorGUIUtility.LookLikeControls(100.0f, 100.0f); fontEditorScrollBar = GUILayout.BeginScrollView(fontEditorScrollBar, GUILayout.ExpandHeight(true), GUILayout.Width(host.InspectorWidth)); // Header GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorHeaderBG, GUILayout.ExpandWidth(true)); Object newBmFont = EditorGUILayout.ObjectField("BM Font", font.bmFont, typeof(Object), false); if (newBmFont != font.bmFont) { font.bmFont = newBmFont; entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } GUILayout.BeginHorizontal(); Texture2D newTexture = EditorGUILayout.ObjectField("Font Texture", font.texture, typeof(Texture2D), false) as Texture2D; if (newTexture != font.texture) { font.texture = newTexture; entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } GUILayout.FlexibleSpace(); if (GUILayout.Button("Delete", EditorStyles.miniButton)) { doDelete = true; } GUILayout.EndHorizontal(); GUILayout.EndVertical(); // Rest of inspector GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true)); if (font.texture != null) { string assetPath = AssetDatabase.GetAssetPath(font.texture); if (assetPath.Length > 0) { // make sure the source texture is npot and readable, and uncompressed if (!tk2dSpriteCollectionBuilder.IsTextureImporterSetUp(assetPath)) { if (tk2dGuiUtility.InfoBoxWithButtons( "The texture importer needs to be reconfigured to be used as a font texture source. " + "Please note that this will globally change this texture importer. ", tk2dGuiUtility.WarningLevel.Info, "Set up") != -1) { tk2dSpriteCollectionBuilder.ConfigureSpriteTextureImporter(assetPath); AssetDatabase.ImportAsset(assetPath); } } } } if (font.data == null || font.editorData == null) { if (tk2dGuiUtility.InfoBoxWithButtons( "A data object is required to build a font. " + "Please create one or drag an existing data object into the inspector slot.\n", tk2dGuiUtility.WarningLevel.Info, "Create") != -1) { string name = font.bmFont?font.bmFont.name:"Unknown Font"; string editorDataPath = EditorUtility.SaveFilePanelInProject("Save Font Data", name, "prefab", ""); if (editorDataPath.Length > 0) { int prefabOffset = editorDataPath.ToLower().IndexOf(".prefab"); string dataObjectPath = editorDataPath.Substring(0, prefabOffset) + "data.prefab"; // Create data object { GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); go.active = false; #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(dataObjectPath); EditorUtility.ReplacePrefab(go, p); #else Object p = PrefabUtility.CreateEmptyPrefab(dataObjectPath); PrefabUtility.ReplacePrefab(go, p); #endif GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); font.data = AssetDatabase.LoadAssetAtPath(dataObjectPath, typeof(tk2dFontData)) as tk2dFontData; } // Create editor object { GameObject go = new GameObject(); tk2dFont f = go.AddComponent <tk2dFont>(); f.proxyFont = true; f.data = font.data; go.active = false; #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(editorDataPath); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #else Object p = PrefabUtility.CreateEmptyPrefab(editorDataPath); PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); #endif GameObject.DestroyImmediate(go); tk2dEditorUtility.GetOrCreateIndex().AddFont(AssetDatabase.LoadAssetAtPath(editorDataPath, typeof(tk2dFont)) as tk2dFont); tk2dEditorUtility.CommitIndex(); font.editorData = AssetDatabase.LoadAssetAtPath(editorDataPath, typeof(tk2dFont)) as tk2dFont; } entry.name = font.Name; host.OnSpriteCollectionSortChanged(); } } } else { font.editorData = EditorGUILayout.ObjectField("Editor Data", font.editorData, typeof(tk2dFont), false) as tk2dFont; font.data = EditorGUILayout.ObjectField("Font Data", font.data, typeof(tk2dFontData), false) as tk2dFontData; } //font.dupeCaps = EditorGUILayout.Toggle("Dupe caps", font.dupeCaps); font.flipTextureY = EditorGUILayout.Toggle("Flip Texture Y", font.flipTextureY); font.charPadX = EditorGUILayout.IntField("Char Pad X", font.charPadX); GUILayout.EndVertical(); GUILayout.EndScrollView(); GUILayout.EndHorizontal(); if (doDelete && EditorUtility.DisplayDialog("Delete sprite", "Are you sure you want to delete the selected font?", "Yes", "No")) { font.active = false; font.bmFont = null; font.data = null; font.texture = null; SpriteCollection.Trim(); host.OnSpriteCollectionChanged(false); } return(true); }
public static void Build(tk2dFont gen) { if (gen.bmFont == null || gen.texture == null) { EditorUtility.DisplayDialog("BMFont", "Need an bmFont and texture bound to work", "Ok"); return; } if (gen.material == null) { gen.material = new Material(GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked)); string materialPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "material.mat"); AssetDatabase.CreateAsset(gen.material, materialPath); } if (gen.data == null) { string bmFontPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "data.prefab"); GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); tk2dEditorUtility.SetGameObjectActive(go, false); PrefabUtility.SaveAsPrefabAsset(go, bmFontPath); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); gen.data = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(tk2dFontData)) as tk2dFontData; } ParseBMFont(AssetDatabase.GetAssetPath(gen.bmFont), gen.data, gen); if (gen.manageMaterial) { Shader s = GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked); if (gen.material.shader != s) { gen.material.shader = s; tk2dUtil.SetDirty(gen.material); } if (gen.material.mainTexture != gen.texture) { gen.material.mainTexture = gen.texture; tk2dUtil.SetDirty(gen.material); } if (gen.gradientTexture != null && gen.gradientTexture != gen.material.GetTexture("_GradientTex")) { gen.material.SetTexture("_GradientTex", gen.gradientTexture); tk2dUtil.SetDirty(gen.material); } } gen.data.version = tk2dFontData.CURRENT_VERSION; gen.data.material = gen.material; gen.data.textureGradients = gen.gradientTexture != null; gen.data.gradientCount = gen.gradientCount; gen.data.gradientTexture = gen.gradientTexture; gen.data.invOrthoSize = 1.0f / gen.sizeDef.OrthoSize; gen.data.halfTargetHeight = gen.sizeDef.TargetHeight * 0.5f; // Rebuild assets already present in the scene tk2dTextMesh[] sprs = Resources.FindObjectsOfTypeAll(typeof(tk2dTextMesh)) as tk2dTextMesh[]; foreach (tk2dTextMesh spr in sprs) { spr.Init(true); } tk2dUtil.SetDirty(gen); tk2dUtil.SetDirty(gen.data); // update index tk2dEditorUtility.GetOrCreateIndex().AddOrUpdateFont(gen); tk2dEditorUtility.CommitIndex(); }
static void CreateIndex() { GameObject go = new GameObject(); tk2dIndex newIndex = go.AddComponent <tk2dIndex>(); go.active = false; List <string> rebuildSpriteCollectionPaths = new List <string>(); // check all prefabs to see if we can find any objects we are interested in List <string> allPrefabPaths = new List <string>(); Stack <string> paths = new Stack <string>(); paths.Push(Application.dataPath); while (paths.Count != 0) { string path = paths.Pop(); string[] files = Directory.GetFiles(path, "*.prefab"); foreach (var file in files) { allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6)); } foreach (string subdirs in Directory.GetDirectories(path)) { paths.Push(subdirs); } } // Check all prefabs int currPrefabCount = 1; foreach (string prefabPath in allPrefabPaths) { EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count)); GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; if (!iterGo) { continue; } tk2dSpriteCollection spriteCollection = iterGo.GetComponent <tk2dSpriteCollection>(); tk2dFont font = iterGo.GetComponent <tk2dFont>(); tk2dSpriteAnimation anims = iterGo.GetComponent <tk2dSpriteAnimation>(); if (spriteCollection) { tk2dSpriteCollectionData spriteCollectionData = spriteCollection.spriteCollection; if (spriteCollectionData) { if (spriteCollectionData.version < 1) { rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection)); } newIndex.AddSpriteCollectionData(spriteCollectionData); } } else if (font) { newIndex.AddFont(font); } else if (anims) { newIndex.AddSpriteAnimation(anims); } else { iterGo = null; System.GC.Collect(); } tk2dEditorUtility.UnloadUnusedAssets(); ++currPrefabCount; } EditorUtility.ClearProgressBar(); // Create index Object p = EditorUtility.CreateEmptyPrefab(indexPath); EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(go); // unload all unused assets tk2dEditorUtility.UnloadUnusedAssets(); // Rebuild invalid sprite collections if (rebuildSpriteCollectionPaths.Count > 0) { EditorUtility.DisplayDialog("Upgrade required", "Please wait while your sprite collection is upgraded.", "Ok"); int count = 1; foreach (var scPath in rebuildSpriteCollectionPaths) { tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection; EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count)); tk2dSpriteCollectionBuilder.Rebuild(sc); sc = null; tk2dEditorUtility.UnloadUnusedAssets(); ++count; } EditorUtility.ClearProgressBar(); } }
public override void OnInspectorGUI() { tk2dFont gen = (tk2dFont)target; EditorGUILayout.BeginVertical(); DrawDefaultInspector(); if (GUILayout.Button("Commit...")) { if (gen.bmFont == null || gen.texture == null) { EditorUtility.DisplayDialog("BMFont", "Need an bmFont and texture bound to work", "Ok"); return; } if (gen.material == null) { gen.material = new Material(GetShader(gen.gradientTexture != null)); string materialPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "material.mat"); AssetDatabase.CreateAsset(gen.material, materialPath); } if (gen.data == null) { string bmFontPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "data.prefab"); GameObject go = new GameObject(); go.AddComponent <tk2dFontData>(); go.active = false; #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_4) Object p = EditorUtility.CreateEmptyPrefab(bmFontPath); EditorUtility.ReplacePrefab(go, p); #else Object p = PrefabUtility.CreateEmptyPrefab(bmFontPath); PrefabUtility.ReplacePrefab(go, p); #endif GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); gen.data = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(tk2dFontData)) as tk2dFontData; } ParseBMFont(AssetDatabase.GetAssetPath(gen.bmFont), gen.data, gen); if (gen.manageMaterial) { Shader s = GetShader(gen.gradientTexture != null); if (gen.material.shader != s) { gen.material.shader = s; EditorUtility.SetDirty(gen.material); } if (gen.material.mainTexture != gen.texture) { gen.material.mainTexture = gen.texture; EditorUtility.SetDirty(gen.material); } if (gen.gradientTexture != null && gen.gradientTexture != gen.material.GetTexture("_GradientTex")) { gen.material.SetTexture("_GradientTex", gen.gradientTexture); EditorUtility.SetDirty(gen.material); } } gen.data.material = gen.material; gen.data.textureGradients = gen.gradientTexture != null; gen.data.gradientCount = gen.gradientCount; gen.data.gradientTexture = gen.gradientTexture; // Rebuild assets already present in the scene tk2dTextMesh[] sprs = Resources.FindObjectsOfTypeAll(typeof(tk2dTextMesh)) as tk2dTextMesh[]; foreach (tk2dTextMesh spr in sprs) { spr.Init(true); } EditorUtility.SetDirty(gen); EditorUtility.SetDirty(gen.data); } EditorGUILayout.EndVertical(); }
static void CreateIndex() { tk2dIndex newIndex = ScriptableObject.CreateInstance <tk2dIndex>(); newIndex.version = tk2dIndex.CURRENT_VERSION; newIndex.hideFlags = HideFlags.DontSave; // get this to not be destroyed in Unity 4.1 List <string> rebuildSpriteCollectionPaths = new List <string>(); // check all prefabs to see if we can find any objects we are interested in List <string> allPrefabPaths = new List <string>(); Stack <string> paths = new Stack <string>(); paths.Push(Application.dataPath); while (paths.Count != 0) { string path = paths.Pop(); string[] files = Directory.GetFiles(path, "*.prefab"); foreach (var file in files) { allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6)); } foreach (string subdirs in Directory.GetDirectories(path)) { paths.Push(subdirs); } } // Check all prefabs int currPrefabCount = 1; foreach (string prefabPath in allPrefabPaths) { EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count)); GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; if (!iterGo) { continue; } tk2dSpriteCollection spriteCollection = iterGo.GetComponent <tk2dSpriteCollection>(); tk2dSpriteCollectionData spriteCollectionData = iterGo.GetComponent <tk2dSpriteCollectionData>(); tk2dFont font = iterGo.GetComponent <tk2dFont>(); tk2dSpriteAnimation anim = iterGo.GetComponent <tk2dSpriteAnimation>(); if (spriteCollection) { tk2dSpriteCollectionData thisSpriteCollectionData = spriteCollection.spriteCollection; if (thisSpriteCollectionData) { if (thisSpriteCollectionData.version < 1) { rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection)); } newIndex.AddSpriteCollectionData(thisSpriteCollectionData); } } else if (spriteCollectionData) { string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteCollectionData)); bool present = false; foreach (var v in newIndex.GetSpriteCollectionIndex()) { if (v.spriteCollectionDataGUID == guid) { present = true; break; } } if (!present && guid != "") { newIndex.AddSpriteCollectionData(spriteCollectionData); } } else if (font) { newIndex.AddOrUpdateFont(font); // unfortunate but necessary } else if (anim) { newIndex.AddSpriteAnimation(anim); } else { iterGo = null; System.GC.Collect(); } tk2dEditorUtility.UnloadUnusedAssets(); ++currPrefabCount; } EditorUtility.ClearProgressBar(); // Create index newIndex.hideFlags = 0; // to save it AssetDatabase.CreateAsset(newIndex, indexPath); AssetDatabase.SaveAssets(); // unload all unused assets tk2dEditorUtility.UnloadUnusedAssets(); // Rebuild invalid sprite collections if (rebuildSpriteCollectionPaths.Count > 0) { EditorUtility.DisplayDialog("Upgrade required", "Please wait while your sprite collection is upgraded.", "Ok"); int count = 1; foreach (var scPath in rebuildSpriteCollectionPaths) { tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection; EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count)); tk2dSpriteCollectionBuilder.Rebuild(sc); sc = null; tk2dEditorUtility.UnloadUnusedAssets(); ++count; } EditorUtility.ClearProgressBar(); } index = newIndex; tk2dSpriteGuiUtility.ResetCache(); }
bool ParseBMFont(string path, tk2dFontData bmFont, tk2dFont source) { IntFontInfo fontInfo = null; try { XmlDocument doc = new XmlDocument(); doc.Load(path); fontInfo = ParseBMFontXml(doc); } catch { fontInfo = ParseBMFontText(path); } if (fontInfo == null || fontInfo.chars.Count == 0) { return(false); } float texWidth = fontInfo.scaleW; float texHeight = fontInfo.scaleH; float lineHeight = fontInfo.lineHeight; float scale = 2.0f * source.targetOrthoSize / source.targetHeight; bmFont.lineHeight = lineHeight * scale; tk2dFontChar[] chars = new tk2dFontChar[source.numCharacters]; int minChar = 65536; int maxCharWithinBounds = 0; int numLocalChars = 0; float largestWidth = 0.0f; foreach (var theChar in fontInfo.chars) { tk2dFontChar thisChar = new tk2dFontChar(); int id = theChar.id; int x = theChar.x; int y = theChar.y; int width = theChar.width; int height = theChar.height; int xoffset = theChar.xoffset; int yoffset = theChar.yoffset; int xadvance = theChar.xadvance; // precompute required data float px = xoffset * scale; float py = (lineHeight - yoffset) * scale; thisChar.p0 = new Vector3(px, py, 0); thisChar.p1 = new Vector3(px + width * scale, py - height * scale, 0); if (source.flipTextureY) { thisChar.uv0 = new Vector2(x / texWidth, y / texHeight); thisChar.uv1 = new Vector2(thisChar.uv0.x + width / texWidth, thisChar.uv0.y + height / texHeight); } else { thisChar.uv0 = new Vector2(x / texWidth, 1.0f - y / texHeight); thisChar.uv1 = new Vector2(thisChar.uv0.x + width / texWidth, thisChar.uv0.y - height / texHeight); } thisChar.advance = xadvance * scale; largestWidth = Mathf.Max(thisChar.advance, largestWidth); // Needs gradient data if (source.gradientTexture != null) { // build it up assuming the first gradient float x0 = (float)(0.0f / source.gradientCount); float x1 = (float)(1.0f / source.gradientCount); float y0 = 1.0f; float y1 = 0.0f; // align to glyph if necessary thisChar.gradientUv = new Vector2[4]; thisChar.gradientUv[0] = new Vector2(x0, y0); thisChar.gradientUv[1] = new Vector2(x1, y0); thisChar.gradientUv[2] = new Vector2(x0, y1); thisChar.gradientUv[3] = new Vector2(x1, y1); } if (id < source.numCharacters) { maxCharWithinBounds = (id > maxCharWithinBounds) ? id : maxCharWithinBounds; minChar = (id < minChar) ? id : minChar; chars[id] = thisChar; ++numLocalChars; } } if (source.dupeCaps) { for (int uc = 'A'; uc <= 'Z'; ++uc) { int lc = uc + ('a' - 'A'); if (chars[lc] == null) { chars[lc] = chars[uc]; } else if (chars[uc] == null) { chars[uc] = chars[lc]; } } } bmFont.largestWidth = largestWidth; bmFont.chars = new tk2dFontChar[source.numCharacters]; for (int i = 0; i < source.numCharacters; ++i) { bmFont.chars[i] = chars[i]; if (bmFont.chars[i] == null) { bmFont.chars[i] = new tk2dFontChar(); // zero everything, null char } } // kerning bmFont.kerning = new tk2dFontKerning[fontInfo.kernings.Count]; for (int i = 0; i < bmFont.kerning.Length; ++i) { tk2dFontKerning kerning = new tk2dFontKerning(); kerning.c0 = fontInfo.kernings[i].first; kerning.c1 = fontInfo.kernings[i].second; kerning.amount = fontInfo.kernings[i].amount * scale; bmFont.kerning[i] = kerning; } return(true); }
bool ParseBMFont(string path, tk2dFontData fontData, tk2dFont source) { float scale = 2.0f * source.sizeDef.OrthoSize / source.sizeDef.TargetHeight; tk2dEditor.Font.Info fontInfo = tk2dEditor.Font.Builder.ParseBMFont(path); if (fontInfo != null) return tk2dEditor.Font.Builder.BuildFont(fontInfo, fontData, scale, source.charPadX, source.dupeCaps, source.flipTextureY, source.gradientTexture, source.gradientCount); else return false; }