我在cpp中有一些算法,并试图统一使用它们。
阅读http://www.ericeastwood.com/blog/17/unity-and-dlls-c-managed-and-c-unmanaged之后,很高兴在cpp中使用它们并以统一的c脚本使用它们。
但是如果我想上课的话怎么办?我想要我的cpp类,构建读取资源和接口以更新所有内容。如何在团结中创造并保持一个阶级?
我的虚拟示例代码的工作方式如下:
·H
// TestCPPLibrary.h
#ifdef TESTFUNCDLL_EXPORT
#define TESTFUNCDLL_API __declspec(dllexport)
#else
#define TESTFUNCDLL_API __declspec(dllimport)
#endif
extern "C" {
class myMath{
private:
int member;
public:
myMath();
TESTFUNCDLL_API float multiple(float a, float b);
TESTFUNCDLL_API float getMember();
protected:
virtual ~myMath();
};
TESTFUNCDLL_API float TestMultiply(float a, float b);
TESTFUNCDLL_API float TestDivide(float a, float b);
TESTFUNCDLL_API float get();
TESTFUNCDLL_API myMath* _cdecl CreateMath();
// Function Pointer Declaration of CreateMathObject() [Entry Point Function]
typedef myMath* (*CREATE_MATH) ();
}
的.cpp
// TestCPPLibrary.cpp : Defines the exported functions for the DLL application.
//
#include "TestCPPLibrary.h"
extern "C" {
float TestMultiply(float a, float b)
{
return a * b;
}
float TestDivide(float a, float b)
{
if (b == 0) {
return 0;
//throw invalid_argument("b cannot be zero!");
}
return a / b;
}
float get()
{
return 5;
}
myMath* _cdecl CreateMath()
{
return new myMath();
}
float myMath::multiple(float a, float b)
{
if (b == 0) {
return 0;
//throw invalid_argument("b cannot be zero!");
}
return a / b;
}
float myMath::getMember()
{
return member;
}
myMath::myMath(){
member = 6;
}
myMath::~myMath()
{
}
}
急剧代码
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("TestCPPLibrary", EntryPoint="TestDivide")]
public static extern float StraightFromDllTestDivide(float a, float b);
[DllImport("TestCPPLibrary", EntryPoint="?getMember@myMath@@QEAAMXZ")]
private static extern float getMember();
private GameObject cube;
// Use this for initialization
void Start () {
cube = GameObject.Find ("Cube");
float straightFromDllDivideResult = StraightFromDllTestDivide(20, 5);
Debug.Log(straightFromDllDivideResult);
float multipleResult = getMember();
Debug.Log(multipleResult);
}
int i = 1;
// Update is called once per frame
void Update () {
cube.transform.Rotate (Vector3.up * 50f * Time.deltaTime);
// Vector3 newVector = cube.transform.position;
// newVector.z = 15;
// cube.transform.position = newVector;
cube.transform.position =new Vector3(0,0,i++ * Time.deltaTime);
}
}
我不知道如何将一个新课改为一个物体,并用c锐利的方式保持它。
谢谢!