Created
June 2, 2020 07:50
-
-
Save fanhexin/ca1ed58c5e82f8991b1acc6610f3630e 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; | |
using System.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
namespace .Editor | |
{ | |
public static class CustomFontHelper | |
{ | |
[MenuItem("Assets/CreateCustomFont", true)] | |
static bool Validator() | |
{ | |
return Selection.objects.All(x => | |
{ | |
if (!(x is Texture2D)) return false; | |
var importer = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(x)) as TextureImporter; | |
return importer.textureType == TextureImporterType.Sprite && | |
importer.spriteImportMode == SpriteImportMode.Multiple; | |
}); | |
} | |
[MenuItem("Assets/CreateCustomFont")] | |
static void CreateCustomFont() | |
{ | |
foreach (string guid in Selection.assetGUIDs) | |
{ | |
string path = AssetDatabase.GUIDToAssetPath(guid); | |
var importer = TextureImporter.GetAtPath(path) as TextureImporter; | |
var group = importer.spritesheet | |
.OrderBy(x => x.name) | |
.GroupBy(x => x.name.Split('_')[0]); | |
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path); | |
foreach (var item in group) | |
{ | |
string savePath = Path.Combine(Path.GetDirectoryName(path), $"{item.Key}.fontsettings"); | |
CreateFont(savePath, tex, item.ToArray()); | |
} | |
} | |
AssetDatabase.SaveAssets(); | |
AssetDatabase.Refresh(); | |
} | |
static void CreateFont(string savePath, Texture2D tex, params SpriteMetaData[] spriteMetaDatas) | |
{ | |
int asciiStartOffset = Convert.ToChar(spriteMetaDatas[0].name.Split('_')[1]); | |
var font = new Font(); | |
using (var fontSo = new SerializedObject(font)) | |
{ | |
using (var matSp = fontSo.FindProperty("m_DefaultMaterial")) | |
{ | |
var mat = new Material(Canvas.GetDefaultCanvasMaterial().shader) {mainTexture = tex}; | |
string matPath = Path.Combine(Path.GetDirectoryName(savePath), | |
$"{Path.GetFileNameWithoutExtension(savePath)}.mat"); | |
AssetDatabase.CreateAsset(mat, matPath); | |
matSp.objectReferenceValue = mat; | |
} | |
using (var offsetSp = fontSo.FindProperty("m_AsciiStartOffset")) | |
{ | |
offsetSp.intValue = asciiStartOffset; | |
} | |
SerializedProperty characterRects = fontSo.FindProperty("m_CharacterRects"); | |
for (int i = 0; i < spriteMetaDatas.Length; i++) | |
{ | |
SpriteMetaData metaData = spriteMetaDatas[i]; | |
int index = characterRects.arraySize; | |
characterRects.InsertArrayElementAtIndex(index); | |
SerializedProperty item = characterRects.GetArrayElementAtIndex(index); | |
using (var indexSp = item.FindPropertyRelative("index")) | |
{ | |
indexSp.intValue = i; | |
} | |
using (var uvSp = item.FindPropertyRelative("uv")) | |
{ | |
float x = metaData.rect.x / tex.width; | |
float y = metaData.rect.y / tex.height; | |
float w = metaData.rect.width / tex.width; | |
float h = metaData.rect.height / tex.height; | |
uvSp.rectValue = new Rect(x, y, w, h); | |
} | |
using (var vertSp = item.FindPropertyRelative("vert")) | |
{ | |
vertSp.rectValue = new Rect(0, 0, metaData.rect.width, -metaData.rect.height); | |
} | |
using (var advanceSp = item.FindPropertyRelative("advance")) | |
{ | |
advanceSp.floatValue = metaData.rect.width; | |
} | |
} | |
fontSo.ApplyModifiedPropertiesWithoutUndo(); | |
} | |
AssetDatabase.CreateAsset(font, savePath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment