2

my first question on here and I cant seem to find a similar question so sorry if this has been asked before.

This is Unity related by the way, and yes I have also posted on Unity3D answers but thought I might also be able to get help from knowledgeable individuals on stack overflow also.

Basically I am wondering if there is a way to make platform dependent objects or components. I know I can wrap code in pre-processor directive commands which I have been doing, but I tend to use a ton of plugins, many of which only function on specific platforms.

However, I have just one project for all my platform dependent versions of the game and wish to continue working this way. So I was wondering if somehow I can make say an object that has the compatible plugin components, that will only be created if a certain platform is being built for.

If not, is there a way to make an object use say a specific plugins component if say on WP8, but then another entirely different component in its place if building for Android?

If these are not possible, how do you guys get around having platform dependant plugins? Do you simply just make separate projects for each platform? (feels like that defeats the point of unity's cross platform-ness though...)

Thanks guys, any help will be greatly appreciated!

  • 1
    "how do you guys get around having platform dependant plugins?" <- Avoid them as much as possible. If it's essential, such as Google Play vs Apple Game Center integration, there's no way around it. In any case a platform-specific plugin should not alter gameplay itself. I'd rather pay good money for a properly cross-platform plugin that does the same thing equally well on all platforms than combining just two free/cheap plugin doing that for specific (but not all desired) platforms only. – CodeSmile Jul 30 '14 at 17:40
  • Fair enough, but I am using plugins that deal with different in-game advertising, mainly I am using Pubcenter on Microsoft platforms due to being a Microsoft Windows Game Ambassador so its a must for work, but for Android and iOS I am using Admob related plugins in its place. Is there really no way to get around this other than doing everything within code and using pre-processor directives? – Isaac Surfraz Jul 30 '14 at 17:46
  • 1
    Not really. That is unless my suggestions to allow for magic in this world has been implemented by now (haven't checked in a long while). ;) – CodeSmile Jul 30 '14 at 19:39

1 Answers1

0

Unfortunately, you will be forced to rely on pre-processor directives if you need to run unique code per platform.

However, this can be a lot easier to manage if you apply a facade-based design pattern. Instead of peppering platform-specific details in myriad scripts throughout your project, you can create one or more facade components that expose a more generic, abstract interface. The facades can internally manage prefabs, APIs, or whatever other platform-specific details you need.

As an example, you could write a SaveManager class that manages player save data. On platforms where direct file access is available, the SaveManager instantiates and controls an internal FileSaveManager that uses direct file access. On other platforms such as web builds, the SaveManager instantiates and controls a PlayerPrefsSaveManager that uses Unity's PlayerPrefs system instead. That way, each of those classes contains only the code that it needs, and other classes can just call SaveManager without worrying about those details.

Community
  • 1
  • 1
rutter
  • 11,242
  • 1
  • 30
  • 46