我想在Google Cardboard应用程序中显示实时相机提要。基本上说 - 我只是想看看我的相机在纸板应用程序中看到的内容。你能告诉我如何实现它吗?我迷失在 Unity 中。
我认为您想要 Unity 的立体视图,它非常简单 尝试使用此代码从相机获取实时馈送,并按照以下步骤在纸板应用程序中制作您的应用程序。
/***** script for getting live feed from camera *****/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CamScript : MonoBehaviour {
private bool camAvilable ;
private WebCamTexture webcam;
private Texture2D defaultbackground;
private Texture2D output;
public RawImage background;
public RawImage imageToDisplay;
public AspectRatioFitter fit;
private Color[] data;
void IntialCam(){
defaultbackground = background.texture;
WebCamDevice[] cam_devices = WebCamTexture.devices;
if(cam_devices.Length == 0){
Debug.Log ("No Camera");
camAvilable = false;
return;
}
webcam = new WebCamTexture (cam_devices [0].name, 320, 400, 60);
if (webcam == null) {
Debug.Log ("Unable to detect camera");
return;
}
webcam.Play ();
background.texture = webcam;
imageToDisplay.texture = output;
camAvilable = true;
}
void GetFeed(){
if(!camAvilable){
return;
}
float ratio = (float)webcam.width / (float)webcam.height;
fit.aspectRatio = ratio;
float scaleY = webcam.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3 (1f, scaleY, 1f);
int orinat = -webcam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3 (0,0,orinat);
}
void Start () {
IntialCam ();
output = new Texture2D (320,400);
GetComponent<Renderer> ().material.mainTexture = output;
data = new Color[webcam.width * webcam.height];
}
// Update is called once per frame
void Update () {
GetFeed ();
webcam.GetPixels(data);
output.SetPixels(data);
output.Apply();
}
}
现在要使其成为纸板应用程序,您只需要转到Unity 播放器设置>>其他设置>>选中虚拟现实复选框>>然后在虚拟现实 SDK 中选择纸板。
就是这样,现在只需构建它,您就会得到结果。