Developing a Cross Platform XNA Library - c#

I'm trying to develop a cross platform application.
Currently I have set up following solutions, with their platform specified template:
GameBase
Windows
Xbox
WindowsPhone
The Windows, Xbox and WindowsPhone uses the GameBase as reference, but I want to separate different code for each platform used in the GameBase library as the example below:
#if WINDOWS
graphics.PreferredBackBufferWidth = 640;
graphics.PreferredBackBufferHeight = 480;
graphics.ApplyChanges();
#elif WINDOWS_PHONE
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
#endif
It does not work, and I can see that in the Configuration Manager, that GameBase's platform is Any CPU, so I wonder if it has anything to due with the GameBase being a Class Library?
If so how do I fix it, and if possible I would like to keep the #if tags.
Directory setup | Windows Solution Game.cs | Solution Explorer

You have to create separate Visual Studio projects for the different platforms. You can reuse code across multiple projects by adding the source using Add As Link. Within a shared source file that is linked to multiple project you can use #if etc. to separate platform specific code.
How "It does not work" is not clear from your question but in general you cannot build an assembly (e.g. class library) for Windows and then use it on XBox and Windows Phone.

Sharing projects between different project types is a tricky thing to do - just because of the way Visual Studio project types are used and because the different project types actually need to reference different versions of the dot net framework.
There are a few things that can assist:
the Portable Class Library tools - http://msdn.microsoft.com/en-us/library/gg597391.aspx
the Project Linker plugin - http://msdn.microsoft.com/en-us/library/ff921108(v=PandP.20).aspx
manual cut and paste (including add link like above)
Hope that helps a bit. Personally I'm currently mainly using manual cut and paste and hoping that the next version of the Portable Class Library helps (it is due for Win8 Metro stuff)

Not sure if this is your only problem but you mispelled WINDOWS_PHONE:
WINDOWS_HPONE
should be:
WINDOWS_PHONE
UPDATE:
The way that I would do it is I would create a class library solution calling it something like GameBaseLibrary. Then create a project there called GameBase and add all code in there that you would like to share across all the platforms. (I.E. Physics, data access, etc...)
Then in that same solution add XboxBaseLibrary, WindowsBaseLibrary, WindowsPhoneBaseLibrary and add code which is specific to each platform, add a reference in each of those projects to the GameBase project to use code from there in each of the three other projects.
Then create 3 different solutions types - Xbox, Window, Windows Phone and add the compiled dlls from GameBaseLibrary as a reference in each of them to be able to use the code. As you're working on each version of code (xbox, windows, windows phone) pay attention to code that you're typing to ensure that it's not something which can live within the GameBase class because you don't want to keep rewriting the same code.
This way, the bulk of the code is sitting in GameBaseLibrary and the platform specific code is sitting within each of the 3 other projects.
I hope this is clear.

Related

How do I build multiple "targets" or "flavors" of an app using Visual Studio?

I need to build three completely different flavors or targets for my C#/XAML UWP app. Each of them will get placed in the Windows Store using a different name and will have different branding on it. Essentially, I produce three apps that have very similar features but need to be three different apps for business reasons. There is almost zero difference in the source code for these variations.
In Visual Studio, I created three folders and each contains the icons, manifest, certificate files, etc., for the target apps. I use a pre-build event to detect the configuration that I selected for the build and copy the appropriate files to the root of the project. Note that instead of "release" and "debug" builds, I have "release1", "release2", etc. and my configuration folders use matching names.
This all worked great with Visual Studio pre-2019! When I first set things up this way, it worked and was the easiest way I could find.
Visual Studio 2019 validates the element in the project file and my build process no longer works. If I make this empty, something during the build process sets it. I think it gets set when I create app packages for the store and is otherwise untouched.
What is the better way to build multiple targets or flavors of the app? Alternatively, how can I avoid this problem (and it's a problem since that value doesn't actually help me anywhere!)
So far, I have the way I do it now and I've also considered creating separate projects for each flavor with each project using all of the code from a shared project. In fact, I have a shared project that contains code that was once shared with the Windows Phone app that was part of the same solution. I just don't know if that multiple-project solution will work or not and it's hard to justify to the boss the day or two it might take to change the project structure.

Unity 3D single build for various projects

