Quantcast
Channel: Questions in topic: "dll"
Viewing all articles
Browse latest Browse all 706

how to use cpp dll plugin with class in unity

$
0
0
new bee. I have some algorithm in cpp and trying to use them in unity. after reading [http://www.ericeastwood.com/blog/17/unity-and-dlls-c-managed-and-c-unmanaged][1], it's nice to have functions in cpp and use them in c sharp script in unity. But what if I would like to have class things. I want my cpp class, construct reading resources and interface for update things everyframe. how to create and hold a class in unity c sharp? my dummy sample code works like: .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() { } } c sharp script 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); } } I don't know how to new the class to an object and hold it in c sharp. Thanks! [1]: http://www.ericeastwood.com/blog/17/unity-and-dlls-c-managed-and-c-unmanaged

Viewing all articles
Browse latest Browse all 706

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>