如何添加c ++代码到Unity项目的简单方法? [英] How to add c++ code to Unity project easy-way?

查看:264
本文介绍了如何添加c ++代码到Unity项目的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在Unity中构建一个应用程序,需要分子可视化。有一些库可以用来估计分子的性质,读分子,写分子等。但是很少可视化。
我发现这一个,称为超级球,它被成功地用于Unity项目中,用于分子可视化,称为 UnityMol 。我已经将OpenBabel dll添加到项目中,我希望我可以将超级球相同或任何其他方式添加到团队项目中。



问题是我缺乏制作经验dll(没有任何经验,诚实)。



另外我也不知道如何在Unity项目中使用超级游戏c ++文件。
想想与OpenBabel的类比我认为,如果有一个简单的方法来从Mac上的c ++源代码制作dll,我可以简单地添加dll到资产和享受编码,但它并不那么容易,

解决方案

您需要将C ++代码包装在C函数中,然后通过P / Invoke使用它们。 p>

例如,



MyPlugin.cpp

  #define MY_API externC

类上下文
{
public:
const int version = 12345;
};

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

MY_API上下文* CreateContext()
{
return new Context();
}

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

然后将上述代码编译成 *。dll for Windows, *。so for Android, Cocoa Touch Static Library 适用于iOS和 bundle for macOS。



C#中的用法:



MyPlugin.cs

  using System; 
使用System.Runtime.InteropServices;
使用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);
}
}

参考文献:


  1. https://docs.unity3d.com /Manual/NativePlugins.html

  2. http://www.mono-project.com/docs/advanced/pinvoke/


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.

解决方案

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/

这篇关于如何添加c ++代码到Unity项目的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