UnityのWindowsアプリでC++を使用する

C++でDLLを作成し、それをUnityでimportすることになる。

Unity側のDLLを使用するクラス

Hoge.cs

using UnityEngine;
using System.Runtime.InteropServices;

public class Hoge{

  // .dll 拡張子は書かないでよい
  [DllImport("plugin")]
  public static extern int Hoge();

}

DLL 側

これを自分でビルドして、
Assets/Plugins の中に保存することで、DLLをimport 出来るようになる。

plugin.h

#define DLL_UNITY extern "C" __declspec(dllexport)

DLL_UNITY int Hoge ();

plugin.cpp

#include "plugin.h"

// 共通言語ランタイムサポートを有効にして使用することもできる
using namespace System;

int Hoge () {
  return 3;
}