Search Unity

use dll for unity3d

Discussion in 'Scripting' started by Fantasy.Gamer, Apr 20, 2012.

  1. Fantasy.Gamer

    Fantasy.Gamer

    Joined:
    Dec 2, 2009
    Posts:
    50
    hi
    can i use dll(create by matlab)on unity3d?
    thanks
     
  2. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
  3. Fantasy.Gamer

    Fantasy.Gamer

    Joined:
    Dec 2, 2009
    Posts:
    50
    thanks Nems.
    but i don't get my answer yet.
    my mean was matlab but you answer me for c# or C++.
     
  4. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
    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.
     
  5. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    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.
     
  6. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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
     
    Last edited: Aug 9, 2012
  7. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    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.
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  9. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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.
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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)
     
    Last edited: Aug 10, 2012
  11. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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
     
    Last edited: Aug 11, 2012
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  13. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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
     
    Last edited: Aug 13, 2012
  14. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    awesome, please post the steps!
     
  15. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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
     
    Last edited: Aug 16, 2012
  16. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    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
     
  17. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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
     
    Last edited: Dec 12, 2012
  18. tarinee

    tarinee

    Joined:
    May 9, 2013
    Posts:
    2
    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.
     
  19. geomorillo

    geomorillo

    Joined:
    Jul 31, 2012
    Posts:
    12
    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.
     
  20. tarinee

    tarinee

    Joined:
    May 9, 2013
    Posts:
    2
    Hi geomorillo,
    mcr is already installed. I will try to set path environment variable .
    Thanks.
     
  21. bluesyhfang

    bluesyhfang

    Joined:
    Apr 4, 2014
    Posts:
    8
    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!.
     
  22. duanyun29

    duanyun29

    Joined:
    Jan 30, 2015
    Posts:
    1
    Hello tarinee,i met the same problem, DLLNotFoundException:mclmcrrt7_14..cctor().Do you solve the problem?I need your help ,thank you.
     
  23. Kawintiranon

    Kawintiranon

    Joined:
    Dec 19, 2015
    Posts:
    1
    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!
     
    Last edited: Dec 19, 2015
  24. TurquoiseDot

    TurquoiseDot

    Joined:
    Feb 23, 2015
    Posts:
    1
    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!