SpriteFont has no name property?!

Monogame and Community

When loading my assets into my code I put them all into Generic List and call them by name with the Find method, but I see the SpriteFont doesn’t have a name Property like Texture2D does.

I don’t know what to do. I NEED to call my assets by name.


I’m new to C# and MonoGame

When you load the fonts you used a name. Use that name when adding them to the list.

I was thinking dictionary. Use a dictionary and add the fonts to that using the name you loaded the font with.

You could just wrap the spritefont sort of nice that way too.

    // can be a struct or a class...
    public struct MyFont
        string name;
        SpriteFont instance;

        public string Name { get { return name; } }
        public SpriteFont Instance{ get { return instance; } }
        public static implicit operator String(MyFont _this){return _this.Name;} 
        public static implicit operator SpriteFont(MyFont _this){return _this.instance;}

        public MyFont(Microsoft.Xna.Framework.Content.ContentManager content, string name)
            this.name = name;
            this.instance = content.Load<SpriteFont>(name);


Game1 ..ect....

MyFont myfont;

// for testing
string s;
SpriteFont sf;


        protected override void LoadContent()

             myfont = new MyFont(Content, "MgGenFont");

             s = myfont;
             sf = myfont;

             // so you can just pass myfont to a spritebatch drawstring call
             // spriteBatch.DrawString(myfont, "test", Vector2.Zero, Color.White);

             s = myfont.Name;
             sf = myfont.Instance;

          .... ect ...

Now it has a name property.

No need to do what you are doing. ContentManager is doing the same thing internally.
If you call content.Load(“font1”) it will return the same instance of the cached SpriteFont.