Search Unity

Font file >> external import and Assignement in runtime.

Discussion in 'Editor & General Support' started by sama-van, Apr 27, 2017.

  1. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Hello,

    Made a tool for my team that need to deal with font files.
    The font files can't be drop into the Resources folder since it is for a stand alone build tool and they are free to load the font they want.

    Searched on the web and didn't found anything, most of the topic empty.
    Even using the www method then casting Font doesn't make it work...

    Any idea if this can be achieved? any code sample?

    As always thanks by advance!

    Cheers :)
     
  2. MoonHeadJohn

    MoonHeadJohn

    Joined:
    Oct 8, 2016
    Posts:
    2
    Did you manage to find a solution for this?
     
  3. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Nothing!
    Still no solution! :)
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,436
    loading fonts kind of works (using system.drawing), but how to convert System.Drawing.Font into UnityEngineFont or get the font data there..?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using System.Drawing.Text;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using Font = UnityEngine.Font;
    8.  
    9. public class LoadFont : MonoBehaviour
    10. {
    11.     public Text text; // testing with ui text
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         // 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
    17.         var privateFontCollection = new PrivateFontCollection();
    18.         // Provide the path to the font on the filesystem
    19.         privateFontCollection.AddFontFile(@"C:\Windows\Fonts\comic.ttf");
    20.  
    21.         var myCustomFont = new System.Drawing.Font((FontFamily)privateFontCollection.Families[0], 36f);
    22.  
    23.         Debug.Log(myCustomFont);
    24.  
    25.         // Get the array of FontFamily objects.
    26.         var fontFamilies = privateFontCollection.Families;
    27.  
    28.         // How many objects in the fontFamilies array?
    29.         var count = fontFamilies.Length;
    30.  
    31.         // Display the name of each font family in the private collection
    32.         // along with the available styles for that font family.
    33.         for (int j = 0; j < count; ++j)
    34.         {
    35.             // Get the font family name.
    36.             var familyName = fontFamilies[j].Name;
    37.  
    38.             // Is the regular style available?
    39.             if (fontFamilies[j].IsStyleAvailable(System.Drawing.FontStyle.Regular))
    40.             {
    41.                 string familyNameAndStyle = "";
    42.                 familyNameAndStyle = familyNameAndStyle + familyName;
    43.                 familyNameAndStyle = familyNameAndStyle + " Regular";
    44.  
    45.                 System.Drawing.Font regFont = new System.Drawing.Font(
    46.                    familyName,
    47.                    16,
    48.                    System.Drawing.FontStyle.Regular,
    49.                    GraphicsUnit.Pixel);
    50.  
    51.                 //e.Graphics.DrawString(familyNameAndStyle, regFont, solidBrush, 16);
    52.  
    53.                 var f = new UnityEngine.Font(familyName);
    54.                 // Font f = (Font)regFont;
    55.  
    56.                 // get font data ?
    57. //                f.characterInfo
    58.  
    59.                 text.font = f;
    60.             }
    61.         }
    62.  
    63.     }
    64.  
    65. }
    66.  
     
  5. MoonHeadJohn

    MoonHeadJohn

    Joined:
    Oct 8, 2016
    Posts:
    2
    In my situation I need a solution that'll work for Android and iOS. As far as I know, System.Drawing isn't compatible.
     
  6. HinxVietti_WITL

    HinxVietti_WITL

    Joined:
    Mar 11, 2019
    Posts:
    10
    This can only read installed system font files, and if you just wanna to load system font, just use Font.GetOSInstalledFontNames(); to load All fonts installed . this can not load and local font file.
     
  7. Umresh

    Umresh

    Joined:
    Oct 14, 2013
    Posts:
    56
    Hi,
    Tried your script but it throws me error saying
    ERROR : The type or namespace name 'Text' does not exist in the namespace 'System.Drawing' .
    I'm using unity 2019.2.6 with .net 4.x for Android