Jump to content

How do I use a custom font


Recommended Posts

I would like to setup a textarea to use a monospaced font but I have no idea how to do it. Are there built in fonts I can use and/or is there a way to load in a font?

Edit:

I've tried using Resource.Load to load a font but I cant get it to work, it never loads anything. I've looked around on the internet but the examples are all vague and whatever I try doesn't work. I tried coping a courier new font from my computer into the resources directory of KSP and then loading it with Resource.Load but no font is loaded. Could anyone tell me how I actually use Resource.Load to load a font properly?

Edit:

I've come to the understanding that resources that can be loaded with resource.load are linked into the game at compile time and therefore this will not work.

What options do I have to use a custom font. Can I create some kind of font asset that can be used or do I have to do something crazy like make an image with my font and do all the drawing myself?

Edited by Xty
Link to comment
Share on other sites

I've tried using Resources.Load to load a font but I cant get it to work, it never loads anything. I've looked around on the internet but the examples are all vague and whatever I try doesn't work. I tried coping a courier new font from my computer into the resources directory of KSP and then loading it with Resource.Load but no font is loaded. Could anyone tell me how I actually use Resource.Load to load a font properly?

Link to comment
Share on other sites

  • 5 months later...

Sorry for necro-threading but did you ever find an answer to this? I have exactly the same problem and it's really infuriating. It seems that KSP only allows you to make more resource files of a very specific limited few types - you can make meshes and textures for the sake of part files. Those it will scan through the GameData files looking for. But you can't just make any arbitrary Unity resources (like fonts) and have them get loaded, it seems.

And the frustration is that the only help you ever get from online forums about Unity all start from the presumption that you're in full control of the software from the ground up and and can design your Unity project in the Unity editor. For someone making a mod for KSP, that's just not true. You have to live with the design SQUAD made. THEY didn't add the font you want to THEIR resources, so you have to add it on the fly at runtime. And THAT just Does Not Work no matter what you try.

Why am I trying to do this? Because I want to make a script text editor widget for kOS and the only fonts that KSP has preloaded are all proportional width, which is no good for a programmer's editor.

Link to comment
Share on other sites

Why am I trying to do this? Because I want to make a script text editor widget for kOS and the only fonts that KSP has preloaded are all proportional width, which is no good for a programmer's editor.

All signs point to using a custom font sheet image and doing the rendering yourself unfortunately

Link to comment
Share on other sites

All signs point to using a custom font sheet image and doing the rendering yourself unfortunately

Right. Already have that. But it then precludes using a built-in text editing widget like Unity's GUI.TextArea to do all the text editing logic for you. Having to implement from the ground up things like mouse highlight & cut & paste, scrollbar, linewrapping, and so on by hand when that's already been solved thousands of times over is annoying.

Link to comment
Share on other sites

Have you tried dropping the TTF in the Assets folder? Apparently that's how it's done.

Edit:

Forgot you're trying to force KSP to load a new font.

Those instructions are operating under the assumption that I have full control over the entire application from the ground up. They don't quite work for when you're writing a mod to a Unity application someone else (SQUAD) is in control of setting up.

Link to comment
Share on other sites

  • 2 months later...

Having the same problem.

Managed to export a font from Unity as bitmap tileset, added shadow in Photoshop and got the new custom font working back in the editor with a GUIText object.

Now the problem is how to make KSP load the font. I tried Resource.Load but this can only load things from the Resource folder according to the manual. I tried even that:

var font = Resources.Load<Font>("Assets/arialbd_copy");,

