如何将 C++ 代码添加到 Unity 项目中? [英] How to add c++ code to Unity project easy-way?

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

问题描述

所以,我正在尝试在 Unity 中构建一个应用程序,这需要分子可视化.有一些库可用于估计分子的特性、读取分子、写入分子等.但用于可视化的库很少.我发现了这个叫做 hyperballs 并且它在一个名为 UnityMol.我已经将 OpenBabel dll 添加到项目中,现在我希望我可以以相同或任何其他方式将 hyperballs 添加到统一项目中.

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.

问题是我缺乏制作 dll 的经验(说实话,根本没有经验).

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

我也不知道如何在 Unity 项目中使用 hyperballs c++ 文件.想到与 OpenBabel 的类比,我认为如果有一种简单的方法可以在 Mac 上从 c++ 源代码制作 dll,我可能可以简单地将 dll 添加到资产中并享受编码,但这并不像我想象的那么容易.

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.

推荐答案

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

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

例如

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;
    }
}

然后将上面的代码编译成Windows的*.dll,Android的*.so,iOS的Cocoa Touch Static Library和<代码>捆绑适用于macOS.

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

在 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);
    }
}

参考文献:

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

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

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