Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

c# - How to add c++ code to Unity project easy-way?

So, I am trying to build an app in Unity, which requires molecular visualisation. There are some libraries which can be used to estimate properties of molecules, read molecules, write molecules etc. But there are few for visualisation. I have found this one, called hyperballs and it is used successfully in a Unity project for molecular visualisation called UnityMol. I have already added OpenBabel dll to the project now I wish I could add hyperballs the same or any other way to unity project.

The problem is that I lack experience with making dlls (no experience at all, to be honest).

Also I don't know how to use hyperballs c++ files inside the Unity project. Thinking of an analogy with OpenBabel I thought that if there is a simple way to make a dll from c++ source code on a Mac, I could probably simply add dll to assets and enjoy coding, but it is not as easy, as I thought.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to wrap the C++ code in C functions, then use them through P/Invoke.

For example,

MyPlugin.cpp

#define MY_API extern "C"

class Context
{
public:
    const int version = 12345;
};

MY_API int GetVersion(const Context* _pContext)
{
    if (_pContext == nullptr)
    {
        return 0;
    }
    return _pContext->version;
}

MY_API Context* CreateContext()
{
    return new Context();
}

MY_API void DestroyContext(const Context* _pContext)
{
    if (_pContext != nullptr)
    {
        delete _pContext;
    }
}

Then compile the above code into *.dll for Windows, *.so for Android, Cocoa Touch Static Library for iOS and bundle for macOS.

Usage in C#:

MyPlugin.cs

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class MyAPI : MonoBehaviour
{
#if UNITY_EDITOR || UNITY_STANDALONE
    const string dllname = "MyPlugin";
#elif UNITY_IOS
    const string dllname = "__Internal";
#endif

    [DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr CreateContext();

    [DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
    private static extern int GetVersion(IntPtr _pContext);

    [DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
    private static extern void DestroyContext(IntPtr _pContext);

    static MyAPI()
    {
        Debug.Log("Plugin name: " + dllname);
    }

    void Start ()
    {
        var context = CreateContext();
        var version = GetVersion(context);
        Debug.LogFormat("Version: {0}", version);
        DestroyContext(context);
    }
}

References:

  1. https://docs.unity3d.com/Manual/NativePlugins.html
  2. http://www.mono-project.com/docs/advanced/pinvoke/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...