美文网首页
元宇宙,我来了(VR室内漫游)

元宇宙,我来了(VR室内漫游)

作者: 安宇辛 | 来源:发表于2021-09-10 15:07 被阅读0次

    一:准备工作
    1.unity :2018.4.36f1
    2.插件:
    场景:ArchVizPRO Interior Vol6


    ArchVizPRO Interior Vol6

    通用包:Standard Assets (for Unity 2018.4)


    Standard Assets (for Unity 2018.4) image.png

    小米VR插件:MIVR_Unity_SDK_1.8.7(目前已没有没有更新)
    下载地址:https://dev.mi.com/console/doc/detail?pId=818

    二:将资源全部导入(过程比较漫长请耐心等待)


    导入好了,美美哒

    三:打个包在手机上运行一下
    使用插件Easy Touch 5 Touchscreen Virtual Controls实现基本的移动和转向
    可以参考我的另一篇文章https://www.jianshu.com/p/7e5d328aa19c

    走道
    室内

    四:导入小米sdk
    1.报错

    Assets\MIVR\Editor\BuildScripts.cs(30,28): error CS0029: Cannot implicitly convert type 'UnityEditor.Build.Reporting.BuildReport' to 'string'
    
    解决
     string error = BuildPipeline.BuildPlayer(scenes, "sample.apk", BuildTarget.Android, BuildOptions.None).ToString();
    

    五:添加VR相机


    VR

    六:实现VR手柄的控制移动旋转

    using System.Collections;
    using System.Collections.Generic;
    using MIVR;
    using UnityEngine;
    
    public class player : MonoBehaviour
    {
        float moveSpeed = 0.5f;
        public bool isVR = false;
    
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            if (isVR)
            {
                //InputManager.ControllerState.IsTouching  是否是触摸状态
                if (InputManager.ControllerState.IsTouching)
                {
                    // TouchPosition.x 表示水平方向,最左为 0,最右为 1。TouchPosition.y 表示竖直 ,最上为 0,最下为 1
                    if (InputManager.ControllerState.TouchPosition.y <= 0.25 && InputManager.ControllerState.TouchPosition.y > 0)
                    {
                        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
                    }
                    if (InputManager.ControllerState.TouchPosition.y >= 0.75)
                    {
                        transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
                    }
                    if (InputManager.ControllerState.TouchPosition.x <= 0.25 && InputManager.ControllerState.TouchPosition.x > 0)
                    {
                        transform.Rotate(0, -10 * Time.deltaTime, 0, Space.Self);
                    }
                    if (InputManager.ControllerState.TouchPosition.x >= 0.75)
                    {
                        transform.Rotate(0, 10 * Time.deltaTime, 0, Space.Self);
                    }
                }
            }
            else
            {
                if (Input.GetKey(KeyCode.W))
                {
                    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.S))
                {
                    transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.A))
                {
                    transform.Rotate(0, -25 * Time.deltaTime, 0, Space.Self);
                }
                if (Input.GetKey(KeyCode.D))
                {
                    transform.Rotate(0, 25 * Time.deltaTime, 0, Space.Self);
                }
            }
            
    
        }
    }
    
    

    七:打包
    1.添加签名文件到
    Project/Assets/Plugins/Android/assets/MiVR_sig/
    2.修改发布文件
    AndroidManifest
    把<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="22" />去掉

    把最低发布选19或者19以上


    最低发布

    3.添加动态权限android7.0开始敏感权限需要动态请求

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Android;
    
    public class main : MonoBehaviour
    {
        public bool isVR = false;
    
        void Start()
        {
          
            if (isVR) {
               //动态获取android权限
                if (!Permission.HasUserAuthorizedPermission("android.permission.WRITE_EXTERNAL_STORAGE"))
                {
                    Permission.RequestUserPermission("android.permission.WRITE_EXTERNAL_STORAGE");
    
                }
            }
            else
            {
                 Cursor.visible = true;//鼠标显示
                //Cursor.visible = false;//鼠标隐藏
            }
    
           
    
        }
    
        // Update is called once per frame
        void Update()
        {
            //返回键退出游戏
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            } 
        }
    }
    
    

    4打包报错

    cam.hdr = VrManager.Instance.IsHdrEnabled;
    改成
    cam.allowHDR = VrManager.Instance.IsHdrEnabled;
    

    5.安装包到小米VR一体机

    相关文章

      网友评论

          本文标题:元宇宙,我来了(VR室内漫游)

          本文链接:https://www.haomeiwen.com/subject/ptmwmltx.html