Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get an Android WebView as a Unity Texture?

Discussion in 'Android' started by jrmgx, Nov 3, 2016.

  1. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    I spent a week on this already, but I'm not an expert so I may miss something, or even want to do something impossible.

    I have a Unity project (with Daydream VR) and my goal is to get a WebView working on a 3D surface (as a texture) in VR mode (for Android) I know it sounds complicated and, I bet it is.

    This is where I am so far:

    First of all I started with the samples from Oculus. They made a plugin that links an Android MediaPlayer (native class from android) to a Unity Texture, this is kind of easy because the media player of android already use SurfaceTexture to render itself, and with the plugin they just created a bridge between the two environments.

    I got this working easily.

    Then after I found this article: http://www.felixjones.co.uk/neo website/Android_View/ and so I made a deduction that it could be possible to use the same trick to get a native Android WebView in a Unity Texture.

    From the article, I thought that the only thing I needed was a custom Android WebView that renders to a SurfaceView (so it will be quite similar to the Android MediaView) and then the oculus plugin would work too.

    Of course nothing was like that...

    Step 1/
    I made an Android plugin for Unity to expose a custom WebView that (IMHO) would render to a SurfaceView (but I'm not sure it is quite right)
    see => JrmgxWebView.java

    Step 2/
    I compiled the oculus plugin, with minor changes
    see => *.cpp

    Step 3/
    I adapted the Unity script that link both the Android plugin and the oculus plugin for unity, added that to a simple Daydream Scene on a 3D cube (with the script associated)
    see => WebviewPlayerSampleWeb.cs

    I get logs, and from what I understand, the problem is that the SurfaceView does not get refreshed. I can see that my Custom WebView is instanciated well, most of the code works and there are no error (nor crash)
    But the texture only change from white (start value) to black (when the plugin is initialized) and then nothing happen. The textureId coming from the plugin get stuck to either 0 or 29, and AndroidSurfaceTexture->GetNanoTimeStamp() never returns new values.

    On the java side I played with .invalidate(), used .loadUrl() and .loadData() and on the C++ part, I checked each method call to see if everything was called. I spend so much time on this code that I can't even remember all the combination I tried.

    What is wrong with this? I'm sure I'm missing a basic point here.

    most code here https://gist.github.com/jrmgx/019cba403769dda27a6d42e48c461b1b

    PS: be nice with me, it's been a week, and it is my first time with Unity, C++, JNI and C#

    Thank you in advance
     
  2. geovana-a

    geovana-a

    Joined:
    Jul 14, 2016
    Posts:
    4
    Hi! Do you have any update? I followed your gist code using original OculusMediaSurface.so library and I made an Android plugin for Unity to expose a custom WebView, but is shown only white texture.
    Thanks!