How to only load current used font
We're using different fonts depending on the language, but we want to only load the current font needed in order to save memory (some platforms have very limited RAM).
This is what we have in our Root View:
This is All.Font.xaml:
This is Default.Font.xaml:
This is Japanese.Font.xaml:
This is how we handle the resource dictionaries in the RootView code:
With this setup we've seen that all the fonts are currently being loaded into memory. Looks like that by just using noesis:Dependency the font will be loaded. Is there a way to define the fonts the project needs, but only load the specific font needed for each case?
This is what we have in our Root View:
Code: Select all
[...]
<noesis:Xaml.Dependencies>
<noesis:Dependency Source="/Assets/UI/Fonts/All.Font.xaml"/>
</noesis:Xaml.Dependencies>
[...]
Code: Select all
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
<FrameworkElement x:Key="Dependencies">
<noesis:Xaml.Dependencies>
<noesis:Dependency Source="Default/Default.Font.xaml"/>
<noesis:Dependency Source="JP/Japanese.Font.xaml"/>
<noesis:Dependency Source="KR/Korean.Font.xaml"/>
<noesis:Dependency Source="SC/SimplifiedChinese.Font.xaml"/>
<noesis:Dependency Source="TC/TraditionalChinese.Font.xaml"/>
</noesis:Xaml.Dependencies>
</FrameworkElement>
</ResourceDictionary>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<FontFamily x:Key="MainFont">
/Assets/UI/Fonts/#OurDefaultFont
</FontFamily>
</ResourceDictionary>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<FontFamily x:Key="MainFont">
/Assets/UI/Fonts/JP/#OurJapaneseFont
</FontFamily>
</ResourceDictionary>
Code: Select all
protected void UpdateFont(LocGeneralLanguage generalLanguage)
{
ResourceDictionary newFont = GetFont("Default/Default.Font.xaml");
switch (generalLanguage)
{
case LocGeneralLanguage.ja:
newFont = GetFont("JP/Japanese.Font.xaml");
break;
case LocGeneralLanguage.ko:
newFont = GetFont("KR/Korean.Font.xaml");
break;
case LocGeneralLanguage.zn_ch:
newFont = GetFont("SC/SimplifiedChinese.Font.xaml");
break;
case LocGeneralLanguage.zh_tw:
newFont = GetFont("TC/TraditionalChinese.Font.xaml");
break;
}
RefreshResources(newFont);
}
protected ResourceDictionary GetFont(string fontName)
{
if (ResourceDictionaries.TryGetValue(fontName, out ResourceDictionary resourceDictionaryFont))
{
return resourceDictionaryFont;
}
Uri uri = new Uri("Assets/UI/Fonts/" + fontName, UriKind.Relative);
resourceDictionaryFont = new ResourceDictionary { Source = uri };
ResourceDictionaries.Add(fontName, resourceDictionaryFont);
return resourceDictionaryFont;
}
private void RefreshResources(ResourceDictionary newFont)
{
Resources.MergedDictionaries.Clear();
Resources.MergedDictionaries.Add(newFont);
PersistentContext.GetPreSessionState().CurrentFont = newFont;
}
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: How to only load current used font
Hi, using noesis:Dependency should not load the referenced xaml, only register it in the Xaml provider (along with its dependencies) so it can be later found and loaded.
Perhaps you mean that the font asset is "loaded" by Unity (because it is referenced by another asset: the xaml)?
Perhaps you mean that the font asset is "loaded" by Unity (because it is referenced by another asset: the xaml)?
Re: How to only load current used font
Hi!
Yeah, that's what I mean.Perhaps you mean that the font asset is "loaded" by Unity (because it is referenced by another asset: the xaml)?
Re: How to only load current used font
If the font is a dependency, it will be included in the final assets, but it will only be loaded if you use it. Unfortunately, I have noticed that when Unity launches, we load and unload all fonts to scan their information. This process could be optimized to avoid unnecessary loading. Could you please create a ticket to address this issue?
What behavior are you observing?
What behavior are you observing?
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests