0

I am trying to develop a native plugin for Unity (using Unity 2017.1.0f3 and VS 2015). The target platform for my native plugin is UWP and the plugin is written in C#.

Below are the code for my plugin and my script in Unity.

Plugin code in a Class Library targeting Windows 10 14393

public static class SettingsService
    {
        public static string GetSetting( string key )
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            try
            {
                var stringvalue = localSettings.Values[key] as string;
                return stringvalue;
            }
            catch ( ArgumentNullException en )
            {
                return default( string );
            }
        }

        public static void SaveSetting( string key, string value )
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            localSettings.Values[key] = value;
        }
    }

Unity script code

public class TestNativePlugin : MonoBehaviour {

    [DllImport( "UnityPluginTestUWP", EntryPoint = "GetSetting" )]
    private static extern string GetSetting( string key );

    [DllImport( "UnityPluginTestUWP", EntryPoint = "SaveSetting" )]
    private static extern void SaveSetting( string key, string value );
    // Use this for initialization
    void Start () {    
        SaveSetting( "setting", "oeoeoe" );
        System.Diagnostics.Debug.WriteLine( GetSetting( "setting" ) );
    }    
}

The problem is that in runtime I get an EntryPointNotFoundException with the message : Unable to find an entry point named 'SaveSetting' in DLL UnityPluginTestUWP.dll'

I have read online that this may occur because of mangling of function names by the compiler. But this was only in C++ code. My plugin is written in C#.

Any help to overcome this is welcome.

1
  • Why are you importing DLL how if was writen in C++? directly include your dll in your asset folder and use it. Commented Aug 3, 2017 at 15:03

1 Answer 1

1

What you have in your question is an managed plugin. You only need to use DllImport and extern if you are making a native C++ plugin with another C# plugin that wraps around it. That's not the case here since I don't see any C++ code.

A simple managed plugin from Unity's Doc:

namespace DLLTest {

    public class MyUtilities {

        public int c;

        public void AddValues(int a, int b) {
            c = a + b;  
        }

        public static int GenerateRandom(int min, int max) {
            System.Random rand = new System.Random();
            return rand.Next(min, max);
        }
    }
}

Compile it and put the .dll file in your Unity <project folder>/Assets folder

Usage:

public class Test : MonoBehaviour {

     void Start () {
        MyUtilities utils = new MyUtilities();
        utils.AddValues(2, 3);
        print("2 + 3 = " + utils.c);
     }

     void Update () {
        print(MyUtilities.GenerateRandom(0, 100));
     }
}

If you are going to use any Unity function or API in the managed plugin, you have to also add UnityEngine.dll as reference to your plugin before building it. Simply right click on the Solution Explorer and choose Add Reference then look for UnityEngine.dll in the following folder below:

You can find this on Windows at:

Program Files\Unity\Editor\Data\Managed\UnityEngine.dll

You can find this on Mac OSX at:

Applications/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll
Sign up to request clarification or add additional context in comments.

2 Comments

I think I may have the wrong idea as to what native/managed plugins are. I said that it was a native plugin because it includes code like var localSettings = ApplicationData.Current.LocalSettings; that cannot be compiled by Unity since it is UWP specific. But you are saying that my plugin is still a managed plugin (because it is written in C# a managed language) am I right?
You are welcome. Always try the solution before commenting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.