Unity3D调用摄像头并保持为图片然后加载到场景中代码 拍照照相代码
using UnityEngine;using System.Collections;
using System.IO;
public class CameraController : MonoBehaviour {
public string deviceName;
publicWebCamTexture 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<UITexture>().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
// print("44444:"+strTempTime);
}
}
/// <summary>
/// 调用摄像机
/// </summary>
/// <returns></returns>
IEnumerator CallTheCamera()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if(Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices.name;
//摄像机摄像的区域
webTex = new WebCamTexture(deviceName, 400, 300, -112);
GameObject.Find("CameraTexture").GetComponent<UITexture>().mainTexture = webTex;
webTex.Play();
}
}
/// <summary>
/// 获取并保存texture
/// </summary>
/// <returns></returns>
IEnumerator GetTexture2D()
{
print(GameObject.Find("CameraTexture").transform.localPosition);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(GameObject.Find("CameraTexture").GetComponent<UITexture>().mainTexture.width, GameObject.Find("CameraTexture").GetComponent<UITexture>().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<UIAtlas>().spriteList.Add(
//
UnityEditor.AssetDatabase.Refresh();
//yield return new WaitForSeconds(1);
GameObject.Find("pig5").GetComponent<UITexture>().mainTexture = Resources.Load(strTempTime.ToString(), typeof(Texture)) as Texture;
print("33333"+strTempTime);
// NGUITools.AddSprite(tempA, tempA.GetComponent<UIAtlas>(), "tempSprite");
// UIAtlas atlas = (Instantiate(ImgAtlas) as GameObject).GetComponent<UIAtlas>();
// Texture text = Resources.Load(Application.dataPath + "/Resources/" + strTempTime + ".jpg", typeof(Texture)) as Texture;
//print(atlas.ToString());
//UISlicedSprite ssprite = NGUITools.AddWidget<UISlicedSprite>(Game);
// GameObject.Find("pig5").GetComponent<UISlicedSprite>().s
//UIAtlas atlas = Resources.Load(Application.dataPath + "/test/" + "Camera Atlas", typeof(UIAtlas)) as UIAtlas;
//print(atlas.ToString());
//objAvatar.GetComponent<UISlicedSprite>().spriteName = atlas.name;//this.gameObject.GetComponent<UISlicedSprite>().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);
}
}
Assets/GUI/paizao.cs(41,59): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,83): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,161): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
Assets/GUI/paizao.cs(54,193): error CS1502: The best overloaded method match for `UnityEngine.Texture2D.Texture2D(int, int)' has some invalid arguments
Assets/GUI/paizao.cs(54,193): error CS1503: Argument `#1' cannot convert `object' expression to type `int'
Assets/GUI/paizao.cs(70,46): error CS0246: The type or namespace name `UITexture' could not be found. Are you missing a using directive or an assembly reference?
这些是什么情况{:5_385:} 学习学习了 楼主是超人 好帖就是要顶 真心顶 很好哦 LZ真是人才 楼主是超人 真心顶
页:
[1]