Search Unity

WebcamTexture not playing when called with button click event

Discussion in 'Scripting' started by SavedByZero, Feb 10, 2016.

  1. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    124
    I've attached the script below to an image on a canvas. That canvas also has a play and stop button.

    Okay, so the code below starts the webcam and records just fine when I call it with StartCoroutine(play()) from the Start function. However, when I press a "play" button and call OnPlay(), which in turn calls the same coroutine as I call in the Start() function, it doesn't play. I've traced that the button is actually calling the right function, but I get all "false" results for those isPlaying traces when I do it that way. Any idea what would cause this?

    If you're wondering why I'm using a coroutine, it's because it seems to need a slight delay before I start playing and mapping it to the canvas.

    The stop button / OnStop() function works fine in all cases.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. public class WebcamRecorder : MonoBehaviour {
    7.         private WebCamTexture _webcamTexture;
    8.         public List<Color32[]> data;
    9.     // Use this for initialization
    10.     void Start () {
    11.                 data = new List<Color32[]> ();
    12.                 _webcamTexture = new WebCamTexture ();
    13.                 StartCoroutine (play());   //WORKS
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.                 //Debug.Log ("isPlaying" + _webcamTexture.isPlaying);
    19.                 if (_webcamTexture.isPlaying)
    20.                     data.Add (_webcamTexture.GetPixels32 ());
    21.  
    22.     }
    23.  
    24.     public void OnPlay()
    25.     {
    26.                 Debug.Log("web cam texture " + _webcamTexture);
    27.                 StartCoroutine(play());   //DOESN'T WORK
    28.                 Debug.Log ("playing " + _webcamTexture.isPlaying);
    29.     }
    30.  
    31.         IEnumerator play()
    32.         {
    33.              
    34.             Debug.Log ("trying " + _webcamTexture.isPlaying);  
    35.             _webcamTexture.Play ();
    36.             Debug.Log ("trying2 " + _webcamTexture.isPlaying);  
    37.             yield return new WaitForSeconds (0.1f);
    38.             GetComponent<Image> ().canvasRenderer.SetTexture (_webcamTexture);
    39.             Debug.Log ("trying3 " + _webcamTexture.isPlaying);  
    40.                      
    41.         }
    42.  
    43.     public void OnStop()
    44.     {
    45.             //yield return new WaitForSeconds (10f);
    46.             _webcamTexture.Stop ();
    47.             Debug.Log ("pixel length:" + data.Count + " one entry size: " + data[0].Length);
    48.  
    49.     }
    50. }
    51.  
     
    Last edited: Feb 10, 2016