本範例共需兩個 Plane,一個是動態畫面預覽,另一個是拍照完畢顯示。

將本程式碼套用至其中一個 Plane,而另一個Plane 套用至本程式碼的  public GameObject g; 之中

運行後即可有效果。

本程式碼支持 PC 的 WebCam 網路攝影機、Android 與 iOS 的攝影機。

using UnityEngine;
using System.Collections;

public class MyCamera : MonoBehaviour {

    public GameObject g;

    WebCamTexture c;

    void Start () {
        c = new WebCamTexture (WebCamTexture.devices[0].name);
        renderer.material.mainTexture = c;
        c.Play ();
    }

    void OnGUI(){
        if(GUI.Button(new Rect(0010050), "拍照")){
            Texture2D t = new Texture2D(c.width, c.height);
            t.SetPixels(c.GetPixels());
            t.Apply();
            g.renderer.material.mainTexture = t;
//          System.IO.File.WriteAllBytes("image.png", t.EncodeToPNG());  // 如果你要保存至硬碟or記憶卡的話
        }
    }

}





arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(18) 人氣()