TLabAltoh / TLabWebView

Plug-in for WebView that runs in Unity and can display WebView results as Texture2D
https://tlabgames.gitbook.io/tlabwebview/
MIT License
5 stars 4 forks source link

Black screen due to Screen Optimization conflict #6

Closed firdiar-mita closed 3 weeks ago

firdiar-mita commented 1 month ago

image

How to Reproduce

  1. run this code during runtime

        Vector2 ScreenFullResolution = new Vector2(Screen.width, Screen.height);
        int resolution = 80;//80% of original screen
        Vector2 finalRes = ScreenFullResolution * (resolution/ 100f); 
    
        Screen.SetResolution((int)finalRes.x, (int)finalRes.y, true);
  2. Init webview

Desc this can happen because, in the code that I mention above changing value of Screen.Width & Screen.Height. causing initialization of webview to be incorrect.

Note: the code I mention above is often used in Mobile (android/ios), to significantly boost rendering performance by reducing screen resolution to 60-80% of original resolution.

firdiar-mita commented 1 month ago

My solution is to save the screen resolution in OnLoadInitialization , then use it instead of Screen.Height & Screen.Width

image image

private static int ScreenFullResolutionBig;
        private static int ScreenFullResolutionSmall;

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
        private static void PreInitWebview()
        {
            //Getting screen size, befor it's changed
            ScreenFullResolutionBig = Screen.width > Screen.height ? Screen.width : Screen.height;
            ScreenFullResolutionSmall = Screen.width > Screen.height ? Screen.height : Screen.width;
        }

        private static Vector2Int GetOriginalScreenResolution()
        {
            Vector2Int ScreenFullResolution;
            if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown)
            {
                ScreenFullResolution = new Vector2Int(ScreenFullResolutionSmall, ScreenFullResolutionBig);
            }
            else if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
            {
                ScreenFullResolution = new Vector2Int(ScreenFullResolutionBig, ScreenFullResolutionSmall);
            }
            else
            {
                ScreenFullResolution = new Vector2Int(ScreenFullResolutionBig, ScreenFullResolutionSmall);
            }

            return ScreenFullResolution;
        }
private IEnumerator InitTask()
        {
#if UNITY_ANDROID && !UNITY_EDITOR || DEBUG
            // I cannot find the way to preload (load on startup)
            // jni shared library. so call library function and
            // load dinamically here. (call unity plugin on load)
            if (SystemInfo.renderingThreadingMode == UnityEngine.Rendering.RenderingThreadingMode.MultiThreaded)
            {
                GL.IssuePluginEvent(NativePlugin.DummyRenderEventFunc(), 0);
            }
            else
            {
                NativePlugin.DummyRenderEvent(0);
            }
#endif

            m_state = State.INITIALISING;
            ongoingLoadUrl = m_url;

            yield return new WaitForEndOfFrame();

#if UNITY_ANDROID && !UNITY_EDITOR || DEBUG

            m_NativePlugin = new AndroidJavaObject("com.tlab.libwebview.UnityConnect"); 

            yield return null;

            m_rawObject = m_NativePlugin.GetRawObject();
            m_rawImage.texture = new Texture2D(m_texWidth, m_texHeight, TextureFormat.ARGB32, false, false);
            m_rawImage.texture.name = "Webview-"+gameObject.name; 
            m_prevTexID = m_rawImage.texture.GetNativeTexturePtr();

            if (m_NativePlugin != null)
            {
                SetDownloadOption(m_dlOption);
                SetDownloadSubDir(m_dlSubDir);

                SetOnPageFinish(m_jsEventCallback.onPageFinish);
                SetOnDownloadStart(m_jsEventCallback.dlEvent.onStart);
                SetOnDownloadFinish(m_jsEventCallback.dlEvent.onFinish);

                SetOnCatchDownloadUrl(
                    m_jsEventCallback.catchDlUrlEvent.go,
                    m_jsEventCallback.catchDlUrlEvent.func);

                SetDownloadEventVariableName(
                    m_jsEventCallback.dlEvent.varDlUrlName,
                    m_jsEventCallback.dlEvent.varDlUriName,
                    m_jsEventCallback.dlEvent.varDlIdName);

                var isVulkan = (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan);

                m_updateFrameFunc = isVulkan ? UpdateVulkanFrame : UpdateGLESFrame;

                var screenResolution = GetOriginalScreenResolution();
                int s_width = screenResolution.x;
                int s_height = screenResolution.y;

                //Debug.Log($"{s_width} x {s_height} , Vulkan: {(isVulkan)}");
                m_NativePlugin.Call("initialize",
                    m_webWidth, m_webHeight,
                    m_texWidth, m_texHeight,
                    s_width, s_height, m_url, isVulkan);
            }

            while (!IsInitialized())
            {
                yield return null; // use null instead of 'WaitForEndOfFrame()' to reduce producing garbage
            }

            m_state = State.INITIALIZED;
#endif
        }
firdiar-mita commented 1 month ago

sorry i didn't fork this repo, hope it helps