TrueTypeFontImporter.GenerateEditableFont does not appear to be functional

Hey all,

I am attempting to make a font editable via scripting. I am aware that I can click on the gear icon and choose to create an editable copy, but I would like to achieve the same via editor scripting.

Docs in question are at: http://docs.unity3d.com/Documentation/ScriptReference/TrueTypeFontImporter.GenerateEditableFont.html

My code:

using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class FontManager : ScriptableObject {

    TextureImporter textureImporter = new TextureImporter();

	[MenuItem ("Assets/Make Editable Font")]
    static void FontManager()
	{
        System.Object Fonts = new System.Object();
        Fonts = Resources.Load("Fonts/nokia", typeof(Font));
        Font font = (Font)Fonts;

        string path = AssetDatabase.GetAssetPath(font);
            Debug.Log("Generating copy of: " + path);
            TrueTypeFontImporter fontImporter = AssetImporter.GetAtPath(path) as TrueTypeFontImporter;
            fontImporter.GenerateEditableFont(Application.dataPath + "/Resources/Fonts/nokia_copy");
            }}

But when I attempt to run the code I get the error:

Could not get texture importer
UnityEditor.TrueTypeFontImporter:GenerateEditableFont(String)

Which is completely useless. For the record, my font is in “Assets/Resources/Fonts/nokia.ttf”. Has anyone managed to get this function to work?

Thanks!

The function does work. Given an ASCII or Unicode font, GenerateEditableFont will make you a copy of it, so you can edit the bitmap texture. I’m not sure why you are using Resources - that is really a runtime thing and not an editor thing. Code like this worked for me:

using UnityEngine;
using UnityEditor;

public class FontManager :  MonoBehaviour {

    [MenuItem ("Assets/Make Editable Font")]
    static void Fontie()
    {
        UnityEngine.Object f = AssetDatabase.LoadMainAssetAtPath("Assets/Sathu.ttf");
        string path = AssetDatabase.GetAssetPath(f);
        Debug.Log("Generating copy of: " + path);
        TrueTypeFontImporter fontImporter = AssetImporter.GetAtPath(path) as TrueTypeFontImporter;
        fontImporter.GenerateEditableFont("Assets/Sathu2.ttf");
    }
}

Note that the files that are created are written into the Assets folder, since they are copies of assets.