unity3d - 使用 Unity 构建的 Oculus Go VR 应用程序中的 Web 浏览器

标签 unity3d virtual-reality oculus oculusgo

我目前正在尝试找到一种在 VR 应用程序中使用网络浏览器的方法。基本上目标是打开一个像 https://stackoverflow.com 这样的页面。在面板中并使其可通过 oculus go Controller 滚动。

我已经对插件进行了一些研究,例如 this但它们似乎都不适用于 Oculus Go。

最佳答案

更新:我更新了 repo支持视频,是一个基于 GeckoView 浏览器引擎的功能齐全的 3D 浏览器。它依赖 Oculus 的 OVROverlay 将 Android 插件中生成的帧渲染到 Unity3D 纹理上。

This是我做的一个 repo,希望我们可以实现一个不错的游戏内浏览器。这有点错误/缓慢,但它可以工作(大部分时间)。

它使用 Java 插件,通过覆盖 View 的 Draw 方法将 Android WebView 渲染为 Bitmap,然后将其转换为 png 和将它传递给一个统一的 RawImage。有很多工作要做,所以请随时改进它!

enter image description here

使用方法:

在 repo 中,您可以找到需要导入 Assets/Plugins/Android/和 BrowserView.cs 的插件 (unitylibrary-debug.aar) >UnityThread.cs,您可以使用它来将 Android WebView 转换为 Unity 的 RawImage 可以显示的纹理。适当填写 BrowserView.cs 的公共(public)字段。确保在 Unity 的播放器设置中将 API 级别设置为 25。

代码示例

这里重写 WebViewDraw 方法来创建位图和 PNG,并初始化您需要的变量:

public class BitmapWebView extends WebView{

    private void init(){
        stream = new ByteArrayOutputStream();
        array = new ReadData(new byte[]{});
        bm = Bitmap.createBitmap(outputWindowWidth,
             outputWindowHeight, Bitmap.Config.ARGB_8888);
        bmCanvas = new Canvas(bm);
   }


    @Override
    public void draw( Canvas ){
        // draw onto a new canvas  
        super.draw(bmCanvas);
        bm.compress(Bitmap.CompressFormat.PNG, 100, stream);

        array.Buffer = stream.toByteArray();
        UnityBitmapCallback.onFrameUpdate(array,
            bm.getWidth(),
            bm.getHeight(),
            canGoBack,
            canGoForward );
        stream.reset();

    }
}  
// you need this class to communicate properly with unity
public class ReadData {
    public byte[] Buffer;
    public ReadData(byte[] buffer) {
        Buffer=buffer;
    }
}

然后我们将 png 传递给一个统一的 RawImage。 这是 Unity 接收方:

// class used for the callback with the texture
class AndroidBitmapPluginCallback : AndroidJavaProxy
{
    public AndroidBitmapPluginCallback() : base("com.unityexport.ian.unitylibrary.PluginInterfaceBitmap") { }
    public BrowserView BrowserView;

    public void onFrameUpdate(AndroidJavaObject jo, int width, int height, bool canGoBack, bool canGoForward)
        {
        AndroidJavaObject bufferObject = jo.Get<AndroidJavaObject>("Buffer");
        byte[] bytes = AndroidJNIHelper.ConvertFromJNIArray<byte[]>(bufferObject.GetRawObject());
        if (bytes == null)
            return;
        if (BrowserView != null)
        {
            UnityThread.executeInUpdate(()=> BrowserView.SetTexture(bytes,width,height,canGoBack,canGoForward));
        }
        else
            Debug.Log("TestAndroidPlugin is not set");

    }
 }


public class BrowserView : MonoBehaviour {
// Browser view needs a RawImage component to display webpages


   void Start () {
        _imageTexture2D = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
        _rawImage = gameObject.GetComponent<RawImage>();
        _rawImage.texture = _imageTexture2D;

        #if !UNITY_EDITOR && UNITY_ANDROID
        // Get your Java class and create a new instance
        var tempAjc = new AndroidJavaClass("YOUR_LIBRARY.YOUR_CLASS")
        _ajc = tempAjc.CallStatic<AndroidJavaObject>("CreateInstance"); 
        // send the callback object to java to get frame updates
        AndroidBitmapPluginCallback androidPluginCallback = new AndroidBitmapPluginCallback {BrowserView = this};
        _ajc.Call("SetUnityBitmapCallback", androidPluginCallback);
        #endif

    }
   // Android callback to change our browser view texture
    public void SetTexture( byte[] bytes, int width, int height, bool canGoBack, bool canGoForward)
    {

        if (width != _imageTexture2D.width || height != _imageTexture2D.height)
            _imageTexture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);

        _imageTexture2D.LoadImage(bytes);
        _imageTexture2D.Apply();
        _rawImage.texture = _imageTexture2D;
    }
}

关于unity3d - 使用 Unity 构建的 Oculus Go VR 应用程序中的 Web 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508682/

相关文章:

unity3d - Unity 中缺少 GVRMain 文件

swift - ARKit 和 SceneKit 中的遮挡 Material 或保持着色器

linux - 嵌入式处理器的 Unity 构建

c# - 使用 Unity Video Player 连续无缝播放不同的视频

ios - iOS 构建中未显示 Unity 调试日志

c# - void oncollisionenter 发生了多少次?

c# - 在 Samsung Gear VR 中检测点击

javascript - 在 A 形框架中单击时播放声音

c++ - 无法为 Oculus Rift 创建 OpenGL 交换纹理

c# - 为双边移动统一镜像 Oculus Rift Controller 位置