Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

take photo function in unity

using UnityEngine;
using System.Collections;
using System.IO;

public class WebCamPhotoCamera : MonoBehaviour 
{
    WebCamTexture webCamTexture;

    void Start() 
    {
        webCamTexture = new WebCamTexture();
        GetComponent<Renderer>().material.mainTexture = webCamTexture; //Add Mesh Renderer to the GameObject to which this script is attached to
        webCamTexture.Play();
    }

    IEnumerator TakePhoto()  // Start this Coroutine on some button click
    {

    // NOTE - you almost certainly have to do this here:

     yield return new WaitForEndOfFrame(); 

    // it's a rare case where the Unity doco is pretty clear,
    // http://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
    // be sure to scroll down to the SECOND long example on that doco page 

        Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
        photo.SetPixels(webCamTexture.GetPixels());
        photo.Apply();

        //Encode to a PNG
        byte[] bytes = photo.EncodeToPNG();
        //Write out the PNG. Of course you have to substitute your_path for something sensible
        File.WriteAllBytes(your_path + "photo.png", bytes);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: loop for x amount of seconds c# 
Csharp :: httpclient 
Csharp :: group-by-in-linq 
Csharp :: how to populate list in c# 
Csharp :: how to get the today date in c# 
Csharp :: get position of another object unity 
Csharp :: find all factors of a given number 
Csharp :: deactivate a gameobject unity 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: how to change text in richtextbox wpf 
Csharp :: predicate EF Core search query 
Csharp :: unity pickup and drop objects 
Csharp :: int array to frequency dictionary c# 
Csharp :: Screen.lockcursor unity 
Csharp :: c# Remove String In C# 
Csharp :: ontriggerenter2d 
Csharp :: unity basic public options 
Csharp :: how to make a character jump c# 
Csharp :: invalidoperationexception c# ui thread 
Csharp :: dictionary all key where value c# 
Csharp :: unity how to make gamemanager instance 
Csharp :: C# How to display text in console 
Csharp :: check if object has parent unity 
Csharp :: how to count letters in c# 
Csharp :: c# datagridview cell align center 
Csharp :: unity navmeshagent set destination 
Csharp :: unity find deactivated gameobject 
Csharp :: declare prop array c# 
Csharp :: docker-compose cassandra db 
Csharp :: how to stop a coroutine unity c# 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =