Search Unity

UnityPluginLoad not called in native plugin

Discussion in 'Windows Mixed Reality' started by xweix, Oct 13, 2016.

  1. xweix

    xweix

    Joined:
    Sep 12, 2016
    Posts:
    11
    In my native plugin, I created the UnityPluginLoad / UnityPluginUnload function as what described in https://docs.unity3d.com/Manual/NativePluginInterface.html . However, I can't make the
    UnityPluginLoad() be called when using the plugin, although my own functions could be called from Unity C# script without any problem.

    In the C++ code, the UnityPluginLoad function is declared in the same way as the documentation:
    #include "IUnityInterface.h"
    extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
    UnityPluginLoad(IUnityInterfaces* unityInterfaces)​

    I checked the exported names of the DLL, and can find UnityPluginLoad function there. Here is the list of the exported functions:
    _Cleanup@0
    _Initialize@0
    _ReturnIntFunc@0
    _SetLogFunction@4
    _UnityPluginLoad@4
    _UnityPluginUnload@0​

    Among them, Cleanup(), Initialize(), ReturnIntFunc(), SetLogFunction() are all explicitly exported functions, and they all work fine. It seems UnitPluginLoad() is also exported with the same naming convention.

    I'm using Unity 5.4.0f3-FTP. Might it be a Unity version or platform specific problem? Thank you very much!
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,775
  3. xweix

    xweix

    Joined:
    Sep 12, 2016
    Posts:
    11
    I checked the sample and found the problem. It seems a .def file is necessary to prevent VC from mangling the name of UnityPluginLoad. The export name has to be "UnityPluginLoad". "_UnityPluginLoad@4" doesn't be recognized.

    However, it's interesting that the native plugin system is more tolerate on the name mangling of user exported functions.

    Thank you very much for your help!
     
    EyePD likes this.
  4. steven3Dim

    steven3Dim

    Joined:
    Feb 21, 2017
    Posts:
    12
    I thought I had the same problem, but I just made a mistake in my code. I just tested it and it seems that at least in Unity v5.5 it is no longer needed to use a .def file. (Visual Studio 2015). I looked with DependencyWalker and the function names were not mangled. Maybe just using extern "C" fixes this also (because C++ mangles function names).
     
  5. EyePD

    EyePD

    Joined:
    Feb 26, 2016
    Posts:
    49
    I just had this problem with 2017.1.1f1. I hope that the .def file requirements can be be added to the "Low-level Native Plugin Interface" documentation.
     
  6. togura

    togura

    Joined:
    Jul 3, 2012
    Posts:
    1
    UnityRegisterRenderingPlugin() seems to cause UnityPluginLoad() not called.
    If you use multiple plugins and one of them uses UnityRegisterRenderingPlugin() that also affects other plugin's UnityPluginLoad() not called.
    Check if all the plugins use UnityRegisterRenderingPluginV5 instead of
    UnityRegisterRenderingPlugin().
     
  7. binarynate

    binarynate

    Joined:
    May 11, 2017
    Posts:
    4
    I just ran into this issue and followed the steps in this helpful article to resolve it:

    https://medium.com/@bengreenier/building-native-unity-plugins-with-visual-studio-8f470e5af9ca

    Like others have mentioned here, the issue is that the UnityPluginLoad function gets prefixed with an underscore when building for x86 (_UnityPluginLoad) unless you use a .def file. This article I linked to provides an example of a .def file and describes how to set it in Visual Studio.