Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UI + webcam texture

Discussion in 'Unity 5 Pre-order Beta' started by Andrei_Leonte, Jan 21, 2015.

  1. Andrei_Leonte

    Andrei_Leonte

    Joined:
    Mar 13, 2014
    Posts:
    14
    Hi,

    I've been grappling with this for a few days now. I've managed to put the direct feed from the mobile phone camera onto a RawImage by simply assigning the WebCamTexture as the RawImage's texture variable. However, I want to make a snapshot(using GetPixels() or GetPixels32()), show that picture in another UI image(presumably another RawImage) and eventually encode it and upload it to a server.

    Here's the problem: I can't see any way to write those pixels in the RawImage object - there is no Texture2D member variable in the RawImage class(contrary to the documentation) and no method to set pixels directly in RawImage. There's no way to confirm that the snapshot even works.

    If I create a new Texture2D object with the width and height of the webcam texture and set this object's pixels, dropping it on the RawImage as its texture has an unpredictable effect - nothing like what the camera sees.

    Am I missing something obvious? I haven't found any sign so far of anyone combining WebCamTexture and the new Unity UI.

    Thank you
     
  2. Andrei_Leonte

    Andrei_Leonte

    Joined:
    Mar 13, 2014
    Posts:
    14
    It turns out I was missing a call to Texture2D.Apply().
     
  3. Bres_Tech

    Bres_Tech

    Joined:
    Feb 4, 2015
    Posts:
    2
    How did you assign the WebcamTexture as the RawImage's texture variable, I'm struggling to get the WebcamTexture working with the new UI to display a live feed, the script I've been using is as follows:

    RawImage background = new GameObject("Image").AddComponent<RawImage>();

    back = new WebCamTexture("Logitech HD Webcam C310");
    back.Play ();

    background.texture = back;

    Would appreciate any help.

    Thanks
     
  4. Andrei_Leonte

    Andrei_Leonte

    Joined:
    Mar 13, 2014
    Posts:
    14
    Are you sure the webcam texture actually works?
    Try logging your camera devices, the assignation looks ok to me, I don't think that's the problem.

    Also, try to check that your game object is actually a valid pointer to an object in the editor and that your component is present.

    The way I set things up is have the RawImage object added directly in the canvas hierarchy in Unity Editor, with the RawImage component readily available. Then I drag the RawImage object onto the slot in the C# script component.

    My code is this:

    Code (CSharp):
    1.     private WebCamTexture webCamTexture;
    2.     public RawImage imgCam;
    3.  
    4.  
    5.  
    6.     public void Show(bool bShow)
    7.     {
    8.         gameObject.SetActive(bShow);
    9.  
    10.         if(webCamTexture==null)
    11.             InitCam();
    12.  
    13.         if(bShow)
    14.             webCamTexture.Play();
    15.         else
    16.             webCamTexture.Stop();
    17.     }
    18.  
    19.  
    20.  
    21.     void InitCam()
    22.     {
    23.         webCamTexture=new WebCamTexture(640,480);
    24.  
    25.         Debug.Log("Camera devices:");
    26.  
    27.         WebCamDevice[] devices=WebCamTexture.devices;
    28.  
    29.         int i = 0;
    30.         while (i < devices.Length)
    31.         {
    32.             Debug.Log(devices[i].name);
    33.             i++;
    34.         }
    35.  
    36.         imgCam.texture=webCamTexture;
    37.  
    38.         //flip preview on iOS
    39. #if UNITY_IOS
    40.         imgCam.gameObject.GetComponent<RectTransform>().localScale=new Vector3(1,-1,1);
    41.         Debug.Log ("ios - flipping camera preview");
    42. #endif
    43.     }
    44.  
    45.  
    46.  
    47.  

    It's not final, but the preview works. I even managed to get the pixels from a snapshot.
     
    Artgig and Bres_Tech like this.
  5. Bres_Tech

    Bres_Tech

    Joined:
    Feb 4, 2015
    Posts:
    2
    I implemented the public call for the RawImage and dragged the RawImage object onto the slot in the C# component, it works perfectly.

    Thanks for your help it much appreciated.
     
  6. Andrei_Leonte

    Andrei_Leonte

    Joined:
    Mar 13, 2014
    Posts:
    14
    Glad you solved it!