Ejemplo n.º 1
0
    private static Font CreateFont(AntFont aFontData, Material aMaterial, CharacterInfo[] aCharacters)
    {
        Font font = new Font();

        font.material      = aMaterial;
        font.name          = aFontData["info"]["face"].Value;
        font.characterInfo = aCharacters;

        // Изменение значений доступных только для чтения... ;)
        SerializedObject objFont = new SerializedObject(font);

        objFont.FindProperty("m_FontSize").floatValue    = 0.0f;
        objFont.FindProperty("m_LineSpacing").floatValue = aFontData["common"]["lineHeight"].AsFloat;

        List <AntFontItem> kerningList = aFontData.GetAllBy("kerning");

        if (kerningList.Count > 0)
        {
            SerializedProperty kerningProp = objFont.FindProperty("m_KerningValues");
            for (int i = 0; i < kerningList.Count; i++)
            {
                kerningProp.InsertArrayElementAtIndex(i);
                SerializedProperty kern = kerningProp.GetArrayElementAtIndex(i);
                kern.FindPropertyRelative("second").floatValue = kerningList[i]["amount"].AsFloat;
            }
        }

        objFont.ApplyModifiedProperties();
        return(font);
    }
Ejemplo n.º 2
0
    private static CharacterInfo[] CreateCharacters(AntFont aFontData, Vector2 aTextureSize)
    {
        var           charsList = aFontData.GetAllBy("char");
        Rect          rect      = new Rect();
        AntFontItem   curChar;
        CharacterInfo info;

        CharacterInfo[] result = new CharacterInfo[charsList.Count];
        for (int i = 0; i < charsList.Count; i++)
        {
            curChar = charsList[i];

            // Создание информации о символе.
            info         = new CharacterInfo();
            info.index   = curChar["id"].AsInt;
            info.advance = curChar["xadvance"].AsInt;

            // Чтение данных и рассчет UV координат для символа.
            rect.x      = curChar["x"].AsFloat / aTextureSize.x;
            rect.y      = curChar["y"].AsFloat / aTextureSize.y;
            rect.width  = curChar["width"].AsFloat / aTextureSize.x;
            rect.height = curChar["height"].AsFloat / aTextureSize.y;
            rect.y      = 1f - rect.y - rect.height;

            // Применение расчитанных UV координат к символу.
            info.uvBottomLeft  = new Vector2(rect.xMin, rect.yMin);
            info.uvBottomRight = new Vector2(rect.xMax, rect.yMin);
            info.uvTopLeft     = new Vector2(rect.xMin, rect.yMax);
            info.uvTopRight    = new Vector2(rect.xMax, rect.yMax);

            // Чтение данных и рассчет смещений для символа.
            rect.x      = curChar["xoffset"].AsFloat;
            rect.y      = curChar["yoffset"].AsFloat;
            rect.width  = curChar["width"].AsFloat;
            rect.height = curChar["height"].AsFloat;
            rect.y      = -rect.y;
            rect.height = -rect.height;

            // Применение расчитанных смещений к символу.
            info.minX = Mathf.RoundToInt(rect.xMin);
            info.maxX = Mathf.RoundToInt(rect.xMax);
            info.minY = Mathf.RoundToInt(rect.yMax);
            info.maxY = Mathf.RoundToInt(rect.yMin);

            result[i] = info;
        }

        return(result);
    }
Ejemplo n.º 3
0
    public static void ImportBitmapFont()
    {
        TextAsset selected = Selection.activeObject as TextAsset;

        if (selected != null)
        {
            string rootDir  = Path.GetDirectoryName(AssetDatabase.GetAssetPath(selected));
            string fileName = Path.GetFileNameWithoutExtension(selected.name);

            // Чтение данных из текстового файла шрифта.
            Debug.Log("AntFontImporter: Reading FNT data...");
            AntFont fontData = new AntFont(selected.text);

            // Создание текстуры.
            string texFileName = rootDir + "/" + fontData["page"]["file"].Value;
            Debug.Log("AntFontImporter: Creating Texture2D from \"" + texFileName + "\" image...");
            Texture2D texture = CreateTexture(texFileName);

            // Создание символов.
            Debug.Log("AntFontImporter: Initializing font chars...");
            CharacterInfo[] chars = CreateCharacters(fontData, new Vector2(texture.width, texture.height));

            // Создание материала.
            string matFileName = rootDir + "/" + fileName + ".mat";
            Debug.Log("AntFontImporter: Creating Material and saving it to \"" + matFileName + "\"...");
            Material material = CreateMaterial(texture);
            AssetDatabase.CreateAsset(material, matFileName);

            // Создание шрифта.
            string fontFileName = rootDir + "/" + fileName + ".fontsettings";
            Debug.Log("AntFontImporter: Creating Font Settings and saving it to \"" + fontFileName + "\"...");
            Font font = CreateFont(fontData, material, chars);
            AssetDatabase.CreateAsset(font, fontFileName);

            Debug.Log("AntFontImporter: BitmapFont is done!");
        }
    }