❔ Loading a Unity Font object from an assembly's Embedded Resource

Well, I'm trying to load a font I have as a resource embedded in my C# assembly. I've tried a number of things, my most recent attempt seen here (ignore the bad variable names, I'll clean it up once I get it working):
Shell.print("=====FONTS=====");
string resourcePath = Assembly.GetExecutingAssembly().GetManifestResourceNames()[0];
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
var tempFiles = new TempFileCollection();
string file = tempFiles.AddExtension("otf");
using (FileStream fs = File.OpenWrite(file))
{
fontStream.CopyTo(fs);
}
unifont = new Font(file);
Shell.print(unifont.characterInfo.Length);
Shell.print("===END FONTS===");
Shell.print("=====FONTS=====");
string resourcePath = Assembly.GetExecutingAssembly().GetManifestResourceNames()[0];
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
var tempFiles = new TempFileCollection();
string file = tempFiles.AddExtension("otf");
using (FileStream fs = File.OpenWrite(file))
{
fontStream.CopyTo(fs);
}
unifont = new Font(file);
Shell.print(unifont.characterInfo.Length);
Shell.print("===END FONTS===");
The idea being that once I successfully load the font, it will print something other than 0. I have also tried passing the embedded "path" TextAdventure.Assets.unifont-15.0.06.otf straight to the Font constructor (both relative path & appended to the absolute path of the assembly). I have also put the .otf file in a Unity Asset Bundle and used AssetBundle.LoadFromStream(fontStream).LoadAsset<Font>("assets/unifont-15.0.06.otf") to try and grab the file. Any ideas?
16 Replies
JakenVeina
JakenVeina13mo ago
well if we want to approach this directly new Font(file) file here is a string which presumably should be a file path for the file containing the font
乙ᗰᗩᑎ350᙭
correct, it's a path
JakenVeina
JakenVeina13mo ago
what it ACTUALLY is is tempFiles.AddExtension("otf") so somehow you've taken a COLLECTION of files and tried to attach a file extension to it what does that even mean? also, the collection appears to be empty
乙ᗰᗩᑎ350᙭
Here, lemme print the value of file
JakenVeina
JakenVeina13mo ago
alright can you verify the file itself?
乙ᗰᗩᑎ350᙭
will do
JakenVeina
JakenVeina13mo ago
otherwise, seems solid to me
乙ᗰᗩᑎ350᙭
So we do have a file lemme check the file contents well... ZACKPN-1 isn't a User... And even then, there's no otf files in my Temp directory Why would it make up a path that doesn't exist lol? Ok @ReactiveVeina made some changes... Can confirm it creates an actual working .otf file at the path of variable file... But the Font object created from it doesn't seem to work...
JakenVeina
JakenVeina13mo ago
it's a WORKING OTF file, specifically? what you expect it to be? does it work if you just hard-code the path to the source OTF file? not the extracted one?
乙ᗰᗩᑎ350᙭
working otf
乙ᗰᗩᑎ350᙭
nope, fails with hardcoded path this seems to be an issue with the Unity Font stuff 😦
unifont = new Font("C:\\Program Files(x86)\\Steam\\steamapps\\common\\Human Fall Flat\\BepInEx\\plugins\\unifont.otf");
unifont = new Font("C:\\Program Files(x86)\\Steam\\steamapps\\common\\Human Fall Flat\\BepInEx\\plugins\\unifont.otf");
JakenVeina
JakenVeina13mo ago
yeah or at least your understanding of how to use it any chance it's a file permissions issue? that's about my only other thought
乙ᗰᗩᑎ350᙭
I mean, I don't think so? I'm already creating/accessing the file from the same assembly, not to mention that I'm storing it in the game's file folder that it should have full access to
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts