70 lines
3.1 KiB
C#
70 lines
3.1 KiB
C#
|
||
using UnityEngine;
|
||
using System.Collections;
|
||
public class CameraSetting : MonoBehaviour {
|
||
public int Camertype;
|
||
public int CamerFPS;
|
||
public int CamerWidth;
|
||
public int CamerHeight;
|
||
private WebCamTexture t;
|
||
private WebCamDevice[] mWebCamDevices;
|
||
private bool isDebug;
|
||
private bool isNeedSetCamera = true;
|
||
// Use this for initialization
|
||
void Start () {
|
||
isDebug = gameObject.GetComponentInParent<gyroscope>().isDebug;
|
||
StartCoroutine (initWebCamera());
|
||
}
|
||
IEnumerator initWebCamera(){
|
||
//权限判断,之前这里的写法没注意导致第一次进入会黑屏(iOS)
|
||
yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
|
||
if (Application.HasUserAuthorization(UserAuthorization.WebCam)){
|
||
//拿手机上的相机
|
||
mWebCamDevices = WebCamTexture.devices;
|
||
//CameraType 0后置。根据不同进行判断
|
||
string name = mWebCamDevices [Camertype].name;
|
||
t = new WebCamTexture (name);
|
||
//这里设置的分辨率都不是最终的,最终获取到的分辨率是根据相机自己的参数来的,它会给你匹配一个最接近你设置的。
|
||
t.requestedFPS = CamerFPS;
|
||
t.requestedHeight = CamerHeight;
|
||
t.requestedWidth = CamerWidth;
|
||
//设置Quad的贴图为相机拍摄到的。
|
||
GetComponent<Renderer> ().material.mainTexture = t;
|
||
t.Play ();
|
||
//下面一句必须放在Play后面,否则t.width等获得不到数据。
|
||
//旋转画布到正的位置,这里要特别注意,有些时候会出现画面横过来或者倒了。
|
||
gameObject.transform.localRotation = gameObject.transform.localRotation
|
||
* Quaternion.AngleAxis(t.videoRotationAngle,Vector3.forward);
|
||
}
|
||
}
|
||
// Update is called once per frame
|
||
void Update () {
|
||
//设置Quad画布的大小,如果写在play后面会导致部分时候获取的数据为1。应该是相机还没打开真正打开就开始获取了,所以数据有误,所以放在了这里。
|
||
if (isNeedSetCamera && t!=null && t.width >200 && t.height >200){
|
||
//判断是否翻转。
|
||
gameObject.transform.localScale = new Vector3(t.width*-1, t.height*(t.videoVerticallyMirrored?1:-1), 1f);
|
||
//计算场景内相机的视域(重要)
|
||
gameObject.GetComponentInParent <Camera>().fieldOfView =
|
||
Mathf.Atan(t.height / 2f / gameObject.transform.localPosition.z) * Mathf.Rad2Deg * 2-1;
|
||
isNeedSetCamera = (t.width <200 && t.height <200);
|
||
Debug.Log("fieldOfView:" + gameObject.GetComponentInParent<Camera>().fieldOfView);
|
||
}
|
||
}
|
||
public void StopWebCamera(){
|
||
if (t != null) {
|
||
t.Stop ();
|
||
}
|
||
}
|
||
public void PauseWebCamera(){
|
||
if (t != null && t.isPlaying) {
|
||
t.Pause ();
|
||
}
|
||
}
|
||
public void StartWebCamera(){
|
||
if (t != null) {
|
||
t.Play ();
|
||
isNeedSetCamera = true;
|
||
}
|
||
}
|
||
}
|
||
|