Unity在安卓上对于VR的支持

来源:互联网 发布:计算机算法设计难么 编辑:程序博客网 时间:2024/05/22 13:17

设置Android开发环境

  • 安装jdk.
  • 安装android-sdk-windows.(https://docs.unity3d.com/Manual/android-sdksetup.html)
  • Unity已安装Android部分的插件(http://download.unity3d.com/download_unity/3829d7f588f3/TargetSupportInstaller/UnitySetup-Android-Support-for-Editor-5.5.2f1.exe )。选择对应的Unity版本号。

转换到Android:

在File->Bulid Setting 中选择Android选项。然后点击Switch Platform:

图1

设置JDK和android sdk的开发路径

在Edit->Preferences->External Tools中设置,如下图:

图2

PlayerSetting的设置

Company Name: 公司名,与Bundle名相同,最好改掉。
Product Name: 产品名。
Orientation: 屏幕的自适应设置。
Bundle Identifier: 必须是独一无二的。
设置参考如下:

图3

图4

Android上使用的实例

  • 新建一个cube.
  • 新建一个脚本Rotate。在脚本上做如下更改:
void Update () {        transform.Rotate(Vector3.up * Time.deltaTime * 10);        QuitApp();} private void OnMouseDown() {     GetComponent<Renderer>().material.color = Color.red;}void QuitApp(){     if (Input.GetKeyDown(KeyCode.Escape))      {         Application.Quit();      } }
  • QuitApp用于退出程序
  • 导出程序,我们就可以在手机中看到cube运行的画面了,点击cube后,cube就会变成红色。

下面我们来看看更多的针对于手机的操作:
- 在脚本Rotate上继续写下如下的函数:

void TouchTest1()    {        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)        {            // Get movement of the finger since last frame            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;            // Move object across XY plane            transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);        }    }    void TouchTest2()    {        for (int i = 0; i < Input.touchCount; ++i)        {            if (Input.GetTouch(i).phase == TouchPhase.Began)                clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;        }    }    void AccTest()    {        Vector3 dir = Vector3.zero;        dir.x = -Input.acceleration.y;        dir.z = Input.acceleration.x;        if (dir.sqrMagnitude > 1)            dir.Normalize();        dir *= Time.deltaTime;        transform.Translate(dir * speed);    }
  • TouchTest1可以用于用手指控制cube的移动
    TouchTest2可以根据手指触碰的数目实例化gameobject.
    AccTest可以用于重力感应。

安卓全景图片

  • 这里我们使用暴风魔镜VR,导入其安装包。
  • 将暴风魔镜的下的MojingMain导入到场景中。
  • 将demo下的qiu放入到场景中。
  • 将全景图拖入qiu上,把shader改为Unlit/texture。
  • 导出场景,即可在手机上观看全景图。
    全景视频的播放。
  • 首先,我们导入Easy Moive Texture的插件。
  • 然后将需要播放的视频放入SteamingAssets中
  • 把Easy Moive Texture中的VideoManager拖入到场景中,把VideoManager上的Mesh Renderer和Quad移除,然后把Media Player Ctr脚本上的Str File Name命名为我们所要播放全景视频的名字。并把qiu拖到Target Material上。
  • 导出运行即可观看全景视频。

用gaze实现全景图片的切换
- 新建一个Image的UI, 把Canavs调整为世界坐标,然后把Image放到全景图片的门的位置,然后把source image换为Knob。命名为ChangeScene
- 然后在新建一个Image的UI,其位置与上一个UI相同,把source image换为Circle, 并将Image Type改为Filled,先把Fill Amout的初始位置设为0.命名为LoadingScene
- 给ChangeScene添加碰撞体。并把其tag改为Door。
- 在ChangeScene上新建脚本DoorMange.cs,其代码如下:

public class DoorManage : MonoBehaviour {    public string gotoScene;    public Image thisLoading;    // Use this for initialization    void Start () {    }    // Update is called once per frame    void Update () {    }}

然后把gotoScene换位另一个要切换的场景的名字。thisloading添加为LoadingScene.
- 然后在MojingMain下面的Main Camera上添加VRControl.cs脚本,其代码如下:

void Update () {        RaycastHit hit;        Ray ray = GetComponent<Camera>().ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));        Debug.DrawRay(ray.origin, 10*ray.direction, Color.red);        if(Physics.Raycast(ray, out hit))        {            if(hit.collider.tag == "Door")            {                DoorManage hitDoor = hit.collider.GetComponent<DoorManage>();                if (hitDoor.thisLoading.fillAmount < 1)                {                    hitDoor.thisLoading.fillAmount = hitDoor.thisLoading.fillAmount + 0.02f;                }                else                {                    Application.LoadLevel(hitDoor.gotoScene);                }            }        }    }

从相机上发射一条射线,当射线触碰到ChangeScne这个UI时,我们把进度条加上一定的数值,当大于1是,我们切换场景。
- 把该场景复制一份。
- 把要切换的连个场景均添加到bulid setting里面。
- 导出即可运行。