您的位置:首页 > 运维架构 > Linux

Unity 入門教學:拍照,支持 Android、iOS、PC、MAC、Linux...

2014-09-17 12:31 591 查看
本範例共需兩個 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(0, 0, 100, 50), "拍照")){

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記憶卡的話

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