1、导入360全景图,去掉Generate Mip Maps的勾选,防止出现接缝线。
2、新建Sphere样式的空对象,坐标与Camera相同
3、Camera的Clear Flags 设置为:Solid Color
4、新建材质球 Shader 选择 Skybox/Panormic
5、把全景附给材质球,再把材质球托给Sphere的空对象
6、相机做一个自旋转,就可以看效果了。
查看代码
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GyroController: MonoBehaviour
{
public float moveSpeed = 1;//物体旋转速度
public GameObject target;
private Vector2 oldPosition;
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private float distance = 0;
private bool flag = false;
//摄像头的位置
private float x = 0f;
private float y = 0f;
//左右滑动移动速度
public float xSpeed = 250f;
public float ySpeed = 120f;
//缩放限制系数
public float yMinLimit = -360;
public float yMaxLimit = 360;
//是否旋转
private bool isRotate = true;
//计数器
private float count = 0;
//初始化游戏信息设置
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
// Update is called once per frame
void Update()
{
if (isRotate)
{
target.transform.Rotate(Vector3.down, Time.deltaTime * moveSpeed, Space.World);
}
if (!isRotate)
{
count += Time.deltaTime;
if (count > 5)
{
count = 0;
isRotate = true;
}
}
//触摸类型为移动触摸
if (Input.GetMouseButton(0))
{
//根据触摸点计算X与Y位置
x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
isRotate = false;
}
//判断鼠标滑轮是否输入
float temp = Input.GetAxis("Mouse ScrollWheel");
if (temp != 0)
{
if (temp > 0)
{
// 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
if (distance > -15)
{
distance -= 0.5f;
}
}
if (temp < 0)
{
// 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
if (distance < 20)
{
distance += 0.5f;
}
}
}
}
//计算距离,判断放大还是缩小。放大返回true,缩小返回false
bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//old distance
float oldDistance = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
//new distance
float newDistance = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (oldDistance < newDistance)
{
//zoom+
return true;
}
else
{
//zoom-
return false;
}
}
//每帧执行,在Update后
void LateUpdate()
{
if (target)
{
//重置摄像机的位置
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
}
float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}