where arialbd_copy.fontsettings and some other things reside in Kerbal Space Program/Resource/Assets. Still it just returns null :(

Please let me know if you find out how to do this. I'd like to have shadowed text for KerbalFlightData.

Link to comment
Share on other sites

The problem is that Resource.Load searches a path of locations to look for resources, and you can't change this path to include more directories than SQUAD decided to include. SQUAD decided to look for some types of resources (textures and meshes) by looking through everything in GameData, but not other types of resources (fonts).

So the problem is that your TTF font file isn't in the search pattern SQUAD set up, and you're not allowed to alter that search pattern.

The only solution that might work, or so I've heard, is to build your own assets file and put the font into it. But the problem with this is that to export an assets file from Unity requires the very expensive full-on developer version of Unity, not the freebie play-around version of Unity, so most of us modders can't do it. (And If I'm going to fork over that much money, I want an assurance that it would fix the problem before I do so. I don't want to fork over the money as an *expeirment* to find out if it would work, and then find out that it doesn't.

Link to comment
Share on other sites

It is supposed to use a bitmap font. But anyway, in what directories does KSP actually look for fonts, if any at all? I guess it loads them only from asset bundles since i tried putting a font in the resource dir.

From what i have seen i believe it is possible to initialize an instance of the Font class manually by code. It would involve setting the uv's for each character. This is time consuming tedious extra coding work, which is why i haven't tried it yet.

Link to comment
Share on other sites

It is supposed to use a bitmap font. But anyway, in what directories does KSP actually look for fonts, if any at all? I guess it loads them only from asset bundles since i tried putting a font in the resource dir.

From what i have seen i believe it is possible to initialize an instance of the Font class manually by code. It would involve setting the uv's for each character. This is time consuming tedious extra coding work, which is why i haven't tried it yet.

If you find a way to do it, please let me know because I want to do the same thing - I want to use a built-in Unity TextArea widget, but have it showing a monospace font, and all of the fonts KSP includes are proportional.

Link to comment
Share on other sites

From what i have seen i believe it is possible to initialize an instance of the Font class manually by code. It would involve setting the uv's for each character. This is time consuming tedious extra coding work, which is why i haven't tried it yet.

The problem I hit was getting the various character/spacing info into a Font instance I created. I couldn't even set line spacing. It can be changed (and retrieved) in the Unity editor but there doesn't seem to be any way to pass that info back into a Font... :(

Link to comment
Share on other sites

The only solution that might work, or so I've heard, is to build your own assets file and put the font into it. But the problem with this is that to export an assets file from Unity requires the very expensive full-on developer version of Unity, not the freebie play-around version of Unity, so most of us modders can't do it. (And If I'm going to fork over that much money, I want an assurance that it would fix the problem before I do so. I don't want to fork over the money as an *expeirment* to find out if it would work, and then find out that it doesn't.

It does work with Unity Pro and Asset files. I did some test that when I was making SmokeScreen. You can test Unity Pro for 30(?) days, but it's still expansive in the end :)

Link to comment
Share on other sites

It does work with Unity Pro and Asset files. I did some test that when I was making SmokeScreen. You can test Unity Pro for 30(?) days, but it's still expansive in the end :)

Hmm... I wonder if those 30 days would be enough to learn how to create an assets file, stick a font into it, export it, and then use it from then on, never having to run Unity Pro ever again.

Link to comment
Share on other sites

  • 2 weeks later...

Some things i tried ... but no luck


byte[] fubar = ((Texture2D)GUI.skin.font.material.mainTexture).EncodeToPNG();
KSP.IO.File.WriteAllBytes<DMFlightData>(fubar, "fubar.png");

produces an error saying that the texture cannot be accessed because it is in video mem.


Texture2D fontTex = GameDatabase.Instance.GetTexture("KerbalFlightData/Textures/plain_white", false);
fontTex.filterMode = FilterMode.Bilinear;
theFont = new Font("DMFont");
theFont.characterInfo = new CharacterInfo[1];
theFont.characterInfo[0].index = 65; // this is the decimal code for an 'A'
theFont.characterInfo[0].uv = new Rect(0.527f, 0.156f, 0.054f, 0.125f);
theFont.characterInfo[0].width = 8f;
theFont.characterInfo[0].vert = new Rect(0f, -2f, 10f, -15f); // no idea .. the unity editor shows values like these
theFont.characterInfo[0].size = 12;
theFont.characterInfo[0].style = FontStyle.Normal;
theFont.material = new Material(GUI.skin.font.material);
theFont.material.mainTexture = fontTex;

GUI.Label("A") with this font draws nothing :(

Link to comment
Share on other sites

  • 7 months later...
It does work with Unity Pro and Asset files. I did some test that when I was making SmokeScreen. You can test Unity Pro for 30(?) days, but it's still expansive in the end :)

The only solution that might work, or so I've heard, is to build your own assets file and put the font into it. But the problem with this is that to export an assets file from Unity requires the very expensive full-on developer version of Unity, not the freebie play-around version of Unity, so most of us modders can't do it. (And If I'm going to fork over that much money, I want an assurance that it would fix the problem before I do so. I don't want to fork over the money as an *expeirment* to find out if it would work, and then find out that it doesn't.

Can someone explain how to do this? And does this work with only fonts or with everything like shaders and other fancy stuff?

If this is the case I could get someone to compile my assets for me.

Link to comment
Share on other sites

You need to run in DX11 for them to work AFAIK.

Really? I just assumed they would work if you have a card with SM5.0 because I saw a guy injecting a dx11 phong shader in this thread http://forum.kerbalspaceprogram.com/threads/109269-Custom-Shader-Loader-2-0-release

If this is the case I guess my mod is doomed. KSP in dx11 is horrible.

Anyway, I get this at compile time when I try to use assetbundles 0tCe4YU.png

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...