- Home /
WebCamTexture problem
I am developing an app for iOs and Android devices, I want to use the front camera of the device, I used WebCamTexture for this purpose. When I run the app in Editor, the stream from camera comes perfect, but on iphone or iPad the stream is 90 degrees rotated. I searched the forums and got into this solution:
http://docs.unity3d.com/ScriptReference/WebCamTexture-videoRotationAngle.html
I read somewhere that using the link above must fix your issue, but when I run the code above on iPhone, my whole plane (GameObject which is running the script) dissappears. I searched a lot but did'nt found anything. how can I solve this issue?
my Unity pro version: 4.5.5 xcode version: 6.1 running on iPhone 5S with iOS version: 7.1 also running on iPad with iOS version: 8.0.1 also running on Simulator (iPhone 6 version: 8.1)
Answer by swappy123 · Jan 24, 2020 at 12:11 PM
I am not sure if my answer will work, but this is finest solution that could help you out for camera rotation angle.
(Below code is not created by me, I have followed tutorial on YouTube to get working camera for my devices and changed some part of code as required by me).
Steps to use:
1) Add RawImage UI gameobject in your scene. (set it's pivot to stretch to fill screen width and height).
2) Add AspectRatioFitter script on this object.
3) Attach following script to any other gameobject you like.
4) Add appropriate component reference to your CameraManager script objects.
private bool isCamAvailable;
private WebCamTexture backCam;
private Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
private void Start()
{
StartCoroutine("CheckAuthentication");
}
private void Update()
{
if (!isCamAvailable)
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);
}
IEnumerator CheckAuthentication() {
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam)) {
defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
{
Debug.Log("No camera detected");
isCamAvailable = false;
}
else
{
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("No back cam found!!");
backCam = new WebCamTexture(devices[0].name, Screen.width, Screen.height);
//return;
}
backCam.Play();
background.texture = backCam;
isCamAvailable = true;
}
}
}
}
vote up if you like my answer. Happy coding..