XNA C# - Manually create Content folder and adding assets - c#

I've been trying to use XNA framework along with Visual Studio 2012. Since the xna framework is not yet compatible with Visual Studio 2012 I had to set it manualy.
Yet, I managed to succesfully import xna references and create default game methods allowing me to run my program.
The problem came when I tried to add texture to my project and load it. I manually created a "Content" folder in which I added my textures following msdn documentation at http://msdn.microsoft.com/en-US/library/bb313966(v=xnagamestudio.30).aspx
The problem is, since it's not XNA game project I wont have the properties window showing my texture file as a texture but just as a resource file.
And the following code:
Load<Texture2D>("GrassTexture");
Will systematically lead to an error file not found.
I've set my ContentManger as it follows:
ContentManager.RootDirectory = "Content";
Does anyone know how to properly set the content folder ?

I had this problem too, but there is a opportunity to enable the xna game project for vs 12.
Look here: https://stackoverflow.com/a/10881007/1780038

Related

Visual Studio Code intellisense for Unity not working

so I've been trying to get Visual Studio Code set up to work with Unity.
Here is the list of extensions I have for Unity:
C# version 1.21.17
Debugger for Unity version 2.7.5
Unity Tools version 1.2.3
Unity Code Snippets version 1.3.0
So I have the IntelliSense working for methods like Awake, Start, OnCollisionEnter2D etc., but when I try to get methods like GetComponent they dont show up, which can be quite bothering as I am trying to learn and still dont know thte methods so well as to know all of their parameters and the exact way they are named.
So as it turns out(For some reason), the problem was that even though it appeared that when double clicking a script in Unity opened up the entire project folder in VS Code, I could see all the files and that stuff for some reason that was not properly loaded. I tried reopening the project folder from VS Code, rather than from unity and it all worked out. Now I am able to use IntelliSense to its full extent. Leaving this in case others are struggling with this.
Once you've opened up the project folder from vs code, if you leave it open, double clicking scripts from unity will work fine, no need to manually open thtem from vs code everytime you want to edit a script.
Sometimes it happens if you don't set VSCode as the default editor for Unity scripts.
In Unity go to Edit > Preferences > External Tools > External script editor and choose Visual Studio Code.

Can I test/run Unity C# in Visual Studio/Code?

I'm working on a Unity project and want to test/try some code: i.e. loading a CSV and save in a different Dictionaries. As for now, I'm coding in Visual Studio/Code and have to switch back to Unity to run it.
Is there a way to test run the code in the Visual Studio/Code directly - only for console output?
This would be great for testing, instead of switching back and forward all the time.
Thanks
For a game project of mine, I set up a seperate VS project independent from the Unity project, and bound all the cs files in there.
The files tested this way did not use UnityEngine at all, but you could also set up a dummy-UnityEngine for the test. Location of this folder is outside of the Unity project folder to prevent Unity from touching those test cs files and vcproj files.
From the VS project, I can simply include any cs files from the Unity-project Assets folder.
My dir structure:
ProjectRoot/ <== real project root folder.
.git/ <== git tracking for unity part and testcode.
Unity/ <== Unity's project homedirectory.
Assets/
script
... various scripts.
mats/
scenes/
..... <== lots of Unity project stuff
testcode.sln
testcode.vcproj <== refers to cs files from testcode AND Unity.
testcode/
testclass.cs
.....
For testing, I open the testcode.sln in VS, and compile-and-run it all in there. Unity need not be open.

Content Pipeline not working despite file existing in expected locations

I've been trying to figure out why the content pipeline tool isn't allowing me to load textures and spritefonts.
My MainGame constructor:
public MainGame()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content"; //Content folder
_graphics.SupportedOrientations = DisplayOrientation.LandscapeRight;
_textureAggregate = new TextureAggregate();
}
This is the error (ContentLoadException: The content was not found):
Finally, my project folder structure:
Now, the defaultfont.xnb is being created by Content.mgcb to the ~/Content/bin folder, I've just copy-pasted it to /Content attempting to get the project to recognize at least this one file. Note that the stream is working just fine (but I don't know if that's the right way to load textures now?).
I'm looking for anything else that I might not know about that requires configuration in order to get my project to recognize these files.
You should not be adding the content into the Content folder. Instead, you need to open your content pipeline tool (Content.mgcb). You can add the textures you need there instead by clicking "Add Existing Item".
As is turns out, I didn't correctly remember the details of installing monogame and xamarin, and had created the project as an openGL game (the little icon is the same after creation on a compromised system so I thought nothing of it).
I blame this on having just redeployed my dev environments and had managed to install the monogame pipeline tool but none of the templates, so searches for the monogame project template type went nowhere but I could still create the content project in the Content folder.
I happened across all the same classes and objects I needed to do basic work without getting compile-time errors via nuget.
So, if anyone else is having a real hard time with this, make sure you installed MonoGame and are working with the templates and didn't mistakenly create an opengl game like this idiot :)

