Unity全景图

来源:互联网 发布:python中文注释 编辑:程序博客网 时间:2024/05/21 17:14

目的:

  • 1:在Unity中浏览全景图
  • 2:利用移动端陀螺仪浏览全景图
  • 3:在移动端没有陀螺仪情况下通过手指滑动浏览全景图

在Unity中如何创建全景图:

  1. 在Untiy中导入一个球体,用作承载全景图,将球体的位置设置为坐标原点;
  2. 然后在Unit中创建一个Camera,作为查看全景图的视觉;
  3. 最后为方便管理,为球体及Camera创建一个父物体panorama;
如下所示:




基本效果如下所示:

移动端处理:

代码及注释如下所示
using UnityEngine;using System.Collections;/// <summary>///  Made by Bruce/// </summary>public class Gyro : MonoBehaviour{    Quaternion quatMult;    protected void Start()    {        if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)            this.enabled = false;        //设置设备陀螺仪的开启/关闭状态,使用陀螺仪功能必须设置为 true,        Input.gyro.enabled = true;        //判断移动设备是否有陀螺仪————亲自测试OPPO R9手机并没有陀螺仪        if (Input.gyro.enabled)        {            //获取设备重力加速度向量            Vector3 deviceGravity = Input.gyro.gravity;            //设备的旋转速度,返回结果为x,y,z轴的旋转速度,单位为(弧度/秒)            Vector3 rotationVelocity = Input.gyro.rotationRate;            //获取更加精确的旋转            Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;            //设置陀螺仪的更新检索时间,即隔 0.1秒更新一次            Input.gyro.updateInterval = 0.1f;            //获取移除重力加速度后设备的加速度            Vector3 acceleration = Input.gyro.userAcceleration;            quatMult = new Quaternion(0, 0, 1, 0);        }        else        {            //提示用户使用手指滑动查看全景图——Toast.ShowText("请单指滑动查看全景图", 5);        }    }    protected void Update()    {        //如果可以使用陀螺仪        if (Input.gyro.enabled)        {            transform.rotation = Input.gyro.attitude * quatMult;        }        else        {            #region  单点触发旋转(真实模型旋转)            if (Input.touchCount == 1)            {                //触摸为移动类型                if (Input.GetTouch(0).phase == TouchPhase.Moved)                {                    float XX = Input.GetAxis("Mouse X");                    float YY = Input.GetAxis("Mouse Y");                    #region                    if (Mathf.Abs(XX) >= Mathf.Abs(YY))                    {                        if (XX < 0)                        {                            transform.Rotate(Vector3.up, 60 * Time.deltaTime, Space.Self);                        }                        if (XX > 0)                        {                            transform.Rotate(-Vector3.up, 60 * Time.deltaTime, Space.Self);                        }                    }                    else                    {                        if (YY < 0)                        {                            transform.Rotate(Vector3.left, 60 * Time.deltaTime, Space.Self);                        }                        if (YY > 0)                        {                            transform.Rotate(-Vector3.left, 60 * Time.deltaTime, Space.Self);                        }                    }                    #endregion                }            }            #endregion        }    }    void OnDestory()    {        Input.gyro.enabled = false;    }}


注:

以上代码处理了存在陀螺仪情况已经设备没有陀螺仪的情况(没有的情况下,使用的是单指滑动屏幕查看全景图,具体的查看方式可以自己写。)

0 0
原创粉丝点击