Unity中Oculus分屏相机和普通相机一键切换

Unity中Oculus分屏相机和普通相机一键切换

一、OCulus 分屏相机介绍

VR开发工程中,总会觉得OC分屏的处理太慢,严重浪费时间啊! 
但是不使用有不好调试,来回切换相机就成为了一个必须。

近来常用,所以就写了一个小功能来实现它。用tab来实现OC分屏相机和普通相机的切换。 
说明:过于频繁切换,会导致Unity崩溃,所以限制了2秒的最短切换频率。

二、具体怎么实现呢?

首先, 
新建一个工程吧。我使用的Unity版本为5.1.1f,64位版本。当然32位也是可以的。 
然后, 
导入了2013插件,导入了OC的0.4.4版本,使用顺手了。要是使用Unity5.3版本,并且不需要对OC相机进行控制处理的,只需所要缩放renderScale等功能的,5.3版本已经集成了,更多请参考 蛮牛的Untiy5.3 翻译

网址:http://www.manew.com/thread-45174-1-1.html?_dsign=ad0f20e3

最后, 
都导入以后,如下图: 
Unity中Oculus分屏相机和普通相机一键切换

三、相机

创建了一个相机的预制体,包括一个正常的相机,一个是OC的分屏相机。名字为Player。 
如下图:

Unity中Oculus分屏相机和普通相机一键切换

四、代码:

代码很简单,就是切换显示而已: 
如下:

using UnityEngine;
using System.Collections; namespace SLQJ_VR
{
public class ShiftCamera : MonoBehaviour
{
[Header("相机类型")]
public CameraType cameraType;
[Header("旋转速度")]
public float Speed = 100;
public KeyCode ShiftKey = KeyCode.Tab; private float shiftStartTime;
private float shiftDelayTime = 2.0f; private Transform XRotateObject;
private Transform YRotateObject;
private string nomalCamera = "NormalCamera";
private string stereoCamera = "OVRCameraRig";
private GameObject mCurrentCamera = null; void Awake()
{
shiftStartTime = Time.time;
XRotateObject = this.transform;
CameraChoose();
} public GameObject GetCurrentCamera()
{
CameraChoose();
return mCurrentCamera;
} // Update is called once per frame
void Update()
{
CameraYaw();
CameraPitch();
//
ShiftCameraUseKey();
ShowLookDirect();
}
/// <summary>
/// 键盘控制相机的旋转。
/// </summary>
private void CameraPitch()
{
if(null != YRotateObject)// Vertical
{
YRotateObject.Rotate(new Vector3(Speed * Time.deltaTime * Input.GetAxis("Vertical"), 0, 0));
}
} private void CameraYaw()
{
if (null != XRotateObject)//Horizontal
{
XRotateObject.Rotate(new Vector3(0, Speed * Time.deltaTime * Input.GetAxis("Horizontal"), 0));
}
}
/// <summary>
/// 使用tab键,来切换相机
/// </summary>
private void ShiftCameraUseKey()
{
if (Input.GetKey(ShiftKey) && Time.time - shiftStartTime >= shiftDelayTime)
{
shiftStartTime = Time.time;
cameraType++;
cameraType = (CameraType)((int)cameraType % (int)CameraType.Size);
CameraChoose();
}
} /// <summary>
/// 在编辑器模式下,显示条方向线
/// </summary>
private void ShowLookDirect()
{ #if !UNITY_ANDROID || UNITY_EDITOR
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
Vector3 StartPt = mCurrentCamera.transform.position;
Vector3 TmpDirector = mCurrentCamera.transform.forward;
Debug.DrawRay(StartPt, TmpDirector * 1000);
#endif
}
/// <summary>
/// 相机选择
/// </summary>
void CameraChoose()
{
if (CameraType.Normal == cameraType)
{
YRotateObject = transform.FindChild(nomalCamera);
mCurrentCamera = YRotateObject.gameObject;
mCurrentCamera.SetActive(true);
transform.FindChild(stereoCamera).gameObject.SetActive(false);
}
else
{
YRotateObject = transform.FindChild(stereoCamera);
mCurrentCamera = YRotateObject.gameObject;
mCurrentCamera.SetActive(true);
transform.FindChild(nomalCamera).gameObject.SetActive(false);
}
}
} public enum CameraType
{
/// <summary>
/// 正常相机
/// </summary>
Normal = 0,
/// /// <summary>
/// OC相机
/// </summary>
Stereo = 1,
/// <summary>
/// 总类型个数
/// </summary>
Size = Stereo + 1
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

代码基本都带了注释,不多说了。 
不过,要说的一点,就是代码中,把相机对象的旋转,X方向和Y方向的旋转也就是Pitch和Yaw给分开了,很明显这好处大大的。

五、结论

按下tab 键,不要着急,因为从正常相机切换到双屏渲染,要稍微费些时间。 
当然,切换到正常模式相机还是很迅速的。这个切换时间问题,只能这样。

Unity中Oculus分屏相机和普通相机一键切换

上一篇:CEdit控件[转]


下一篇:【html】【15】特效篇--分页