API 不可知的 Vector3、Matrix 等?
我试图让我的游戏逻辑与 API 无关。这样做时,我不想使用 Unity3D 的 Vector3 结构或 XNA 等。执行此操作的最佳方法是什么?如果我推出自己的 Vector3 结构并只为各种 API 实现编写隐式转换器,那么看起来会非常繁重。
对于这种事情有最佳实践吗?这不像我可以要求微软、Unity 等公司去野餐并遵守通用界面。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它看起来像 XNA 和 Unity
Vector3
实现非常相似,主要区别在于大小写属性名称。可惜没有办法实现扩展属性。您可以做的是创建一组扩展方法,以及一些条件编译(
#if UNITY_3_3
或#if XNA
)并专门使用这些扩展方法来检索与 API 无关的代码中的值。这仍然允许您将未经修改的 Vector3 对象传递给需要 Vector3 的平台特定代码,而无需进行大量转换。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.根据我的经验,我会选择一个平台来编写逻辑,然后在另一个平台上创建一个匹配的实现,并转换为该平台的本机类型。设计和编写第三个实现然后必须转换到两个平台是没有意义的!
在所有平台相同的情况下,我可能会更喜欢 XNA,因为您可以从 MonoGame 或(最终)ExEn 获取开源实现并使用它。
然而,在实践中,我认为您可能最好从打算开发的平台(XNA 或 Unity)开始,然后在实际需要时创建兼容层。
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.