gree / unity-webview

zlib License
2.3k stars 694 forks source link

the sample web view is slow #118

Open melo1523 opened 8 years ago

melo1523 commented 8 years ago

when the web view popup, it is very slow to scroll, do you know why?

gusseleet commented 8 years ago

Are you using Android? If so, did you read the README? I had the same problem before I read it.

melo1523 commented 8 years ago

i use mac os version, i found it is because guitext is slow in unity, but i don't know how to fix it

KojiNakamaru commented 8 years ago

The OSX version renders the WebView content as an offscreen image and converts it to a texture. It is costly and leads such a slow performance (though it depends on the content and your Mac performance).

melo1523 commented 8 years ago

so is there any way to make it fast? even change the source code?

KojiNakamaru commented 8 years ago

If you use Mac with a retina display, this plugin renders an image in retina resolution. By setting "Open in Low Resolution" fo Unity.app, every rendering is done in low resolution and this boosts the plugin's rendering speed.

cf. https://support.apple.com/en-us/HT202471

Nesh108 commented 7 years ago

@KojiNakamaru I am doing some profiling of the webview and I noticed that most of the time is spent here: _CWebViewPlugin_Update, which is within a plugin.

Is there any way to tweak this part without having to set the whole app to Low Resolution? The goal is to have a smooth webview without losing quality on the rest of the app. Is that possible?

Nesh108 commented 7 years ago

@KojiNakamaru if that function could have a setting for the refresh rate, it would work wonders. I tried using a very dumb framerate "decreaser" and it made the whole page way smoother. The only problem with my implementation is that GetButtonDown/Up are tied to the last frame.

So, if the webview could still listen to all the events but update the texture only every X frames, that would increase the control a developer has on the webview. It shouldn't be too complicated to implement, right? 😄

KojiNakamaru commented 7 years ago

I've added bitmapRefreshCycle in #215. Please check the latest.

hitechcode commented 6 years ago

Hello, I'm trying Mac webview with unity 2018.2.10f1 and it gives very poor performance on Retina display. as you said before, I've tried setting "Open in Low Resolution" and got better performance but as I don't want to do that, I tried some improvement in webviewobject.cs by replacing _CWebViewPlugin_Update to update function.this gives better performance even in Retina display.

`void Update()
    {
        if (hasFocus)
        {
            inputString += Input.inputString;
        }
    for (; ; )
    {
        if (webView == IntPtr.Zero)
            break;
        string s = _CWebViewPlugin_GetMessage(webView);
        if (s == null)
            break;
        switch (s[0])
        {
            case 'E':
                CallOnError(s.Substring(1));
                break;
            case 'S':
                CallOnStarted(s.Substring(1));
                break;
            case 'L':
                CallOnLoaded(s.Substring(1));
                break;
            case 'J':
                CallFromJS(s.Substring(1));
                break;
            case 'V':
                EvaluateJS(s.Substring(1));
                break;
        }
    }

    if (webView == IntPtr.Zero || !visibility)
        return;

    isViewing = true;
    Vector3 pos = Input.mousePosition;
    bool down = Input.GetButton("Fire1");
    bool press = Input.GetButtonDown("Fire1");
    bool release = Input.GetButtonUp("Fire1");
    float deltaY = Input.GetAxis("Mouse ScrollWheel");
    bool keyPress = false;
    string keyChars = "";
    short keyCode = 0;

    if (inputString != null && inputString.Length > 0)
    {
        keyPress = true;
        keyChars = inputString.Substring(0, 1);
        keyCode = (short)inputString[0];
        inputString = inputString.Substring(1);
    }

    refreshBitmap = (Time.frameCount % bitmapRefreshCycle == 0);
    _CWebViewPlugin_Update(webView,
        (int)(pos.x - rect.x), (int)(pos.y - rect.y), deltaY,
        down, press, release, keyPress, keyCode, keyChars,
        refreshBitmap);

    if (refreshBitmap)
    {
        {
            var w = _CWebViewPlugin_BitmapWidth(webView);
            var h = _CWebViewPlugin_BitmapHeight(webView);
            if (texture == null || texture.width != w || texture.height != h)
            {
                texture = new Texture2D(w, h, TextureFormat.ARGB32, false, true);
                texture.filterMode = FilterMode.Bilinear;
                texture.wrapMode = TextureWrapMode.Clamp;
            }
        }

        _CWebViewPlugin_SetTextureId(webView, (int)texture.GetNativeTexturePtr());
        _CWebViewPlugin_SetCurrentInstance(webView);
    }
}

bool refreshBitmap = false;
public int bitmapRefreshCycle = 1;
void OnGUI()
{
    if (webView == IntPtr.Zero || !visibility)
        return;

    if (refreshBitmap)
    {
#if UNITY_4_6 || UNITY_5_0 || UNITY_5_1
            GL.IssuePluginEvent(-1);
#else
            GL.IssuePluginEvent(GetRenderEventFunc(), -1);
#endif
        }
        if (texture != null)
        {
            Matrix4x4 m = GUI.matrix;
            GUI.matrix
                = Matrix4x4.TRS(
                    new Vector3(0, Screen.height, 0),
                    Quaternion.identity,
                    new Vector3(1, -1, 1));
            GUI.DrawTexture(rect, texture);
            GUI.matrix = m;
            isViewing = false;
        }
    }
#endif` 

may be you need to do some adjustments with above code just for your reference

thank you

KojiNakamaru commented 6 years ago

Thank you. I tried your code and measured its performance by profiler, and could not find any major difference, at least for my environment (MacBook Pro (Retina, 13-inch, Mid 2014), High Sierra, Unity 2018.2.6f1). I might miss something when importing the above code. If possible, could you please provide a pull request?