本範例可以使用 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();
}
}
}

請問一下我想要跳到下一個場景就把這web關掉~~~有方法嗎~~ 他一直沒關掉~~鏡頭的燈一直亮著~~~
這樣寫即可,在進入下個場景他就會自動關閉了 WebCamTexture c; void Start () { c = new WebCamTexture (); renderer.material.mainTexture = c; c.Play (); } void OnDisable() { c.Stop(); }
謝謝你的解答:)
你好 想請問如果想要將相片存取該怎麼做呢?
using System.IO; File.WriteAllBytes ("D:/A.png", texture2D.EncodeToPNG());
*****
*****
*****
*****
*****
*****
*****
出現 Cannot use web cam, since the user has not authorized this! UnityEngine.WebCamTexture:Play() 的錯誤訊息 怎解決 攝影機叫不出來 還有renderer.material 他說 沒有material 的定義 要改成this.GetComponent()才能用 是為甚麼
想請問怎樣把camere裡的背景顏色換成自己想要的貼圖,他都只有顏色的選項
*****
請問有辦法把影像傳到另一個裝置上? 比如電腦或者手機 我找到的資料似乎跟 RPC 有關 可是官方說已經過時了
不好意思,我用了上面這個方法,在手機出來的影像會需要轉90度,這是正常的嘛?
我把這個腳本附著在一個Cube上 然後編譯到Android手機上 結果發現 手機鏡頭的紅光有在閃爍 那個方塊有變顏色 (變成粉紫色) 但並沒有出現相機畫面 請問是為什麼呢? 我的手機是Android 6.0版本的
您好,我最近在做Unity與Webcam相關的東西。 我想請問有辦法使用WebCamTexture取得webcam的ˊ每一幀嗎? 我的程式需要針對每一幀做辨識等 雖然不是但可以考慮成類似自己寫臉部偵測 謝謝