Unity:将Google Cardboard VR应用程序部署为独立/ Web时无法移动?

时间:2016-05-11 02:28:36

标签: web unity3d google-cardboard virtual-reality

我在Unity中使用Google Cardboard SDK创建了一个VR应用程序,现在正尝试将其部署到Web并作为独立部署。该游戏在iPhone和Android上运行良好,用户通过转动头部并触摸屏幕来导航游戏(我相信“Fire1”命令)。当我切换平台时会出现问题。

在测试编辑器中,即使我的电脑没有绑在我头上,我仍然可以通过按住Alt键转动并只需点击屏幕移动来玩游戏。我认为在部署到独立时会出现这种情况,但我遇到了问题:

当部署到网络和独立时,用户根本无法移动,因此我甚至不确定游戏是否正常工作 - 按住alt键不执行任何操作,单击屏幕不会执行任何操作。他们不能玩游戏。

这里有什么问题?是否有其他输入可以专门用于独立替换头部跟踪等?如何将其他Google Cardboard VR应用程序部署到网络上?

在我的WebplayerDevice脚本中:

//For web support

#if UNITY_WEBPLAYER

using UnityEngine;
using System.Runtime.InteropServices;
using System.Collections.Generic;

public class CardboardWebplayerDevice : BaseCardboardDevice {

    // Simulated neck model.  Vector from the neck pivot point to the point between the eyes.
    private static readonly Vector3 neckOffset = new Vector3(0, 0.075f, 0.08f);

    // Use mouse to emulate head in the editor.
    private float mouseX = 0;
    private float mouseY = 0;
    private float mouseZ = 0;

    public override void Init() {
        Input.gyro.enabled = true;
        Debug.Log ("ITS WEB!!");

    }

    public override bool SupportsNativeDistortionCorrection(List<string> diagnostics) {
        return false;  // No need for diagnostic message.
    }

    public override bool SupportsNativeUILayer(List<string> diagnostics) {
        return false;  // No need for diagnostic message.
    }

    // Since we can check all these settings by asking Cardboard.SDK, no need
    // to keep a separate copy here.
    public override void SetUILayerEnabled(bool enabled) {}
    public override void SetVRModeEnabled(bool enabled) {}
    public override void SetDistortionCorrectionEnabled(bool enabled) {}
    public override void SetStereoScreen(RenderTexture stereoScreen) {}
    public override void SetSettingsButtonEnabled(bool enabled) {}
    public override void SetAlignmentMarkerEnabled(bool enabled) {}
    public override void SetVRBackButtonEnabled(bool enabled) {}
    public override void SetShowVrBackButtonOnlyInVR(bool only) {}
    public override void SetNeckModelScale(float scale) {}
    public override void SetAutoDriftCorrectionEnabled(bool enabled) {}
    public override void SetElectronicDisplayStabilizationEnabled(bool enabled) {}
    public override void SetTapIsTrigger(bool enabled) {}

    private Quaternion initialRotation = Quaternion.identity;

    private bool remoteCommunicating = false;
    private bool RemoteCommunicating {
        get {
            if (!remoteCommunicating) {
remoteCommunicating = Vector3.Dot(Input.gyro.rotationRate, Input.gyro.rotationRate) > 0.05;
            }
            return remoteCommunicating;
        }
    }

    public override void UpdateState() {
        Quaternion rot;
        if (Cardboard.SDK.UseUnityRemoteInput && RemoteCommunicating) {
            var att = Input.gyro.attitude * initialRotation;
            att = new Quaternion(att.x, att.y, -att.z, -att.w);
            rot = Quaternion.Euler(90, 0, 0) * att;
        } else {
            bool rolled = false;
            if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) {
                mouseX += Input.GetAxis("Mouse X") * 5;
                if (mouseX <= -180) {
                    mouseX += 360;
                } else if (mouseX > 180) {
                    mouseX -= 360;
                }
                mouseY -= Input.GetAxis("Mouse Y") * 2.4f;
                mouseY = Mathf.Clamp(mouseY, -85, 85);
            } else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
                rolled = true;
                mouseZ += Input.GetAxis("Mouse X") * 5;
                mouseZ = Mathf.Clamp(mouseZ, -85, 85);
            }
            if (!rolled && Cardboard.SDK.autoUntiltHead) {
                // People don't usually leave their heads tilted to one side for long.
                mouseZ = Mathf.Lerp(mouseZ, 0, Time.deltaTime / (Time.deltaTime + 0.1f));
            }
            rot = Quaternion.Euler(mouseY, mouseX, mouseZ);
        }
        var neck = (rot * neckOffset - neckOffset.y * Vector3.up) * Cardboard.SDK.NeckModelScale;
        headPose.Set(neck, rot);

        triggered = Input.GetMouseButtonDown(0);
        tilted = Input.GetKeyUp(KeyCode.Escape);
    }


    public override void PostRender() {
        // Do nothing.
    }

    public override void UpdateScreenData() {
        Profile = CardboardProfile.GetKnownProfile(Cardboard.SDK.ScreenSize, Cardboard.SDK.DeviceType);
        ComputeEyesFromProfile();
        profileChanged = true;
    }

    public override void Recenter() {
        mouseX = mouseZ = 0;  // Do not reset pitch, which is how it works on the phone.
        if (RemoteCommunicating) {
            //initialRotation = Quaternion.Inverse(Input.gyro.attitude);
        }
    }
}
#endif

错误(即使我将Webplayer设备类更改为BaseVRDevice也没有区别):enter image description here

2 个答案:

答案 0 :(得分:0)

关键组合仅在Unity3d编辑器中可用。

Cardboard Unity3d SDK通过在VRDevices下加载相关设备脚本,支持不同平台的不同行为:

https://github.com/googlesamples/cardboard-unity/blob/master/Cardboard/Scripts/VRDevices

您将看到UnityEditorDevice.cs具有处理ALT和CTRL键的代码。

对于Web部署,我建议您创建一个新设备并实现鼠标移动(没有键)来环顾四周。您可以通过编辑BaseVRDevice.cs来获取Cardboard SDK来加载您的设备:

    public static BaseVRDevice GetDevice() {
        if (device == null) {
    #if UNITY_EDITOR
          device = new UnityEditorDevice();
    #elif ANDROID_DEVICE
          device = new CardboardAndroidDevice();
    #elif IPHONE_DEVICE
          device = new CardboardiOSDevice();
    // Support your device here:
    #elif UNITY_WEBPLAYER
          device = new CardboardWebplayerDevice();
    #else
          throw new InvalidOperationException("Unsupported device.");
    #endif
        }
        return device;
}

答案 1 :(得分:0)

您看到的编译错误是因为您在Cardboard.cs脚本中引用的变量(autoUntiltHead,Screen,Device等)都在#if UNITY_EDITOR部分中。将其更改为#if UNITY_EDITOR || UNITY_WEBPLAYER,或者只删除#if。然后你就可以编译了。