0
点赞
收藏
分享

微信扫一扫

Unity 图文混排 生成SpriteAsset

舍予兄 2021-10-15 阅读 120

生成Sprite Asset

  1. TexturePacker 生成json、png

  1. TexturePacker 生成 tpsheet、png

  1. 生成文件对比 json与 tpsheet,最终tpsheet中位置和大小数据为最终准确数据。

  1. 打开Sprite Importer

5.导出之后会遇到切割位置信息不对,这时候需要用tpsheet中的数据设置一次。

  1. 使用tpsheet 文件重新赋值(位置,大小等属性)


Open EditorSpriteAsset Window 代码(可以直接copy使用但是需要注意tpsheet路径)
using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.TextCore;

namespace CS
{
    public class EditorSpritesAsset : EditorWindow
    {

        [MenuItem("Tools/EditorSpritesAsset")]
        public static void ShowSceneWindows()
        {
            EditorWindow win = GetWindow(typeof(EditorSpritesAsset), false,"编辑图集",true);
            win.Show();
        }

        private TMP_SpriteAsset mSprAss = null;
        private void OnGUI()
        {
            mSprAss = EditorGUILayout.ObjectField(mSprAss, typeof(TMP_SpriteAsset
            )) as TMP_SpriteAsset;

            if (GUILayout.Button("确定" , GUILayout.Width(50), GUILayout.Height(20)))
            {
                RefreshSpritesAsset();
            }
        }
    
        private void EditorSpriteAsset()
        {
            //此处为tpsheet文件所在的路径,读取文件中的数据
            string str = File.ReadAllText(Application.dataPath + "/TextMesh Pro/Sprites/"+mSprAss.name+".tpsheet");
            string[] strArr = str.Split('\n');
            int i = 0;
            GlyphRect glyphRectTmp = new GlyphRect(0, 0, 0, 0);
            GlyphMetrics glyphMetrics = new GlyphMetrics();
            foreach (var item in strArr)
            {    
                if (item.StartsWith("#") || item.StartsWith(":") || string.IsNullOrEmpty(item)|| item.StartsWith("\r"))
                {
                    continue;
                }
                string[] strArr2 = item.Split(';');
                glyphRectTmp.x = int.Parse(strArr2[1]);
                glyphRectTmp.y = int.Parse(strArr2[2]);
                glyphRectTmp.width = int.Parse(strArr2[3]);
                glyphRectTmp.height = int.Parse(strArr2[4]);
                mSprAss.spriteGlyphTable[i].glyphRect = glyphRectTmp;
                
                glyphMetrics = mSprAss.spriteGlyphTable[i].metrics;
                glyphMetrics.horizontalBearingX = 0;
                glyphMetrics.horizontalBearingY = int.Parse(strArr2[4])-5;
                mSprAss.spriteGlyphTable[i].metrics = glyphMetrics;
                i++;
            }
        }
    }
}

  1. 完毕

8.最后找到TMP Settings 配置文件,将新生成的Sprite Asset 设置为默认。

9.直接设置图片名字就可以生效。
"You must earn 1 <sprite name="currency_diamond_big"/> character if you recruit 10 times"

举报

相关推荐

0 条评论