Hi all, I'm using the DllImport to call some functions defined in my XCode Project (it's an iPhone project but it shouldn't matter...) and everything works great. Today I needed to use call of these function also in another file in my unity project but if I add the same DllImport to that file unity will give me an error when exporting "Diplicate native method found: xxxx. Please check your source carefully." Useless to say that if I don't add the DllImport and just try to call the function my script won't compile (error "The name 'xxxx' does not exist in the current context") Where am I wrong? thank you
yes, both files are in the plugins folder. I need the functions in my c# scripts to be called by normal jscript too (they aren't simple wrappers, I'm performing some operations calling also the extern-c functions) so the problem is that they're both in the plugin folder? I hope there's a workaround...
What do you mean by both files? The file with the DLLImport in (that also contains the additional nicer functions that do the extra work) and a class using the imported functions?
no, basically in my project I have two c# scripts in the plugins folder with the same DllImport (they both need to use the same extern-c function to perform some operations)
you must not have the same dllimport in two functions. having them in one makes them globaly available to the whole project already if the cs file is in the plugins folder
as I said: "if I don't add the DllImport and just try to call the function my script won't compile (error "The name 'xxxx' does not exist in the current context")" the only way is to write a wrap on that function in one file?
I think you missunderstand: DLLImport for a specific C function must be present in one CS file and one CS file only within your plugins folder. you can after that have as many other classes as you want that use these DLLImport functions, but its important that the import happens only in a single place as the import is global for the whole project. I personally though would put the wrapper into the same class as the import as there is no reason to seperate them. They will always be very tightly connected and maintenance will be much easier if they are in the same place
I think the problem here is my bad english let's try with a simple example... file1.cs (inside the plugins folder) Code (csharp): //header blablabla [DllImport ("__Internal")] extern static public void DoSomething(int param); public static void MyFile1Func(int myParam1){ // some stuff here... DoSomething(myParam1 modified in some way); // some other stuff... } //etc. file2.cs (inside the plugins folder) Code (csharp): //header blablabla [DllImport ("__Internal")] extern static public void DoSomething(int param); public static void MyFile2Func(int myParam2){ // some stuff here... DoSomething(myParam2 modified in some way); // some other stuff... } //etc. now if inside the file2 I leave the DllImport I'll get the duplicate error, if I'll remove it I'll get the "doesn't exist in current context" error. My question is: how can i reference the "DoSomething" function in the file2.cs without using DllImport? for now I'm using something like static public void DoSomethingWrap(int param){ DoSomething(param) } inside the first "file1.cs" and calling file1.DoSomethingWrap instead of the original one in "file2.cs" but I'd love to avoid the overhead of a function call every time I need this "feature"
As mentioned, you can not have the same DLLImport declaration twice (in your case for DoSomething on the C side). Having it once is enough, it will exist in the project. You just have to call it appropriately from other classes through file1.DoSomething(...) as it is a function of class file1 What I don't understand is the "first file1.cs" part. There is only one, you can not have the same class multiple times in the project and I would strongly recommend to not mess with partial classes as they are of no use (the partial classes need to be in the same compile step so you can just as well have them in the same file). In your case I'm not even seing a reason why you would have file1 and file2 at all as they both seem to work with the exact same stuff so put them into the same class so you have a single point of binding for the C functions of this type. Double function call: you will always get the double function call (call into wrapper -> call to DLLImport function), thats not avoidable if you want to add something in addition to what is exposed from the C side.
I just didn't know that i could call the function using "file.DoSomething()", thanks this answered my question. that's just a simple example that doesn't have anything to do with my current situation (mine it's "a bit" more complicated), in my case the two function perform two really different operations where the only point in common is the call to an extern function (with multiple parameters, return etc.) anyway everything works great now, thank you!
glad its working now Didn't consider that it could just be related to calling the static imported functions, that was likely a "too trivial" thing as you used it in the example for something else and yeah if the wrapper functions do elementally different stuff I understand that you split it into distinct classes In that case I potentially would make a class with the DLLImport stuff and have the other "usage functionality" distinct unless it makes sense to keep it in the original "import class" (the class with the DLLImport in).
yes that's exactly what I've just done built an "Utility" empty class with all the common DllImport so all the scripts can reference a single class thank you again!