简体   繁体   中英

Using a C++ game engine from C# using SWIG

I have recently acquired the C++ source code of a 3D game engine I plan to use. However my C++ coding skills are far from legendary, and I definitely find C# easier to work with. I have seen components that are implemented fully in C++ and have only a set of wrapper classes in .NET, that have no source code - instead they point directly to the DLL's classes using the [ComImport] keyword to mark classes as external, and the `public virtual extern' keyword to mark functions as external. Is it possible therefore to use SWIG, create classes and methods in C# that link directly to the classes and methods in the DLL of the C++ game engine? So I should be able to instanciate the classes of the C++ engine directly in .NET, and control them from .NET. Is this possible?

You can only use [ComImport] if you C++ classes are COM objects. There is a strong possibility that they are not given this is a Game Engine. (Search your C++ code for COM methods like CoInitialize to know for sure)

Given you have the source, theoretically SWIG will create C# wrappers for your C++ , without needing you C++ classes to be COM objects, However don't expect this to be entirely trivial.

An alternative approach I've taken in the past is to write a 'C' wrapper for my C++ (google 'extern "c"') and then its easy to create C# wrappers, using pinvoke. Whether this is possible in you've case will depend on the C++ api.

Bear in mind that any C# => native (c, c++ etc) will involve some performance penalty. How much will depend on how frequent the C# -> native (or native -> C#) calls are made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM