mcpiroman / UnityNativeTool

Allows to unload native plugins in Unity3d editor
MIT License
183 stars 18 forks source link

dll cant be deleted in unity2019.3.1 #9

Closed zhmt closed 4 years ago

zhmt commented 4 years ago

The UnityNativeTool has been installed. The path of dll is : Assets/Plugins/__libezg_Debug.dll. DllManipulatorScript has beed attached to a gameobject. Run game, native function works fine(just a add method that return int). stop game. DLL cant be deleted now. It complains that the dll has been opened by unity editor.

DllManipulatorScript::OnApplicationQuit has been called. Did I miss something?

mcpiroman commented 4 years ago

Yeah this sounds like it should work. Most probably problem lies somewhere in names and paths.

In case you have a dll at path Assets/Plugins/__libezg_Debug.dll as you mentioned, your [DllImport]s should use libezg_Debug or libezg_Debug.dll, and not e.g. __libezg_Debug.dll.

Just for testing, you could also make sure there aren't any other custom DLLs in that folder (especially that there is no Assets/Plugins/libezg_Debug.dll). Generally this is fine but might be confusing to see which DLL does Unity actually use.

If that doesn't help, then please give me more info about your setup.

zhmt commented 4 years ago

Thank you for your reply.

I ame using "libezg_Debug" in coding.

    [DllImport("libezg_Debug")]
    public static extern long ezgnow();

There is only one dll "__libezg_Debug.dll" in "Assets/Plugins" Unity uses __libezg_Debug.dll actually.

zhmt commented 4 years ago

Here is the coding using my dll:

public class ShowFps : MonoBehaviour
{
    private float deltaTime;
    // Start is called before the first frame update
    private Text fpsText;

    void Start()
    {
        fpsText = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(NativeApi.ezgnow());
    }
}

It is a hello world project , no more code.

mcpiroman commented 4 years ago

Ok, so that's rather strange. In theory it might be that you have used this plugin before using my tool, in which case Unity already loaded it by itself. So if that's the case, you may simply restart Unity once more and it should start working.

Otherwise, could you please ensure that DllManipulator.SysUnloadDll() is being called? And, of course, if there aren't any errors in the Unity's console.

zhmt commented 4 years ago

Unity has been restarted. SysUnloadDll has been called successfully. No warnings found in console.

zhmt commented 4 years ago

I have tried with "LoadLibrary" and "FreeLibrary" before UnityNativeTool. If LoadLibrary on Awake, then do nothing, and FreeLibrary On OnApplicationQuit , dll can be deleted. But if I GetProcAddress and call NativeApi.ezgnow(), then dll can't be deleted.

mcpiroman commented 4 years ago

Oh, maybe Update is called even after OnApplicationQuit for some reason. Try to change it to OnDestroy (just like in the latest version of this project, I just haven't updated the releases yet). Otherwise, I have little clue of why this is not working. Also, try to set loading mode from Lazy to Preload and see what happens. It should be more sensitive to eventual errors.

zhmt commented 4 years ago

I restarted Unity ,set loading mode Preload , with all native functions checked, only executing assembly checked, only in editor checked, and tried with:

public class ShowFps : MonoBehaviour
{
    void Start()
    {
        Debug.Log(NativeApi.ezgnow());
    }

    void Update()
    {
    }

    void OnApplicationQuit()
    {
    }
}

Still no luck. It is weird.

mcpiroman commented 4 years ago

Have you changed OnApplicationQuit to OnDestroy in DllManipulatorScript? And please add some debug logs in Update and OnApplicationQuit to see if maybe there is some Update called after it.

mcpiroman commented 4 years ago

One other thing you may do is setup debug logs in both DllManipulator.SysLoadDll and DllManipulator.SysUnloadDll(). It might be that it tries to load DLL for the second time. Ultimately, there should be only one load and one unload.

zhmt commented 4 years ago

I changed OnApplicationQuit to OnDestroy , and restart unity, and here is the logs:

1581956685278
UnityEngine.Debug:Log(Object)
ShowFps:Update() (at Assets/ShowFps.cs:18)
DllManipulatorScript.OnApplicationQuit called.
UnityEngine.Debug:Log(Object)
UnityNativeTool.DllManipulatorScript:OnApplicationQuit() (at Assets/UnityNativeTool/DllManipulatorScript.cs:101)
SysUnloadDll has beed call.
UnityEngine.Debug:Log(Object)
UnityNativeTool.Internal.DllManipulator:UnloadAll() (at Assets/UnityNativeTool/DllManipulator.cs:83)
UnityNativeTool.DllManipulatorScript:OnDestroy() (at Assets/UnityNativeTool/DllManipulatorScript.cs:94)
DllManipulatorScript.OnDestroy called.
UnityEngine.Debug:Log(Object)
UnityNativeTool.DllManipulatorScript:OnDestroy() (at Assets/UnityNativeTool/DllManipulatorScript.cs:97)
mcpiroman commented 4 years ago

May you please add log entry in DllManipulator.SysLoadDll? Or was it there already? I'm also going to install Unity 2019.3.1f1 to see if this 'works on my machine' as it does on previous versions.

zhmt commented 4 years ago
SysLoadDll E:/gitroot/g4/g4client/Assets/Plugins/__libezg_Debug.dll
SysUnloadDll 

They are all printed once.

zhmt commented 4 years ago

Maybe unity changed dll loading mechanismin in 2019.3.1f1.

mcpiroman commented 4 years ago

Well, it might be that Unity now loads the DLL even tough it's not going to use it. If you comment out the line Debug.Log(NativeApi.ezgnow()); (where you actually use your plugin), and maybe even disable UnityNativeTool (like just disable the game object), does it still lock your dll file?

zhmt commented 4 years ago

If I comment "Debug.Log(NativeApi.ezgnow());" , dll can be deleted no matter UnityNativeTool is enabled or not. I tried both.

mcpiroman commented 4 years ago

"It works on my machine". This is, Unity 2019.3.1, Windows 10. I can normally delete the plugin after I end the game. So there has to be something more with your setup, though I don't see what could it be. Are you sure your plugin doesn't happen to have "Load on startup" in the editor?

zhmt commented 4 years ago

No, "Load on startup" is unchecked. Let me try with a brand new project.

zhmt commented 4 years ago

Thank you for help. It is too late today, I will download from unity offical website and try to reinstall tommorow.

mcpiroman commented 4 years ago

Just also updated the release to the newest version, although I doubt it's gonna help. An working example project could be a nice thing for this repo though.

zhmt commented 4 years ago

I downloaded unity2019.3.1 and reinstalled , still no luck. I am confused, seems nothing wrong, just dosen't work. I will appreciate that if you upload a working example scene. We can make quick test if it works.

zhmt commented 4 years ago

I find a small bug about configuration. After set loading mode to preload, the confguration is not saved. Loading mode will be reset to Lazy when unity is restarted.

zhmt commented 4 years ago

I also tried this one : https://github.com/forrestthewoods/fts_unity_native_plugin_reloader It has an example project, it doesn't work for me either.

zhmt commented 4 years ago

It works with 2019.3.12f1 now.

mcpiroman commented 4 years ago

Nice to hear that although I'm still convicted that it had something to do with your platform/toolchain/whatever rather than this project or Unity itself.