4

I'm trying to keep my game logic API-agnostic. In doing so, I don't want to use Unity3D's Vector3 struct, or XNA's, etc. What's the best way to do this? It seems like it would be awfully cast-heavy if I rolled my own Vector3 struct and just wrote implicit converters to the various API implementations.

Is there a best practice for this kind of thing? It's not like I can ask Microsoft, Unity, etc. to have a picnic and adhere to a common interface.

George R
  • 3,784
  • 3
  • 34
  • 38

2 Answers2

3

It looks like XNA and Unity implementations of Vector3 are really similar, the main difference being the case of property names. Too bad there's no way to implement extension properties.

What you could do is create a set of extension methods, together with some conditional compilation (#if UNITY_3_3 or #if XNA) and exclusively use those extension methods to retrieve values in your API agnostic code. This would still allow you to pass the Vector3 object unmodified to platform specific code requiring Vector3's without tons of casting.

Calvin
  • 4,177
  • 1
  • 16
  • 17
  • Hadn't thought of conditionals, however what I've done instead is opted to make a separate project per API that handles conversions. A better explanation would have been that I'm using Unity for the client, but the server code will be a regular console application although it will still need to use Vectors and so on, so I don't want to have dependencies on Unity in places it shouldn't really belong! – George R Mar 15 '11 at 09:10
2

Based on my experience, I would select one platform to write your logic to, and then, on the other platform, create a matching implementation with conversions to the platform's native types. There's no point in designing and writing a third implementation and then having to do conversions to both platforms!

All platforms being equal, I would probably favour XNA's, as you can grab an open source implementation from MonoGame or (eventually) ExEn and use that.

However, in practice I think you're probably best off to start out with the platform you intend to develop on (XNA or Unity) and then create your compatibility layer once you actually need it.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104