I am hoping someone can help point me in the right direction here. I have a project that does some computation with C dll that is accessed by a C# dll. Its independent of unity. However its useful for some computation inside of unity, so I am trying to integrate it.
The problem is: When using the Unity Editor, pressing Play results in a DLLNotFoundException that is referring to the C dll (not the c# one).
HOWEVER, if you build the project, the DLL does work.
The C# dll works in a visual studio project on its own (nothing to do with Unity). So it doesn’t seem to be a C# or dll problem.
What is the difference between the Editor and pressing play vs. Build and Run, when it comes to DLLs?
Well, we don’t know nothing about your dll yet. Where do you have placed it in your project? What are the import settings? Are you sure the system architecture matches your Unity installation. Keep in mind that you can not use 32 bit dlls inside a 64 bit application or vice versa.
Do you even have the native dll inside your project? You can’t ship your build project without it so it has to be inside your project. It’s possible that a build game may use common system library paths to resolve a DLL and the editor does not. However it’s hard to tell with that little information.
I was talking about the C DLL of course, you’re importing it using [DllImport] right?
What I suggested is this
#if UNITY_EDITOR
// This will run only in Editor mode
[DllImport (@"C:\FullPathToDll\dll_file.dll")]
#else
// This will run in build, using relative path. Make sure the C dll is in the same folder as your .exe
[DllImport ("dll_file.dll")]
#endif
Sorry if it came off that way, I wasn’t mad just asking since I don’t understand how that worked. There was another post that mentioned changing the path of the DLL and showed an image of the Inspector in the Editor, but mine just shows its path and doesn’t let me edit it (so I wasn’t sure if thats what you were saying).
From your last message you are saying I should put this in the “C DLL of course”, but I am guessing from your code snippet you mean the “C # DLL” ?
Anyway, the situation is:
C DLL that is what can’t be found
C# DLL that is the main interface to the C DLL
A unity script that calls the C# DLL < – this works fine.
So the 3rd part is fine, unity and the script editor is finding the C# DLL, but that DLL is not finding the C DLL. So there is no where to put the [DllImport “”] because it was already compiled into a C# DLL and it had nothing to do with Unity.
Right now I put all the DLLs into Assets->Plugins. I also tried putting them in the C:/Programs…/Editor/ directory. I also tried putting them in the root of the project folder. None of them worked, but again, when I build the project, it works fine.
I think I answered most of the missing pieces in my other response, but yes I am using 64-bit release builds of the dlls.
I think the point about how its resolving DLLs is basically what my question is about. I was hoping someone knows whats really going on and why one version works and the other doesn’t.
So you’re not using the C library directly using [DllImport]?
Yes this the problem, but since you’re not interacting directly with the C library (I suppose) then you should make sure the DLL is present along side your C# assembly (you’ll find it in Library\ScriptAssemblies). If you can’t find your C DLL there, try to import it in the Editor like any other asset (Important: inside a “Plugins” folder), this should copy it to output folder. If this doesn’t work too, then I would try to put the DLL in the Unity editor directory (since Play Mode is running from there) or even a known location (Windows/System or /System32…) as a last resort…
The C DLL is being called from a C# DLL (Not a Unity C# Script). The unity c# script is not the same as the c# dll, there are 3 different things. Lets call it “myCinterface.dll” “myCSharpToC.dll” and inside of unity there is “ExampleScript.cs”. ExampleScript.cs is calling “myCSharpToC.dll”, which itself calls “myCinterface.dll”. The unity script doesn’t call the C dll.
The “myCSharpToC.dll” expects the “myCinterface.dll” to be next to it in the same directory. So I don’t understand why there are a million places I can try to put the script and it never works.
Yea so I tried all those places and its not working.
I don’t see anything besides Unity-specific stuff in Library\ScriptAssemblies . I tried manually placing it but Unity just cleans up the folder automatically.
myCinterface.dll should be next to myCSharpToC.dll (both should be in Library\ScriptAssemblies)
For the Editor to copy myCinterface.dll into Library\ScriptAssemblies, you should import it (myCinterface.dll) into Assets/Plugins directory in the Editor. Did you do this? (share screenshots of your Assets folder in Unity Editor, and Library\ScriptAssemblies)
I don’t know why the dlls are not copied… make sure you’re selecting the correct architecture (x86, x64) for both the game and the two dlls. Also try copying the Dlls into the root folder of your project directly (not inside Assets or Assets/Plugins). And as I said, as a last resort, try copying the files into the Unity Editor folder itself (or Windows/System32), as long as it works in build mode, then it’s fine to hack your way through it so you can at least continue working…