Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Webcamtexture does not start on some devices

Discussion in 'Android' started by AkhmedAbasov, Dec 11, 2019.

  1. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    The camera starts to work only after the game is minimized and deployed. Is there a solution for this problem?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Linq;
    6. using System.IO;
    7. using System;
    8. using TMPro;
    9.  
    10. namespace Fitogotchi
    11. {
    12.     public class TestCamers : Singleton<TestCamers>
    13.     {
    14.         [SerializeField] TMP_InputField errorPanelInputField;
    15.         [SerializeField] RawImage cameraRawImage;
    16.         [SerializeField] Text Btn;
    17.  
    18.         WebCamDevice[] webCamDevices;
    19.         WebCamDevice cameraDevice_1, cameraDevice_2;
    20.         string cameraDevice_1_name, cameraDevice_2_name;
    21.  
    22.         WebCamTexture webcamTexture;
    23.  
    24.         void Start()
    25.         {
    26.             webCamDevices = WebCamTexture.devices;
    27.  
    28.             /*
    29.             foreach (var camDevice in webCamDevices)
    30.             {
    31.                 if (camDevice.isFrontFacing)
    32.                 {
    33.                     cameraDevice_1 = camDevice;
    34.                 }
    35.                 else
    36.                 {
    37.                     cameraDevice_2 = camDevice;
    38.                 }
    39.             }
    40.             */
    41.  
    42.             cameraDevice_1 = webCamDevices[0];
    43.             cameraDevice_2 = webCamDevices[1];
    44.  
    45.             Debug.Log("#################################################################################");
    46.             Debug.Log("######### " + webCamDevices.Count());
    47.             Debug.Log("######### " + webCamDevices[0].name);
    48.             Debug.Log("######### " + webCamDevices[1].name);
    49.  
    50.  
    51.             webcamTexture = new WebCamTexture(cameraDevice_1.name);
    52.             cameraRawImage.texture = webcamTexture;
    53.             cameraRawImage.material.mainTexture = webcamTexture;
    54.             webcamTexture.Play();
    55.  
    56.             errorPanelInputField.text = "C1 - " +  webcamTexture.videoRotationAngle.ToString();
    57.         }
    58.  
    59.         int v = 0;
    60.         public void SetSameraRawImageSettings()
    61.         {
    62.             RectTransform rectTransform = cameraRawImage.GetComponent<RectTransform>();
    63.  
    64.             if (v < 3)
    65.                 v++;
    66.             else
    67.                 v = 0;
    68.  
    69.             switch (v)
    70.             {
    71.                 case 0:
    72.                     rectTransform.localScale = new Vector3(1, 1, 1);
    73.                     break;
    74.                 case 1:
    75.                     rectTransform.localScale = new Vector3(1, -1, 1);
    76.                     break;
    77.                 case 2:
    78.                     rectTransform.localScale = new Vector3(-1, -1, 1);
    79.                     break;
    80.                 case 3:
    81.                     rectTransform.localScale = new Vector3(-1, 1, 1);
    82.                     break;
    83.             }
    84.  
    85.            
    86.             Btn.text = v.ToString();
    87.         }
    88.  
    89.  
    90.  
    91.         public void C1()
    92.         {
    93.             if(webcamTexture.isPlaying)
    94.                 webcamTexture.Stop();
    95.  
    96.             webcamTexture = new WebCamTexture(cameraDevice_1.name);
    97.             cameraRawImage.texture = webcamTexture;
    98.             cameraRawImage.material.mainTexture = webcamTexture;
    99.             webcamTexture.Play();
    100.  
    101.             errorPanelInputField.text += "C1 - " + webcamTexture.videoRotationAngle.ToString();
    102.         }
    103.  
    104.         public void C2()
    105.         {
    106.             if (webcamTexture.isPlaying)
    107.                 webcamTexture.Stop();
    108.  
    109.             webcamTexture = new WebCamTexture(cameraDevice_2.name);
    110.             cameraRawImage.texture = webcamTexture;
    111.             cameraRawImage.material.mainTexture = webcamTexture;
    112.             webcamTexture.Play();
    113.  
    114.             errorPanelInputField.text += "C2 - " + webcamTexture.videoRotationAngle.ToString();
    115.         }
    116.  
    117.     }
    118.  
    119. }
    120.  
    121.