hecomi / uWindowCapture

This allows you to use Windows Graphics Capture / PrintWindow / BitBlt in Windows to capture multiple windows individually and easily use them as Texture2D in Unity.
https://tips.hecomi.com/entry/2021/04/30/230322
MIT License
505 stars 81 forks source link

uWindowCapture fails to work on Unity scene change. #34

Open theGaffe opened 3 years ago

theGaffe commented 3 years ago

Hello, I've never written one of these GitHub issues, so hopefully what I'm doing is correct.

uWindowCapture fails its validation if I simply change Unity's scene while in Play. No matter what I do in the settings for the Uwc Window Texture, it does not change anything.

Here is a quick recording showcasing this. Here is the only code used, which just changes the scene:

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SceneManager.LoadScene("WindowTest1");
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SceneManager.LoadScene("WindowTest2");
        }
    }

As you can see, once a scene change has occurred, uWindowCapture simply stops working.

Let me know what can be done, I appreciate your time in reading this.

Using Unity version: 2021.1.4f1.2345.4

theGaffe commented 3 years ago

After some more debugging I found the source of the issue. In the UwcManager.cs file you have:

void OnApplicationQuit()
    {
        Resources.UnloadUnusedAssets();
        Lib.Finalize();
    }

    void OnDisable()
    {
        Lib.SetLogFunc(null);
        Lib.SetErrorFunc(null);
    }

I added the lines from OnApplicationQuit() to OnDisable(), which activates during a scene change:

void OnDisable()
    {
        Lib.SetLogFunc(null);
        Lib.SetErrorFunc(null);
        Resources.UnloadUnusedAssets();
        Lib.Finalize();
    }

And now it works like I want. This is probably a hack that has other issues but at least I can get it working for my purposes for now.

AyumiANDO commented 1 year ago

nice to meet you. This is an issue from a little while ago, but I'd like to comment. This content you wrote helped me. thank you very much.