欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

Auto Create Editable Copy Font(Unity3D开发之二十二),editableunity3d

来源: 开发者 投稿于  被查看 1940 次 评论:134

Auto Create Editable Copy Font(Unity3D开发之二十二),editableunity3d


猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/48318879

Unity中咱们经常对字体做Create Editable Copy,虽然已经非常方便了,但是实际使用场景中,有时候会有不方便的地方。

已经不能忍了……

咱们脚本搞定这事,具体实现后效果
1. 用配置文件指明需要使用的font name,font size,导出的字符集内容
2. 根据配置文件Create Editable Copy,同时保证场景中已存在的组件不丢失字体

一、添加Editor脚本:

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using System.IO;

public  class CreateEditableCopyFont
{

    [MenuItem ("Assets/Create Editable Copy Font")]
    static void CreateFont ()
    {
        TextAsset sourceTextAsset = null;
        try {
            sourceTextAsset = (TextAsset)Selection.activeObject;
        } catch (InvalidCastException e) {
            Debug.Log ("Selected Object is not a txt file: " + Environment.NewLine + e.Message);
        }

        if (sourceTextAsset == null) {
            EditorUtility.DisplayDialog ("No Config selected", "Please select a TxtFile Config...\nSuch as Exsample.txt:\nname=Assets/GameAssets/Fonts/impact.ttf,size=40\n123456789abcdefghijk", "Cancel");
            return;
        }

        int targetFontSize;
        string sourceFontPath, targetFontPath, targetFontCharacters = "";
        try {
            string sourceConfigPath = AssetDatabase.GetAssetPath (Selection.activeObject);
            string[] sourceConfigInfos = sourceTextAsset.text.Split ('\n');
            string headInfo = sourceConfigInfos [0];
            string[] headInfos = headInfo.Split (',');

            sourceFontPath = headInfos [0].Split ('=') [1];
            targetFontPath = sourceConfigPath.Replace (".txt", "_copy");
            targetFontSize = int.Parse (headInfos [1].Split ('=') [1]);
            for (int i = 1; i < sourceConfigInfos.Length; i++) {
                targetFontCharacters += sourceConfigInfos [i];
            }
        } catch (Exception ex) {
            EditorUtility.DisplayDialog ("Config Error", "The config header data error...", "Cancel");
            return;
        }

        // 重新生成字体文件会导致场景中已存在的丢失,
        // 所以需要生成后再次赋值
        string[] targetFontPathInfos = targetFontPath.Split ('/');
        string textCheckName = targetFontPathInfos [targetFontPathInfos.Length - 1];
        List<Text> listTexts = new List<Text> ();
        foreach (Text text in UnityEngine.Object.FindObjectsOfType(typeof(Text))) {
            if (text.font.name == textCheckName) {
                listTexts.Add (text);
            }
        }       

        UnityEngine.Object f = AssetDatabase.LoadMainAssetAtPath (sourceFontPath);
        string path = AssetDatabase.GetAssetPath (f);
        TrueTypeFontImporter fontImporter = AssetImporter.GetAtPath (path) as TrueTypeFontImporter;
        fontImporter.fontTextureCase = FontTextureCase.CustomSet;
        fontImporter.customCharacters = targetFontCharacters;
        fontImporter.fontSize = targetFontSize;
        fontImporter.SaveAndReimport ();
        AssetDatabase.Refresh ();
        Font font = fontImporter.GenerateEditableFont (targetFontPath);

        foreach (Text item in listTexts) {
            item.font = font;
        }

        // 还原ttf设置
        fontImporter.fontTextureCase = FontTextureCase.Dynamic;
        fontImporter.SaveAndReimport ();
        AssetDatabase.Refresh ();

        System.GC.Collect ();
    }
}

二、添加配置文件

impact_40.txt

name=Assets/HEGameLib/Examples/Fonts/impact.ttf,size=40
123456789abcdefg,All Characters you want export.

注意,配置文件我弄的很简单,第一行是使用的原字体和大小,第二行开始是你的需要导出的字符集。

将该文件放到你需要生成字体的目录,比如:Assets/GameAssets/Fonts/ 下。

三、生成字体

这里写图片描述
右键第二步中放入的配置文件,在弹出菜单中点击 Create Editable Copy Font
该目录下会生成所需字体。

再次生成该字体,场景中已存在的字体不会丢失。


修改:
修复导出字体后为还原原始ttf文件设置(20150911)

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关频道:

用户评论