I need to develop a single Unity3D standalone build for separate project modules.
Consider, I have various projects related to 'Physics' for the 12th grade, which contains separate models, images and their respective functionality. For example, Project1: 'Optics', Project2: 'Electrostatics', Project3: 'Dual Nature of matter' and so on. All of these projects are independent of each other in terms of functionality and are complete on their own. Now, I create a 'Unity build'(say, for android initially) that contains a menu layout along with hyperlinks to download separate projects aforementioned from a server. The requirement is to allow the user to download a project(Say, project1: 'Optics') jar/build out of many projects available as an extension/addition to the previous 'Unity build'(even if the size of the build already installed might increase on device). As soon as the project is downloaded, the user should be able to run the project module over the 'Unity Build' instead of running a separate application. Similarly, when the user is done with the downloaded project module(Project1: 'Optics'), another project module(say,Project2 : 'Electrostatics') can be downloaded from the menu of the build which will run over the same 'Unity Build'. The 'Unity build' application serves as an interface which can add a separate layer of encryption so that the projects cannot be downloaded over and run separately by any unrestricted user. Somewhat similar to a media player which can play various kinds of video files. However, in this scenario, I need to run various project modules.
I am looking to create builds for different platforms - Android, Windows, iOS and Web browser (if possible) with similar functionality.
Any kind of help will be appreciated :)
Thanks!
There is a an "example" that comes with Unity as an install option and I believe it does exactly what you want, it has a menu where you can select different demos, where each demo is a project in itself. You should look into that.
Your question is mostly about organisation.
Sounds like your 'Unity build' is a kind of downloader with menue. May be this could be a website. Is the server a webserver?
The server has to have all projects for all possible plattforms, like a matrix.
If somebody starts to download a project, you have to choose the right build for his plattform.
To estimate the plattform, you could send a parameter with the link.
All of these points have many solutions depending on your requirements.
Good luck.
What you need are Asset Bundles.
What are they?
Compressed packages that serve as game extensions that can be downloaded and imported to a unity build at any time. With AssetBundles you can package scenes and load them into unity and load them.
How to create AssetBundles?
Basically you can select any Asset from your project and in the inspector you assign that Asset to a new AssetBundle in the inspector (at the bottom it says Create AssetBundle). A detailed section can be read here https://docs.unity3d.com/Manual/BuildingAssetBundles.html
In your case you should create many Asset Bundles with one scene in each.
How to download and load unity scene files from AssetBundles?
It could be a bit tricky but it has already been accomplished here http://forum.unity3d.com/threads/dynamically-load-scene-from-assetbundle.379316/
Common pitfall
Remember that AssetBundles aren't cross platform compatible. In other words, the Asset Bundle created is restricted only to the platform it is built for so you need to recreate your AssetBundles for each platform
Hope this helped!

Creating Wp8 Unity Plugin in Unity 5.0

I have a windows phone 8.0 plugin created for an app. This app worked perfectly fine in Unity 4.5. I recently updated my Unity to 5.0. I also saw all the needed alterations.
Initially, we needed to create two plugin dlls. One actual and one empty simply like an interface for the editor. This went pretty well in unity 4.5
Now i open the same app in Unity 5.0 by upgrading it. and i am unable to build it for wp8 anymore. I set the inspector platforms correctly to WP8 player for my plugin dll.
But, i keep getting errors like ::
1. Plugins are colliding.
2. There are errors in the script (which are actually not)
3. Cannot load classes from module.
4.System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
etc.
Can anybody help me? I have tried several things already but none seems to work.
I tried :
1. Reimporting everything
2. Rebuilding the dlls.
3. Removing the empty interfaced dll for editor
4. Changing the folder structure for plugins. (Default being
Assets/Plugins/WP8)
I am stuck here since two days now.
Well, it's too late to reply. But anybody else stuck in same problem may get help.
One should know that WP8 Plugins are based on real and fake dlls in order to work in Unity Editor.
In Unity 5, you can specify platform to dll, irrespective of folder structures. However if you put dll in Assets->Plugins->WP8. Unity will mark it as WP8 dll.
Solution is,
1- Put Fake dll in Assets->Plugins->AnyFolder->plugin.dll, where any AnyFolder is other than WP8 folder. Mark it as Editor instead of Any Platform (from inspector [Select platform for plugins]).
2- Put Original dll in Assets->Plugins->WP8->plugin.dll, mark it as WP8Player
3- Keeping original dll selected, in Platfrom Settings [Inspector] modify settings like, CPU -> Any CPU, check mark on Don't Process, Select placeholder.
Placeholder drop down would show the path of fake dll, just set it.
Now you are all done :)
You must set the editor DLLs to "Editor" instead of "Any Platform" and the WP8 plugins to "WP8Player" in the plugin settings.

