收藏 分销(赏)

调用摄像头加载到场景中代码.docx

上传人:二*** 文档编号:4808171 上传时间:2024-10-13 格式:DOCX 页数:6 大小:11.87KB 下载积分:5 金币
下载 相关 举报
调用摄像头加载到场景中代码.docx_第1页
第1页 / 共6页
本文档共6页,全文阅读请下载到手机保存,查看更方便
资源描述
Unity3D调用摄像头加载到场景中代码 Posted on 2013年04月10日 by U3d / Unity3D脚本/插件/被围观 300 次 Unity3D调用摄像头加载到场景中代码如下: using UnityEngine; using System.Collections; using System.IO; public class CameraController : MonoBehaviour { public string deviceName; public WebCamTexture webTex; public GameObject objCameraController; public GameObject objAvatar; public GameObject ImgAtlas; public string strTempTime=""; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(strTempTime!="") { // GameObject.Find("pig5").GetComponent().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture; // print("44444:"+strTempTime); } } /// /// 调用摄像机 /// /// IEnumerator CallTheCamera() { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if(Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; deviceName = devices[0].name; //摄像机摄像的区域 webTex = new WebCamTexture(deviceName, 400, 300, -112); GameObject.Find("CameraTexture").GetComponent().mainTexture = webTex; webTex.Play(); } } /// /// 获取并保存texture /// /// IEnumerator GetTexture2D() { print(GameObject.Find("CameraTexture").transform.localPosition); yield return new WaitForEndOfFrame(); Texture2D t = new Texture2D(GameObject.Find("CameraTexture").GetComponent().mainTexture.width, GameObject.Find("CameraTexture").GetComponent().mainTexture.height); t.ReadPixels(new Rect(15, 177, 406.5f, 281.6f), 0, 0, false); t.Apply(); //把图片数据转换为byte数组 byte[] byt = t.EncodeToPNG(); //然后保存为图片 strTempTime = Time.time.ToString(); print("11111:"+strTempTime); File.WriteAllBytes(Application.dataPath + "/Resources/" + strTempTime + ".jpg", byt); print("22222"+strTempTime); //GameObject atlasPrefab = Resources.Load("TempAtlas") as GameObject; // Instantiate(ImgAtlas) as GameObject; // tempA.GetComponent().spriteList.Add( // UnityEditor.AssetDatabase.Refresh(); //yield return new WaitForSeconds(1); GameObject.Find("pig5").GetComponent().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture; print("33333"+strTempTime); // NGUITools.AddSprite(tempA, tempA.GetComponent(), "tempSprite"); // UIAtlas atlas = (Instantiate(ImgAtlas) as GameObject).GetComponent(); // Texture text = Resources.Load(Application.dataPath + "/Resources/" + strTempTime + ".jpg", typeof(Texture)) as Texture; // print(atlas.ToString()); //UISlicedSprite ssprite = NGUITools.AddWidget(Game); // GameObject.Find("pig5").GetComponent().s //UIAtlas atlas = Resources.Load(Application.dataPath + "/test/" + "Camera Atlas", typeof(UIAtlas)) as UIAtlas; //print(atlas.ToString()); // objAvatar.GetComponent().spriteName = atlas.name;//this.gameObject.GetComponent().spriteName; //objCameraController.SetActive(false); } void CameraStart() { StartCoroutine("CallTheCamera"); } void CameraSStart() { StartCoroutine("GetTexture2D"); } void CameraRestart() { webTex.Play(); } void CameraPause() { webTex.Pause(); } void ClickCameraBtn() { objCameraController.SetActive(true); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服