I am trying to load a font into my game for iPhone. I am using C# and MonoTouch and the excellent MonoGame project (I have got the latest build from the develop3d branch on github).
My fonts load correctly on Windows but when I try to run the game on iPhone I get an error saying could not load font. I have loaded the sprite font into a MonoGame content project and compiled for iOS.
I have imported the produced xnb file in as content, set it to content mode in properties and marked it as copy if newer. However it still won't load. After a lot of digging I found that iOS does not support DXT compressed fonts which is apparently what a SpriteFont is.
I have no idea as to how to go about loading the font into my game. Can someone please help with some code as to how to do it?
I have just figured it out. For those who are struggling with the same thing follow these steps:
Create a new solution choose the mono game content for all platforms (which is basically a dummy game for building content)
Add a new mono game content builder project
Set the build type to the type you want e.g. iOS
Add the content you want
In the property window for each content file change the content processor, to the mono game version of the content you are building. e.g. for a texture choose "MonoGame - texture"
Build the solution which will build the content into a folder for the platform you have built for in the release directory
Add the content into your game project from the built folder of the content project. Make sure you set the build action to content and the copy to output directory to copy if newer (or copy always doesnt really matter)
Use the content using the normal content loader in xna
Related
I am trying to load an AssetBundle from a file, however I get the following error:
The AssetBundle 'path\to\file' could not be loaded because it is not compatible with this newer version of the Unity runtime. Rebuild the AssetBundle to fix this error.
I build my AssetBundle as shown on the Unity wiki:
using UnityEditor;
namespace Editor
{
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
private static void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundles",
BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
}
This generates a correct looking AssetBundle, the manifest file also looks fine.
I load the AssetBundle with the following code:
var assetBundle = AssetBundle.LoadFromFile(path);
Both the AssetBundle and the game are built with the same version of Unity, version 2017.3.1f1 (64 bit). I've also tried building both with the latest available beta build, but this did not resolve the issue.
Changing the BuildTarget to BuildTarget.StandaloneWindows64 also does not resolve the issue.
The Unity docs are outdated a bit on AssetBundles, since Unity 2017 they introduced an entire new assetbundle system, which is easier to use and works with an improved UI called the AssetBundle Browser
I've had issues myself when switching from Unity 5.x to 2017.x using assetbundles, and it actually required me to use the new assetbundle system, and build/load through that to get them to work again.
Get the Assetbundle browser:
Download the AssetBundle Browser from Unity's GitHub
Add the downloaded files to your Unity project
Go to Window
AssetBundle browser
Building an assetbundle:
here you will see two tabs, "configure" and "Build". Select the assetbundle you want to build by dragging a prefab of the object into the configure tab. you'll get a question asking if you want to build it as one big bundle or multiple seperate bundles, select whichever you prefer.
The Browser will also give a warning if multiple bundles share the same assets, and propose to make a single seperate bundle containing all shared resource, depending on how many and how big your bundles are this can save quite alot of space.
Then if you go to the "Build tab" you can select for which platform you want to build and the output path, along with some additional options such as compression type. Then all you have to do is click "Build" to build your new assetbundle compatible with unity 2017.x
Loading an Assetbundle:
Loading an assetbundle from a file is as simple as using the following piece of code: AssetBundle myAssetBundle = AssetBundle.LoadFromFile(path);
You can also load assetbundles from Memory (taking in bytes) or load directly from a stream.
An additional bonus to the new AssetBundle browser is that you can customize it however you need All files can be found in /Assets/Editor/AssetBundleBrowser/. for example I included the functionality to automatically upload all bundles to an FTP after its done building.
Edit: The Unity AssetBundle browser tool works for version 5.6 or higher.
I replaced all the images everywhere (by this I mean in drawable folders and all Windows Assets folders and iOS Resources folder), but it still shows me the default Xamarin icon for the app. I tried this code, too, but it doesn't seem to work either. Can someone tell me the solution?
[assembly: Application(Icon = "#drawable/icon")]
Updating Icon and Name (Android)
If you changed the icon file name please ensure you update the Icon reference in MainActivity.cs:
[Activity(Label = "MyName", Icon = "#mipmap/myicon", Theme = "#style/MainTheme"]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
}
You should also update the names of icon.xml and icon_round.xml in the mipmap-anydpi folder to match to new icon name. If launcher-foreground.png was renamed then update the value of <foreground...> in corresponding icon.xml.
Summary (Android)
Replace the png's in mipmap folders with your new icon
If name was changed, update Icon value in MainActivity.cs
If name was changed, update name of (or create a new copy of) icon.xml and icon_round.xml
If name of launcher-foreground.png was changed then update value in icon.xml. Eg:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="#color/my_launcher_background"/>
<foreground android:drawable="#mipmap/my_launcher_foreground"/>
</adaptive-icon>
If name wasn't changed and you've cleaned and re-built project but still your new icons are not deploying: Delete obj folder from Android project directory
Details and Issues if not updating (Android)
Xamarin Forms/Android puts 2 icons in each mipmap folder (mipmap-mdpi, mipmap-hdpi, mipmap-xhdpi, etc) - icon.png and launcher-foreground.png.
Replace both these images in each of the mipmap folders. I kept the same names but it should be possible to use a different name.
If you rename launcher-foreground.png then you must update the values in icon.xml and icon_round.xml (or their equivalent new names). The value in the <foreground...> tag references the icon/png. So, if launcher-foreground.png was updated to my_launcher-foreground.png this should be reflected in the icon.xml (see step 4 in summary above).
If the icons still don't update even after cleaning/re-building it could be that the original/default icons still existed in the Android project obj folder. The solution is to delete the obj folder from the Android project directory.
iOS
For iOS, please see Managing Icons with Asset Catalogs. A summary taken from the linked page is provided below:
For icons, a special AppIcon image set can be added to the Assets.xcassets file in the app's project.
To use an Asset Catalog, do the following:
Double-click the Info.plist file in the Solution Explorer.
Click on the Visual Assets tab and click on the Use Asset Catalog button under App Icons.
From the Solution Explorer, expand the Asset Catalog folder.
Double-click the Media file to open it in the editor.
Under the Properties Explorer the developer can select the different types and sizes of icons required.
Click on given icon type and select an image file for the required type/size.
Click the Open button to include the image in the project and set it in the xcasset.
MacOs
The guide for MacOs is Application icon for Xamarin.Mac apps. You need to consider the following steps:
Required image sizes and filenames
Packaging the icon resources
Using the icon
detailed in the linked docs above.
For Android try to set the icon app like this:
[Activity(Icon = "#drawable/icon")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
.....
}
Make sure that you have changed all the icon images on all drawable folders ( drawable, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi ).
For iOS I like set the app icons and splash screen with Asset Catalogs, here you can find a guide to how to use it:
https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_images/app-icons/
For iOS in visual studio...
Step1: Generate all the app icons from https://appicon.co/
Step2: In solution explorer, under iOS project, expand 'Asset Catalogs' and double click on 'Assests'. You can change the existing 'AppIcon' icons or Add a new Asset and attach the icons.
This is how you add a new asset...
Step3: In solution explorer, under iOS project, double click on Info.plist.
Go to 'Visual Assets' tab, click on 'App Icons' and change the 'Source' to the
asset that you just created.
Done.
Hope this helps someone!
Answer is deprecated
If I understood you correctly, for iOS project: Properties -> iOS Application
For Android: Properties -> Android Manifest
I had the same problem, but the following helped resolve it:
Go to MainActivity.cs (in the Solution Explorer)
Look for where it says 'Icon ='
Change the location of the Icon, but don't include the extension, i.e. jpg, png, etc. (For ex. Icon = "#drawable/weatherIcon")
Save the file
Restart debugging (the little restart icon beside the stop and pause icon around the top right-hand side of your screen)
If you get stuck on any part of this check out this short video from 02:47 onwards https://youtu.be/5g8lWPQZFxs?t=167. It helped me a lot.
Try to replace all the icon on iOS project > Properties > iOS Application > Iphone Icons/IPad Icons.
I change all of them, the settings icon do change. Images that on Resouces can be created by just insert images on there.
Tested this today and noticed that you only need to change the icon images in all drawable folders,
drawable,
drawable-hdpi,
drawable-xhdpi
drawable-xxhdpi.
Depending on the device it's going to use different images. For me i used "Visual Studio Android Emulator" using:
- 5" KitKat(4,4) XXHDPI Phone (Android 4,4 - API 19)
Did not need to uninstall and reinstall the app in the emulator, it updated on it's own after starting the project with "build" checked in the menu Build/Configuration manager.
On top of that no code changes were made!
If you replace the Icon.png and still nothing changed on the device, than change "Copy to Output Directory" propertiy to "Copy if newer" or "Copy always".
That was my solution.
All of these solutions are great, but
THE SIMPLEST WAY:
You can now just use ResizetizerNT's nuget package and upload one svg file and set IsAppIcon to true. That should generate everything needed.
If the existing app icon is icon.png, just name the svg to icon.svg and it should automatically work.
Alternatively, you can also add another SVG for the background in order to support adaptive icons on Android.
Context: ResizetizerNT was created by the Xamarin.Forms/MAUI Engineering Manager, Jonathan Dick
when i start the sampleNFT -an already made example from ARToolkit- in unity Editor -pressing play button- it worked greatly
BUT when i build my android app it removes my NFT marker which is in this case "gilbratar"
and i made the player setting and Android manifest as good as it supposed to be !
BTW i'm using Unity 5.3.0f4 and ARToolkit 5.3.1
why i have this problem ?!
check this image for more info
You're posting Unity editor logs, but saying you're building to Android. Do you mean you're trying to hit the "Play" button while your build target is set to Android?
If that is the case, I would recommend continuing on the editor set to your desktop platform for debugging purposes, and only switching over to your build target for building.
Also, make sure that the AndroidManifest.xml in your Assets/Plugins/Android folder has it's package name changed from "com.Artoolkit.demo" to whatever you chose as your Bundle Identifier under build settings for Android.
My code is
new GameFont(Content.Load<SpriteFont>("LoadingFont"), "LoadingFont")
According to what I've read, you have to use VS2010 to compile your assets into .xnb format, which I have done, and place them into the Content subfolder in your bin directory, which I have also done. However, I get an error saying "Could not load LoadingFont asset!".
I'm not really sure what else to do. I read a very old post saying that assets made using XNA 4 won't work, but I don't know if that's still true, or how to change my version of XNA to 3.1.
Any ideas? Perhaps there's a better way without using VS2010 at all?
Have you added the content to the Content folder of your project and set the Build Action to Content (in the properties window).
I'm not exactly sure what the process is to use assets in the .xnb format. I know it can be done that way but I normally just add the raw image and sounds files to my Content folder directly. Font's might be a little trickier to do that way though.
EDIT: 2015
A lot of things have changed since this question was written. These days MonoGame has it's own Pipeline tool for processing content into XNB files. The first thing to try would be to process your SpriteFont using the new Pipeline. This way you can avoid the need to depend on XNA altogether.
Alternately, another way to get around font issues in MonoGame is to pre-render them to a texture using the BMFont tool and use MonoGame.Extended to render them in your game.
Once you've installed MonoGame.Extended you can load fonts created with BMFont just as you would a SpriteFont but using the BitmapFont class instead.
_bitmapFont = Content.Load<BitmapFont>("my-font");
Then render some text like so:
_spriteBatch.Begin();
_spriteBatch.DrawString(_bitmapFont, "Hello World", new Vector2(100, 200), Color.Red);
_spriteBatch.End();
I have a full tutorial on my blog
I've been using MonoGame in VS2012 with no trouble; Content.Load<T>("myContent") still works for me. One thing that has been catching me out lately (tip: don't code when sleep-deprived) is that you have to check your assets are set to "Copy to output directory" in Solution Explorer, or else when the game builds, the assets won't go with it, which is why you get that error - it can't load what isn't there! Right-click on your assets and look under Properties. If they're set to "Do not copy" then you'll want to change that.
Like #craftworkgames I can't give you a full answer. But the traditional Content Pipeline that came with XNA 3.1 has gone in 4. So any assets need loading a slightly different way. For example, to load a .png asset to display a sprite I do the following:
rock = Texture2D.FromStream(GraphicsDevice, TitleContainer.OpenStream(#"Rock-Large-Stones-PT.png"));
and add the .png file to the solution as normal. I'm pretty sure it's going to be something similar for fonts but I haven't quite figured it out yet either ;)
I was having the same issue not sure if you got it resolved or not but here is what I did.
Click on the Monogame Content Pipeline under the "Content" folder.
Add in your spritefont or texture.
Make sure in the Monogame Content Pipeline that you change the action from "Build" to "Copy"
Then when you want to load the content simply;
texture = content.Load("texture.png");
Hope this helps :)
I had similar problem but with 'Windows 8 Store' as a target platform... while everything was fine with Windows Open GL version.
On Windows 8 I received "could not load as a non-content file" when I tried content.Load("Fonts/TestFont").
I replaced MonoGame DLLs in my solution with the MonoGame.Windows8 project (I downloaded the sources from CodePlex) and I did some debugging... and found out that MonoGame looks for the Content folder in the bin...\Debug\AppX folder... while during the build the Content folder is copied into different place. So when I manually copied the Content folder into AppX folder, the error has gone and my game prototype works fine now.
I guess I either missed some setting in the Project preferences related to AppX (Presumably this folder is needed for the Windows 8 device emulator?) or... MonoGame should look for the Content folder in different place... Anyway I will be renewing the Content folder manually for now because it is copied into wrong place during the compilation.
I have Content folder in my project and build action is set correctly (Content)... and 'Copy always' is chosen but during the build (compliation) that Content folder is copied to Debug folder while it should be copied to Debug\AppX folder otherwise MonoGame can't find it.
(I maybe wrong with exact paths because I am currently at work and the issue is at my home PC)
Maybe it is just some configuration issue in my Visual Studio 2012.
Hope this information helps.
"
If you want to play along, you can create a Visual Studio project named NaiveTextMovement
and add the 14-point Segoe UI Mono font to the Content directory. The fields in the Game1
class are defined like so: "
how to add 14-point Segoe UI Mono font to the Content directory?
iv tried creating a folder in the solution and placing the font in it then i try to load it using Content.Load ... but it doesnt work. how is that?
You need to create a content project, then create a font description file. Reference the content project from your main XNA project.
In fact, a content project should already have been generated for you.
The font description file is an XML file. Editing it will be self-explanatory once you open it, as it will be auto-generated and well-commented.
When you create a new Windows Phone 7 game project, the template creates two different projects in your solution. One is the WindowsPhoneGame project and the other is the WindowsPhoneGameContent project.
The Game project is where you'll be adding all your code and the Content project is where you'll be adding all your game assets like sprites, textures and spritefonts.
You can see in the image below the two projects shown in the Solution Explorer (that's the pane in the upper right if you have the default configuration for Visual Studio).
The "Content" project is the one highlighted in the above image.
To add a SpriteFont, simple right click on the Content project and select "Add" and then "New Item" from the Add menu. This will open up the below dialog.
From the "Add New Item" dialog, simply select SpriteFont and give it the name you want. That will add a new SpriteFont object to your Content project and to your game.