How to share code and resources between projects

I have two projects named Project 1 and Project 2. I am porting my application from iOS to WP8 and I have ported my iOS_Project 1 to WP8_Project 1. In iOS I have the flexibility to add the files as references and thereby I can achieve code reuse. I would like to perform the same process in WP8, but I couldn't find proper help and here's the question. (Here I want to refer all the classes, images and xaml files like MainPage.xaml)
I know pretty sure about one thing that wpf/forms/.NET projects differ with WP8 structure. Please do provide some help.
Thanks.
When you have add your item to one project, select the option to Add Existing Item to the other project. Navigate to the item you want to add and then you should find a drop-down arrow on the appropriate button to allow you to add the item as a link. That means that there will only ever be one copy of that item but it will be used in both projects.That said, if possible, I would suggest that you break the common functionality out into a library project that you then reference in both the other projects. If you create a portable library then you could use it in a Windows 8 app as well.
You can use references in Visual Studio 2012 - there is no problem. However, PCL (Portable Class Library) is a better solution. Here is example how to add files as link in Visual Studio 2012.

Is it possible to compile a console application into a single .dll file?

There are 5 console apps working off each other's outputs and are separately installed on my computer (C# 4.0, I am the author) . Management would like to distribute this suite of apps to other users, but aren't thrilled about asking non-tech users to install/configure 5 separate applications. Is there any way I can compile each program down into a .dll and reference them through a single master application?
Q. The main issue seems to be that you don't want 5 separate installation steps?
A. Make an installer for the suite :) Use any MSI builder (WiX, Visual Studio setup projects, InstallShield, and many others; Heck, you could even do an XCOPY deployment in most cases)
Q. How do I directly invoke these programs from within a single process?
A. Options:
Load the assemblies in your AppDomain.
Use a separate AppDomain in case of name(space) conflicts or version conflicts
Q. How do I optionally 'hide' the presence of the external console apps from view
A. Look at ilmerge to possibly combine the 'external' assemblies so they aren't visible anymore. If you can't use ilmerge (conflicts, or e.g. WPF apps) you might embed them as resources and load them on demand as per 1. and 2. above
Update: https://libz.codeplex.com/ is a nice looking project that makes this easy (haven't tried it myself)
Just because each of them is a separate .exe file doesn't mean you can't treat them as one application. And they don't have to be installed or configured separately either.
But a much better solution would be to rewrite each of the applications, so that they expose classes or interfaces that can be used without actually running the application. This way, communication between the parts is going to be much easier.
In .Net, the only difference between .exe and .dll is that you can run .exe directly. But you can treat both as libraries, so you can use functionality from one .exe in another .exe. Another step might be separating the core of each application into a .dll and make the .exes just deal with input and output. With this, the combined application wouldn't have all the code that it doesn't need from the other ones.
Its possible if every assembly is using different class names. Just include the whole source code when you compile the final version in one project.
Go to Project's properties, Application and change OutputType from Console to Class Library.
EDIT
Would like to express my doubts on architectual desicion like this, correct me if I'm wrong in my thinking:
Having different EXE applications standalone, I presume, you have different Applications that works standalone.
What advantage you gain by converting them in DLL's and puting them together in one master app ? Why do not just use this EXEs with one master app and launch them ?
In this way you leave as is it already working + you add a layer (master app) so for final user all this seems like one single app.
That is possible - several options:
you put the functionality of each console app into a separate class within the same project and have one "master console app" provide their functionalities
you put the functionality of each console app into a separate class each in different project with DLL as target, then you reference those DLLs as needed from your "master console app"
Note: IF you go the DLL route you could embed the DLLs into the console EXE using the technique from http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
With both option (all in one EXE or EXE + embedded DLLs) you can just make an XCOPY deployment if there are no other dependencies...

Categories