Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android crash on WebCamTexture.Play()

Discussion in 'Scripting' started by Aldo, Jan 26, 2017.

  1. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Unity 5.5.0p1

    Whenever I close the app manually, make the app crash or pause the app this crashes at the next attempt to play a WebCamTexture.

    For it to work again I must restart the phone.

    Simple way to do this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class WebCamScript : MonoBehaviour
    7. {
    8.     private WebCamTexture mCamera, mCamera2;
    9.     public GameObject frontCameraPlane,
    10.         backCameraPlane;
    11.  
    12.     private bool frontCamera = true;
    13.     private WebCamDevice[] devices;
    14.  
    15.     IEnumerator Start()
    16.     {
    17.         devices = WebCamTexture.devices;
    18.  
    19.         mCamera2 = new WebCamTexture(Screen.width, Screen.height);
    20.         mCamera2.filterMode = FilterMode.Trilinear;
    21.  
    22.         backCameraPlane.GetComponent<Renderer>().material.mainTexture = mCamera2;
    23.  
    24.         float yScale2 = backCameraPlane.transform.localScale.x * (float)Mathf.Min((float)Screen.width, (float)Screen.height) / (float)Mathf.Max((float)Screen.width, (float)Screen.height);
    25.         backCameraPlane.transform.localScale = new Vector3(backCameraPlane.transform.localScale.x, yScale2, 1);
    26.  
    27.         if (devices.Length > 1)
    28.         {
    29.             mCamera = new WebCamTexture(devices[devices.Length - 1].name, Screen.width, Screen.height);
    30.             mCamera.filterMode = FilterMode.Trilinear;
    31.  
    32.             frontCameraPlane.GetComponent<Renderer>().material.mainTexture = mCamera;
    33.  
    34.             float yScale1 = -frontCameraPlane.transform.localScale.x * (float)Mathf.Min((float)Screen.width, (float)Screen.height) / (float)Mathf.Max((float)Screen.width, (float)Screen.height);
    35.             frontCameraPlane.transform.localScale = new Vector3(frontCameraPlane.transform.localScale.x, yScale1, 1);
    36.         }
    37.  
    38.         yield return 0;
    39.  
    40.         if (devices.Length > 1)
    41.         {
    42.             frontCameraPlane.SetActive(true);
    43.             backCameraPlane.SetActive(false);
    44.             mCamera.Play();
    45.  
    46.         }
    47.         else
    48.         {
    49.             frontCamera = false;
    50.             frontCameraPlane.SetActive(false);
    51.             backCameraPlane.SetActive(true);
    52.             mCamera2.Play();
    53.         }
    54.     }
    55. }
    This is just the initialization of my script, if you place 2 game objects on screen with he height 100% on portrait mode it will make them fill all the screen.

    if you have 2 cameras it will start with front camera, else it will show back/only camera.

    --------------------How to make it crash-------------------------

    Just hit the home button and then try to resume the app.
    It will crash when the WebCamTexture.Play(); is called.

    Problem isn't just that, problem is that you won't be able to close the app and make it work again unless you restart the phone.
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    5.5.1 release notes
    • Android: Fixed a crash when reloading or resuming scene which uses WebCamTexture. (855603, 859268)
    I'll test it tomorrow and hope it is fixed
     
  3. codeage

    codeage

    Joined:
    Feb 7, 2015
    Posts:
    14
    This problem still exists in the latest version (5.5.2p3). If you call WebCamTexture.Pause() and then call WebCamTexture.Stop() in OnApplicationPause() then there may be less crashes but still could not resolve the problem
    completely.
    .