javascript - 如何在 Three.js 场景中包含 OVRManager?

标签 javascript unity-game-engine three.js virtual-reality oculusgo

我已经创建了几个 Three.js/Javascript 演示应用程序,我正在我的新 Oculus Go 中进行试验。我试图让 Go Controller 在我的应用程序中做一些事情,根据 Oculus 开发人员中心的说法,最好的办法是在我的场景中包含 OVRManager,这样我就可以访问该 API。这听起来不错,但对于所有文档 ( https://developer.oculus.com/documentation/unity/latest/concepts/unity-ovrinput/ ),我看不出如何将 OVRManager 添加到我的场景中!我之前没有使用过 Unity,但根据我在文档中了解到的信息,不应该存在任何兼容性问题(应该存在吗?)

所以我想做的是:

<script src="OVRManager.js or something like that"></script>

然后调用我需要的函数,就像我对 OrbitControls.js 和其他外部依赖项所做的那样。

但对于我的生活而言,Google 搜索只会让我兜兜转转。我看到针对 C++ 和 C# 提出的问题,但这对我没有用。如何让这个 API 在我的 Three.js 场景中工作?我在哪里可以找到它,还有其他方法可以包含它吗?

谢谢!

最佳答案

创建一个统一的 WebGL 构建并将您需要的 API 作为公共(public)方法公开在您附加到游戏对象的 Unity 脚本中。

然后,您应该能够按照 How to call Unity functions from javascript 中的说明进行操作(复制在下方)关于如何从您的 JavaScript 代码中调用这些方法。

如果您使用的是旧版本的 Unity,您也许可以使用 UnityScript,它与 JavaScript 有点相似,可以编写脚本。在撰写本文时,Oculus recommends版本 2017.4.11f1,我认为可能仍支持 UnityScript。

您看到如此少的 UnityScript 信息的一个主要原因是 Unity 已经从 UnityScript 转移到仅支持 C#。

但无论您是用 C# 还是 UnityScript 编写 OVRManager 脚本,Unity 都会让这些方法可以从您的 JavaScript 调用。

Calling Unity scripts functions from JavaScript

Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it is to call methods on GameObjects in your content. If you are making the call from a JavaScript plugin, embedded in your project, you can use the following code:

SendMessage(objectName, methodName, value);

Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object; value can be a string, a number, or can be empty. For example:

SendMessage('MyGameObject', 'MyFunction');
SendMessage('MyGameObject', 'MyFunction', 5);

SendMessage('MyGameObject', 'MyFunction', 'MyString');

If you would like to make a call from the global scope of the embedding page, see the Code Visibility section below.

Code visibility

Starting from Unity 5.6 all the build code is executed in its own scope. This approach makes it possible to embed your game on an arbitrary page without causing conflicts with the embedding page code, as well as makes it possible to embed more than one build on the same page.

If you have all your JavaScript code in the form of .jslib plugins inside your project, then this JavaScript code will run inside the same scope as the compiled build and your code should work pretty much the same way as in previous versions of Unity (for example, the following objects and functions should be directly visible from the JavaScript plugin code: Module, SendMessage, HEAP8, ccall etc.).

However, if you are planning to call the internal JavaScript functions from the global scope of the embedding page, you should always assume that there are multiple builds embedded on the page, so you should explicitly specify which build you are referencing to. For example, if your game has been instantiated as:

var gameInstance = UnityLoader.instantiate("gameContainer", "Build/build.json", {onProgress: UnityProgress});

Then you can send a message to the build using gameInstance.SendMessage(), or access the build Module object using gameInstance.Module.

关于javascript - 如何在 Three.js 场景中包含 OVRManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162597/

相关文章:

javascript - 如何在 Angular 中生成独立的 HTML 页面

Javascript浅拷贝

c# - 显示和隐藏游戏对象的最有效技术?

c# - Unity AdMob 广告未显示

javascript - 盒子几何边

javascript - THREE.js 不使用 .mtl 渲染 .obj - 为 THREE.js 导出文件

javascript - 捕获/处理 502 错误网关错误

javascript - AngularJS + ArcGIS

unity-game-engine - 使用代码更改布局组 childForceExpand

three.js - 使用three.js渲染透明对象的两侧时的工件