I don't know well matlab DLL, but as stated in the link, the DLL has to respect specific rules. Like C linkage (using extern C) to avoid name mangling issues. If it is not the case by default with matlab, you "probably" could make a wrapping DLL around the orignal dll but with C linkage.
If it's a pure .NET library, I believe you can use it in Unity Free. (If it's just a C# script, uncompiled, then you can almost definitely use it in Unity Free.) If it's a C++ DLL, you'll need Unity Pro.
Im also trying to use a dll generated by MATLAB, I've tried compiling it to C,C++, .NET(3.5), with "using mylibrary;", with [DllImport ("mylibrary")], changing settings inside unity, copying them in the Plugins folder , using free and pro, etc etc no luck anyone? what am i doing wrong? this is the function in mathlab, i want only to print a number in console function a=mycos(b) a=cosd(b); the dlls are generated automatically in matlab with deploytool using visual studio 2010 .net librarys
What errors do you get when you build a .NET 3.5 DLL and put it in a Unity project? There's no need to try referencing it, Unity should just try to use it and may print errors.
Anything that requires the InteropServices (like DLLImport) requires Pro to work. It will not work without pro And you can't write to the unity console from native code, you would need to return the value to unity and output it there
I'm using pro and returning a value, look at the code in matlab function a=mycos(b) a=cosd(b) the returning code is a, the dlls are generated automaticly using deploytool, yes im using an script inside unity to write to console. well it seems that you cannot do this because it needs the MCR from matlab, so maybe copying all these dlls into unity? i will try and post my experiments later.
If it requires additional native code components it gets extremely more complicated depending on the 'path' it uses to lookup. you might need to manually copy it next to the exe and copy them additionally next to the editor exe for example, or into the systems global reachable path aka system32 / wow (or one of them as you likely have many of them as most of us ) for matlab I would have assumed that it actually looks at the registry and require a licensed matlab install on the machine to run (similar to mathematica)
not really you dont need a licensed matlab install to run your compiled dll or exe just install the MCR(MATLAB Component Runtime) in the target machine and it allows to execute your app without installing matlab and its free, but maybe the registry lookup part is true, i will try your advice and post later the results if anyone have another tip it will be greatly appreciated
Ah okey thats cool Yes best see if the installation of MCR on a new system expands the 'path' environment variable with the path there. If it does not (as its the case for Mathematica too), then you will need to do that
it works!!!!!!!. 0.9998 UnityEngine.Debug:Log(Object) PluginImport:Start() (at Assets/PluginImport.cs:34) this is the result of doing cos(1) in deg mode using a matlab dll lol had to do lots of things ill post the steps for doing it in another post So i have to answer to the friend Fantasy.Gamer yes you can use a matlab dll in unity but its tricky
too many hours lost just for this!!!! Before anything, 1) install Visual C++ Studio Express 2010 we need the sdk from .NET for compiling, (NOTE:Maybe you can use visual C# express instead for compiling because it require the .NET sdk but no sure ) 2)You must setup matlab with the command "mbuild - setup", and choose the compiler Microsoft Visual C++ Studio Express 2010 3) you must have a function already created and saved to a ".m" file mine is called mycos.m here is its contents: function a=mycos(b) a=cosd(b); end where "a" is the return parameter and "b" the input parameter, it is important to know how many return parameter you have in this case 1 (see step 12) 4) use the "deploytool" command, a window should open, name the project "simplelibs" any name should do, choose a location c:\matlab\simplelib, choose in type .NET Assembly, OK 5) After that new options int form of tabs should appear to the right of your window, the Build and Package Tabs. In the Build tab you should add a class with the Add class option, it will automaticly was named Class1, but i changed it to Myclass, here you can add your functions to your class using Add files, i've added mycos.m 6) You should have noticed 3 buttons next to the project name, click the 3rd one with the shape of a nut, go to Settings a new window should appear, go to .NET tab and in the Microsoft Framework choose 3.5 to make sure you are using .net 3.5 for compiling(required for unity3d) 7) we are ready to compile, do so with the 1st button, it should take a while.... 8) After compiling without errors, the files are in the distrib folder inside your project path , open unity and create a new project. copy the 2 dlls into the root inside unity. 9) Locate de directory "dotnetbuilder" inside "c/.../matlab/../toolbox" copy or drag it all its contents to your unity project. 10) We need the a dll MWArray.dll because when compiling our libraries matlab wraps our function in an Array and it use its methods to access our functions im not gonna explain this, google it please, normally it can be found inside matlab installation dotnetbuilder/bin/win32/2.0/ but since you have copied it to unity dont copy again, when using this mwarray i found a bug with monodevelop in this library so i had to decompile it and fix the bug i've to recompile it again you you can download this file here https://dl.dropbox.com/u/6716823/MWArray.dll and replace the one found inside unity project (do not!!! replace the original from matlab directory) 11) create a c# script and asign it to main camera or any object in scene here is its contents using UnityEngine; using System.Collections; using System; using MathWorks.MATLAB.NET.Arrays; // import from MWArray.dll using simplelib; public class myMatlab : MonoBehaviour { void Start () { simplelib.Myclass g = new simplelib.Myclass(); Debug.Log(g.mycos(1,95).GetValue(0)); } } 12) g.mycos(1,95); // remember when i said it wraps your function !!! the first parameter is the number of parameters returned from our matlab function in this case 1, the 95 is the parameter we want to send to our function, we get an array we can access it with .GetValue(0) 13) play and enjoy This is an example with a simple function so i dont know if it will work with a more complex program i hope this works for you
Will this only run on unity on windows or on mac also? This is really cool if it works! Is this the same as using the builder ne tool? Also Would a function be a lot slower using this method as compared to directly calling it in unity? Many sites talk about having to include mcr or something like that, does this bypass that step? Thanks, Dan
Will this only run on unity on windows or on mac also? don't know probably it will on mac This is really cool if it works! it is working for me in windows Is this the same as using the builder ne tool? step 4 Also Would a function be a lot slower using this method as compared to directly calling it in unity? I dont know really update ---- now i know it runs pretty slow on my application i had to rewrite and implement some of matlab's functions Many sites talk about having to include mcr or something like that, does this bypass that step? in some sense yes. But not completelly as you can see in step 9, i copy some of the mcr files (dotnetbuilder folder) inside the project folder after that the mcr is not required(but haven't tested it). Remember unity compiles all inside a main file. Thanks, Dan
Hi geomorillo and all ... I face the problem after use my compiled dll on Unity like this .... DllNotFoundException: mclmcrrt7_15.dll MathWorks.MATLAB.NET.Arrays.MWArray..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for MathWorks.MATLAB.NET.Arrays.MWArray MathWorks.MATLAB.NET.Utility.MWMCR..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for MathWorks.MATLAB.NET.Utility.MWMCR simpleCosLib.myClass..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for simpleCosLib.myClass NewBehaviourScript.Start () (at Assets/NewBehaviourScript.cs:10) my system is windows 7 64 bit , matlab version 2011a Have any idea? I need your help. Thanks.
Seems like mclmcrrt7_15.dll is missing... did you install mcr? mclmcrrt7x.dll is a run-time library needed to run MATLAB Compiler generated applications on deployment machines that do not have MATLAB and MATLAB Compiler installed. This library is installed during the course of the MATLAB Compiler Runtime (MCR) installation. This error typically results from one of two situations: 1. The correct version of MATLAB Compiler Runtime (MCR) is not installed. Applications generated with MATLAB Compiler depend on the version of the run-time library specific to the version of the MATLAB Compiler that generated the application. You can find the install program for the MCR in the following sub directory of the MATLAB installation which created the program. $MATLABROOT\toolbox\compiler\deploy\win32\mcrInstaller.exe or $MATLABROOT\toolbox\compiler\deploy\win64\mcrInstaller.exe (where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing: matlabroot at the MATLAB command prompt.) Instructions for installing the MCR are located in the documentation: Deploying to End Users :: Deployment Process (MATLAB Compiler) http://www.mathworks.com/help/compiler/deploying-to-end-users.html Running MCRInstaller.exe on the deployment machine will install the MCR, including the mclmcrrt7x.dll: $MCRROOT\v$VER\runtime\win32\mclmcrrt$VER.dll ($MCRROOT is the MATLAB Compiler Runtime root directory that was chosen during the MATLAB Component Runtime installation process. This typically defaults to something like C:\Program Files\MATLAB\MATLAB Compiler Runtime. $VER is the MCR version number without the periods.) The “missing mclmcrrt7x.dll” error message will result if you have not installed the version of the MCR that corresponds to the version of MATLAB Compiler the application was compiled with. For example, if you have installed the MCR for MATLAB Compiler 4.9 (R2008b), version 7.9 of the mclmccrt DLL will be installed in, for example: C:\Program Files\MATLAB\MATLAB Compiler Runtime\v79\runtime\win32\mclmcrrt79.dll. For releases prior to R2008a the runtime libraries will be located in a slightly different directory tree: C:\Program Files\MATLAB\MATLAB Component Runtime\ Now, if an application generated using MATLAB Compiler 4.6 (R2007a) is executed on this computer without installing that version of the MCR, the "missing mclmcrrt76.dll" error would result. 2. A second option is that the system's PATH environment variable was not set correctly. During the course of the MATLAB Compiler Runtime installation, the following directory should have been added to your system path: $MCRROOT\v$VER\runtime\win32 To work around this issue, manually update the PATH environment variable on your deployment machine to include the following directory: $MCRROOT\v$VER\runtime\win32 For instructions on modifying your system PATH environment variable, refer to the solution referenced below. Also note that since these DLLs have different names for each version of the MCR, multiple versions of the MCR can be installed on the same machine.
Hi geomorillo, I followed your instructions and I got an error after I imported the "dotnetbuilder" folder to my unity folder. It says: "Assets/dotnetbuilder/Examples/VS10/NET/MagicRemoteExample/NativeAPI/MagicSquareClient/MagicSquareClient.cs(15,9): error CS0101: The namespace `MagicSquareClient' already contains a definition for `MagicSquareClient'" I am using Matlab 2014a and I am not sure if that's the reason. Can you let me know which version of Matlab you used for this? Thanks a lot!.
Hello tarinee,i met the same problem, DLLNotFoundException:mclmcrrt7_14..cctor().Do you solve the problem?I need your help ,thank you.
I use MATLAB R2015a with mclmcrrt8_5 and I have a problem 'DLLNotFoundException:mclmcrrt8_5..cctor().' too. Actually, It works when I use C#.NET console Application in VS. But not in Unity... Why? In addition, I have already set PATH environment manually. Help me plz, Thank you!
Hi, Just wondering does anybody still have the file that geomorillo shared? (https://dl.dropbox.com/u/6716823/MWArray.dll ) All Dropbox public shared files has became private since 2017. However, the issue with Matlab's MWArray.dll seems still there. Thanks a lot!