Cannot load music file

I put the music(mp3) file in the music folder and created a Song song; object in the game class.
Then I did song = Content.Load<Song>("music file name here") in the ContentLoad method and did MediaPlayer.Play(song).
But it gives me an error saying:
Could not load music asset
I tried making the "Copy to Output Directory" to "Copy always". But this also didn't solve the problem.
I heared about monogame not having a content pipe line, but I don't understand what it is. Can anyone explain what I should do to make my music file playable?
You can find a tutorial (written by me) explaining exactly how to use sounds in Monogame here.
But basically, all you need to do is:
Create a new Game project with XNA.
Add the desired sound file to the content project.
Compile.
Go to the output folder and copy the sound xnb file to your monogame one (add it on the Content folder).
Change it's properties to "Content" and "Copy if newer" so that it works.
The image has a spritefont selected, but the same applies to sound effects.
Then you can use it as you would in a typical XNA game.
As of today, I have not been able to find another way to load them without needing XNA. Same goes for sprite fonts. You need to generate the xnb files by using the Content project provided by XNA.
Remember that you can install XNA on VS 2012+ and Windows 8 without a problem thanks to XNA Refresh.
One more thing to add, the last time I used Monogame (September, 2014); there was a bug in the templates downloaded from the Monogame site that would not allow for any sound effect (song, effect, instance, etc) to be played; and this would happen with no errors being thrown.
The solution at that time was to download the Monogame templates by using Nuget.

MonoGame on linux failing to load effect assets

EDIT: The issue is now solved, I'm including the details here to help anyone else who runs into this, as it's not that simple to solve.
Basically, it comes down to updating using the develop branch.
The first step is to checkout the monogame develop branch, this will not include the thirdparty/libs submodule, so you need to also update this in order to be able to compile the sources.
After this, you can compile MonoGame.Framework.Linux.sln, and update the references in your project to point to the new dll files.
This will still not load effect files, unless they are created using an up-to-date version of the content processors, so you need to go to a windows computer, checkout the develop branch (and libs) - or copy them from your linux system - then compile MonoGame.ContentPipeline/ContentProcessors/MonoGameContentProcessors.sln
You need to update the reference to MonoGameContentProcessors.dll in your content project, you also need to rename the MonoGame.ContentPipeline\ContentProcessors\bin\Release\libmojoshader_32.dll file to simply libmojoshader.dll.
Once all of this is done, you can finally use it as intended - build the .xnb files from your .fx files and add them to the linux project.
Hopefully this information will be useful to someone.
=================================================
Original post:
I have just started out with MonoGame, and am trying to get a very simple application (rotating cube) to work on Windows and Linux - windows is not proving to be a problem, but I am having trouble getting my .fx files to work on Linux.
I have set up a "MonoGame Content Project", added the .fx file to it, selected the "MonoGame Effect" processor, set the build configuration to Linux - this all seems to work and i get a .xnb file in the output directory.
When I set up the Linux project, I copied the .xnb to the "Content" folder (the root directory is set accordingly) and used the following code to load the effect (same code as on windows):
CubeEffect = Content.Load<Effect>("Effect1");
CubeEffect.CurrentTechnique = CubeEffect.Techniques["Technique1"];
This then results in an application crash with this error:
Microsoft.Xna.Framework.Content.ContentLoadException: Could not load Effect1 asset!
Initially i assumed this to be a problem with file names, directory names, or some incorrect setting. However, i tried adding a .png file to the content folder and loading that:
Texture2D Tex = Content.Load<Texture2D>("bg.png");
This works perfectly well (checked the properties of the Texture2D in the debugger, and it has correct details for the file).
Have i missed a step somewhere when converting the shader file? Is there some really obvious thing that I'm not seeing?
If not, does anyone have any clue why it would act like this, or some way to get a more detailed error from it, like an actual reason for it not being able to load the asset?
EDIT: Having looked at the point in the disassembly where it actually throws the error, it seems that it finds the file, but doesn't recognise it as any valid resource type - could this be some kind of version/compatibility issue?
This is a very common problem when trying to load shaders into Monogame.
I tried, and failed to be able to load my custom shader into the Monogame framework.
You need to compile from the develop3d branch, and not the official release. You also need to convert your HLSL shader into a MojoShader compatible syntax. Then you need to either load the effect from the Monogame Content importer (which needs to be configured manually), or add your shader as an embedded resource and load it into your project in order to use it.
I have never been able to actually pull this off myself. From my readings online, this particular part of the Monogame framework is not quite ready for primetime yet.
Here is some information on it. They really didn't provide much information on this as I suspect they know it is very problematic:
https://github.com/mono/MonoGame/wiki/Effects-And-Shaders

Categories