- Home /
Question by
sallehhuddinali · Dec 05, 2019 at 09:56 AM ·
unity 5applicationfixerror-handlinglong
can someone fix the loop error.. Why this app running only for a moment in my phone ?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PhoneCamera : MonoBehaviour { public bool camAvailable; private WebCamTexture backCam; private Texture defaultBackGround;
public RawImage background;
public AspectRatioFitter fit;
public void Start()
{
defaultBackGround = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
{
Debug.Log("no camera detected");
camAvailable = false;
return;
}
for (int i = 0; i < devices.Length; i++)
{
if (!devices[i].isFrontFacing)
{
backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
}
}
if (backCam == null) {
Debug.Log("Unable to find Camera");
return;
}
backCam.Play();
background.texture = backCam;
camAvailable = true;
}
public void Update()
{
if (!camAvailable)
{ return; }
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -backCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
}
Comment