【发布时间】:2013-11-24 23:13:21
【问题描述】:
我一直在用 c++ 编写一个基本的 dll,最终目标是在 Unity 中访问它的一些方法。我在以下位置开始了本教程:http://docs.unity3d.com/Documentation/Manual/Plugins.html,它帮助我理解了这个概念,但是当我在 Unity 中编写类并访问 .dll 后,当我点击播放时它会抛出一个入口点异常错误。是的,我有一个插件文件夹,用于保存我的 .dll 文件。
EntryPointNotFoundException: 添加 PluginImport.Start() (在 Assets/Test/PluginImport.cs:20)
这是我的 .dll 类。
头文件:
#ifndef MATHASSISTANT_ARITHMETICS_ARITHMETIC_H
#define MATHASSISTANT_ARITHMETICS_ARITHMETIC_H
namespace ma{
extern "C"
{
class Arithmetic
{
public:
Arithmetic();//ctor
protected:
virtual ~Arithmetic();//dtor
public:
static __declspec(dllexport) float addition(float& val_1, float& val_2);
static __declspec(dllexport) float substraction(float& val_1, float& val_2);
static __declspec(dllexport) float multiplication(float& val_1, float& val_2);
static __declspec(dllexport) float division(float& val_1, float& val_2);
};
}
}
#endif
这是源文件:
#include "Arithmetic.h"
#include <stdexcept>
namespace ma{
Arithmetic::Arithmetic(){
//TODO: Initialize items here
}
Arithmetic::~Arithmetic(){
//TODO: Release unused items here
}
//FUNCTION: adds two values
float Arithmetic::addition(float& val_1, float& val_2){
return val_1 + val_2;
}
//FUNCTION: substracts two values
float Arithmetic::substraction(float& val_1, float& val_2){
return val_1 - val_2;
}
//FUNCTION: multiplies two values
float Arithmetic::multiplication(float& val_1, float& val_2){
return val_1 * val_2;
}
//FUNCTION: divide two values
float Arithmetic::division(float& val_1, float& val_2){
if(val_2 == 0)
throw new std::invalid_argument("denominator cannot be 0");
return val_1 / val_2;
}
}
这是我在 Unity3D 中创建的类:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
public class PluginImport : MonoBehaviour
{
//Lets make our calls from the Plugin
[DllImport("Arithmetic")]
private static extern float addition(float val_1, float val_2);
[DllImport("Arithmetic")]
private static extern float substraction(float val_1, float val_2);
[DllImport("Arithmetic")]
private static extern float multiplication(float val_1, float val_2);
[DllImport("Arithmetic")]
private static extern float division(float val_1, float val_2);
void Start()
{
Debug.Log(addition(5, 5));
Debug.Log(substraction(10, 5));
Debug.Log(multiplication(2, 5));
Debug.Log(division(10, 2));
}
}
如果有人能帮助我找出我做错了什么,我将不胜感激。提前致谢!
【问题讨论】:
-
@RetiredNinja 很好的来源,但对我的问题没有帮助。我仍然收到
EntryPointNotFound: addition错误。