本範例可以使用 PC 的網路攝影機,或是Android 、iOS 的攝影機

使用本程式碼必須將本腳本套用於 "3D模型中" ,例如 Plane 、Cube 等等,不可使用於空物件或攝影機身上,否則不會出現效果。

using UnityEngine;
using System.Collections;

public class CameraTest : MonoBehaviour {

    void Start () {
        WebCamTexture c = new WebCamTexture ();
        renderer.material.mainTexture = c; // 將目前物體貼圖換成攝影機貼圖
        c.Play ();
    }

}

//----------------------------------------------------------- 以下是進階版

using UnityEngine;
using System.Collections;

public class MyCamera : MonoBehaviour {

    WebCamTexture c;

    void Start () {

        WebCamDevice [] wcd = WebCamTexture.devices;

        if(wcd.Length == 0){
            print ("找不到實體攝影機");
        }else{
            foreach(WebCamDevice wc in wcd){
                print ("目前可用的攝影機有:" + wc.name);
            }
            print ("----------------------------------------------------------------");
            print ("目前使用的攝影機是:" + wcd[0].name);
            c = new WebCamTexture (wcd[0].name);
            renderer.material.mainTexture = c;
            renderer.material.shader = Shader.Find("Mobile/Unlit (Supports Lightmap)");
            transform.localScale = new Vector3((float)c.width/c.height, 1);
            c.Play ();
        }
    }

    void OnDisable() {
        if(c != null){
            c.Stop();
        }
    }

}




arrow
arrow
    全站熱搜

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