Reputation: 1090
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.
Upvotes: 0
Views: 2488
Reputation: 447
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
Upvotes: 1