Skip to content

External Assets in MML

Roman Shapiro edited this page Jan 8, 2021 · 8 revisions

Overview

It is possible to reference external assets (images, fonts, etc) in the MML project file. This page describes this feature in detail.

MyraPad

Following video demonstrates how external assets could be referenced when editing a project in the MyraPad: https://youtu.be/6rKZMoExN_Q

Note. It is recommended to save the file before referencing external assets. Then assets would be referenced relative to the MML file path. Otherwise assets would have absolute paths.

Loading MML with external assets in runtime

If MML project with external assets is loaded through Project.LoadFromXml, then the loading code would need to include AssetManager as parameter.

I.e.

FileAssetResolver assetResolver = new FileAssetResolver(Path.Combine(PathUtils.ExecutingAssemblyDirectory, "Assets"));
AssetManager assetManager = new AssetManager(GraphicsDevice, assetResolver);
string data = File.ReadAllText(filePath);
Project project = Project.LoadFromXml(data, assetManager);

See https://github.com/rds1983/AssetManagementBase for more information regarding what AssetManager is and how to create it.

Exporting MML with external assets to C#

If MML created in the above video will be exported to C#, then it would result in following:

/* Generated by MyraPad at 27.02.2020 21:08:27 */
using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.Brushes;

#if !STRIDE
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#else
using Xenko.Core.Mathematics;
#endif

namespace Myra.Samples.AssetManagement
{
	partial class MainForm: Panel
	{
		private void BuildUI()
		{
			var label1 = new Label();
			label1.Text = "My Game";
			label1.Font = MyraEnvironment.DefaultAssetManager.Load<SpriteFontBase>("fonts/arial64.fnt");
			label1.TextColor = Color.LightBlue;
			label1.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;

			_menuItemStartNewGame = new MenuItem();
			_menuItemStartNewGame.Text = "Start New Game";
			_menuItemStartNewGame.Id = "_menuItemStartNewGame";

			_menuItemOptions = new MenuItem();
			_menuItemOptions.Text = "Options";
			_menuItemOptions.Id = "_menuItemOptions";

			_menuItemQuit = new MenuItem();
			_menuItemQuit.Text = "Quit";
			_menuItemQuit.Id = "_menuItemQuit";

			_mainMenu = new VerticalMenu();
			_mainMenu.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
			_mainMenu.VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Center;
			_mainMenu.LabelFont = MyraEnvironment.DefaultAssetManager.Load<SpriteFontBase>("fonts/comicSans48.fnt");
			_mainMenu.LabelColor = Color.Indigo;
			_mainMenu.SelectionHoverBackground = new SolidBrush("#808000FF");
			_mainMenu.SelectionBackground = new SolidBrush("#FFA500FF");
			_mainMenu.LabelHorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
			_mainMenu.HoverIndexCanBeNull = false;
			_mainMenu.Background = new SolidBrush("#00000000");
			_mainMenu.Border = new SolidBrush("#00000000");
			_mainMenu.Id = "_mainMenu";
			_mainMenu.Items.Add(_menuItemStartNewGame);
			_mainMenu.Items.Add(_menuItemOptions);
			_mainMenu.Items.Add(_menuItemQuit);

			var image1 = new Image();
			image1.Renderable = MyraEnvironment.DefaultAssetManager.Load<TextureRegion>("images/LogoOnly_64px.png");
			image1.Left = 10;
			image1.Top = -10;
			image1.VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Bottom;

			var label2 = new Label();
			label2.Text = "Version 0.6";
			label2.Font = MyraEnvironment.DefaultAssetManager.Load<SpriteFontBase>("fonts/calibri32.fnt");
			label2.Left = -10;
			label2.Top = -10;
			label2.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Right;
			label2.VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Bottom;

			
			Background = new SolidBrush("#C78100FF");
			Widgets.Add(label1);
			Widgets.Add(_mainMenu);
			Widgets.Add(image1);
			Widgets.Add(label2);
		}

		
		public MenuItem _menuItemStartNewGame;
		public MenuItem _menuItemOptions;
		public MenuItem _menuItemQuit;
		public VerticalMenu _mainMenu;
	}
}

It uses static variable MyraEnvironment.DefaultAssetManager as AssetManager. That AssetManager could be configured like this:

MyraEnvironment.DefaultAssetManager.AssetResolver = new FileAssetResolver(Path.Combine(PathUtils.ExecutingAssemblyDirectory, "Assets"));

Full sample is here: https://github.com/rds1983/Myra/tree/master/samples/Myra.Samples.AssetManagement