美文网首页
Unity制作艺术字

Unity制作艺术字

作者: 沉麟 | 来源:发表于2018-11-24 11:30 被阅读0次

    美术给了一些艺术字母和数字的图片,unity不能直接用,上网查了一下bmfront转一下字体
    有的图片可能美术给的有问题,先在ps里面打开再保存一下,然后再用。不行就让美术重做吧。
    下载bmfront,打开图片管理器,最好先清除一下,然后选择添加图片,添加图片时,注意添加id,比如数字图片0的id是48,id不能重复,每个字符都有id,在bmfront界面选中想要添加的字符,在界面右下角第一个数就是id,之后导出就行了。网上教程很多,不懂百度一下。参考链接https://blog.csdn.net/luyuncsd123/article/details/18351057,参考链https://www.jianshu.com/p/675288dd4368
    还有一种用unity制作:

    using UnityEngine;
    using UnityEditor;
    using System.IO;
    public class CreateFont : EditorWindow
    {
        [MenuItem("Tools/创建字体(sprite)")]
        public static void Open()
        {
            GetWindow<CreateFont>("创建字体");
        }
        private Texture2D tex;
        private string fontName;
        private string fontPath;
        private void OnGUI()
        {
            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            GUILayout.Label("字体图片:");
            tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.Label("字体名称:");
            fontName = EditorGUILayout.TextField(fontName);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(string.IsNullOrEmpty(fontPath) ? "选择路径" : fontPath))
            {
                fontPath = EditorUtility.OpenFolderPanel("字体路径", Application.dataPath, "");
                if (string.IsNullOrEmpty(fontPath))
                {
                    Debug.Log("取消选择路径");
                }
                else
                {
                    fontPath = fontPath.Replace(Application.dataPath, "") + "/";
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("创建"))
            {
                Create();
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }
        private void Create()
        {
            if (tex == null)
            {
                Debug.LogWarning("创建失败,图片为空!");
                return;
            }
            if (string.IsNullOrEmpty(fontPath))
            {
                Debug.LogWarning("字体路径为空!");
                return;
            }
            if (fontName == null)
            {
                Debug.LogWarning("创建失败,字体名称为空!");
                return;
            }
            else
            {
                if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings"))
                {
                    Debug.LogError("创建失败,已存在同名字体文件");
                    return;
                }
                if (File.Exists(Application.dataPath + fontPath + fontName + ".mat"))
                {
                    Debug.LogError("创建失败,已存在同名字体材质文件");
                    return;
                }
            }
            string selectionPath = AssetDatabase.GetAssetPath(tex);
            if (selectionPath.Contains("/Resources/"))
            {
                string selectionExt = Path.GetExtension(selectionPath);
                if (selectionExt.Length == 0)
                {
                    Debug.LogError("创建失败!");
                    return;
                }
                string fontPathName = fontPath + fontName + ".fontsettings";
                string matPathName = fontPath + fontName + ".mat";
                float lineSpace = 0.1f;
                //string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length).Replace("Assets/Resources/", "");  
                string loadPath = selectionPath.Replace(selectionExt, "").Substring(selectionPath.IndexOf("/Resources/") + "/Resources/".Length);
                Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
                if (sprites.Length > 0)
                {
                    Material mat = new Material(Shader.Find("GUI/Text Shader"));
                    mat.SetTexture("_MainTex", tex);
                    Font m_myFont = new Font();
                    m_myFont.material = mat;
                    CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
                    for (int i = 0; i < sprites.Length; i++)
                    {
                        if (sprites[i].rect.height > lineSpace)
                        {
                            lineSpace = sprites[i].rect.height;
                        }
                    }
                    for (int i = 0; i < sprites.Length; i++)
                    {
                        Sprite spr = sprites[i];
                        CharacterInfo info = new CharacterInfo();
                        try
                        {
                            info.index = System.Convert.ToInt32(spr.name);
                        }
                        catch
                        {
                            Debug.LogError("创建失败,Sprite名称错误!");
                            return;
                        }
                        Rect rect = spr.rect;
                        float pivot = spr.pivot.y / rect.height - 0.5f;
                        if (pivot > 0)
                        {
                            pivot = -lineSpace / 2 - spr.pivot.y;
                        }
                        else if (pivot < 0)
                        {
                            pivot = -lineSpace / 2 + rect.height - spr.pivot.y;
                        }
                        else
                        {
                            pivot = -lineSpace / 2;
                        }
                        int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
                        info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
                        info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
                        info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
                        info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);
                        info.minX = 0;
                        info.minY = -(int)rect.height - offsetY;
                        info.maxX = (int)rect.width;
                        info.maxY = -offsetY;
                        info.advance = (int)rect.width;
                        characterInfo[i] = info;
                    }
                    AssetDatabase.CreateAsset(mat, "Assets" + matPathName);
                    AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName);
                    m_myFont.characterInfo = characterInfo;
                    EditorUtility.SetDirty(m_myFont);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();//刷新资源  
                    Debug.Log("创建字体成功");
                }
                else
                {
                    Debug.LogError("图集错误!");
                }
            }
            else
            {
                Debug.LogError("创建失败,选择的图片不在Resources文件夹内!");
            }
        }
    }
    
    

    参考网址:https://gameinstitute.qq.com/community/detail/115061
    https://gameinstitute.qq.com/community/detail/124219

    相关文章

      网友评论

          本文标题:Unity制作艺术字

          本文链接:https://www.haomeiwen.com/subject/mwbeqqtx.html