In the game I am developing, I have created a serializable class that generates a .map file for saving and loading levels. In XNA, this works great using the following code:
FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); MapInfo data = (MapInfo)formatter.Deserialize(fileStream);
Unfortunately, in android it appears that the filePath ends up at the device's root (argument being passed in as "Content/maps"). After some research, I came across this page from xamarin that explains how to properly load assets from the asset folder. However, this action takes place in a separate namespace from my main activity, and so far have not found a way to access the Assets.Open() function.
any other ways to load that asset?
UPDATE: After playing around some more, I ended up trying to pass the Assets property of the main activity down through function calls, loading the file this way:
Stream fileStream = androidGame.Assets.Open(filePath); BinaryFormatter formatter = new BinaryFormatter(); MapInfo data = (MapInfo)formatter.Deserialize(fileStream);
SADLY, the file still does not load. Looking at the variables in debug shows the correct address for the activity contained in the variable androidGame, but says Open is an "Unknown member"
UPDATE WITH WORKING SOLUTION:
In case anyone comes across file loading issues in Monogame Android, this is what I ended up doing:
First, make sure you copy/reference your files in the correct dir of your android build. This should appear somewhere under Assets/Content/yourfile.file or subfolder.
Then, check the properties of the file and select "AndroidAsset" for build options and "Copy if newer" for output.
Finally, open the stream using the Assets property of your main activity. This can be accessed using your Microsoft.Xna.Framework.Game class in this way:
Stream fileStream = Game.Activity.Assets.Open(path);
Turns out my issue was not the stream itself, but the deserialization which could not open a .dll in android (overlooked that one...). I ended up switching to XML serialization, since the binary serialization is build dependent