private string filename;
IEnumerator getTexture2d()
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
byte[] bytes = t.EncodeToPNG();
t.Compress(true);
t.Apply();
//获取系统时间
System.DateTime now = new System.DateTime();
now = System.DateTime.Now;
string filename = string.Format("image{0}{1}{2}{3}.png", now.Day, now.Hour, now.Minute, now.Second);
//记录每一个截图名字
StreamWriter sw;
FileInfo ft = new FileInfo(filepath);
if (!ft.Exists)
{
sw = ft.CreateText();
}
else
{
sw = ft.AppendText();
}
sw.WriteLine(filename);
sw.Close();
sw.Dispose();
//应用平台判断,路径选择
if (Application.platform == RuntimePlatform.Android)
{
string origin = Path_save;
destination = "/mnt/sdcard/DCIM/ARphoto";
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
destination = destination + "/" + filename;
Path_save = destination;
}
//保存文件
File.WriteAllBytes(Path_save, bytes);
}
//以上代码是实现在unity3d里面截图并且保存在本地相册里面
public string deviceName;//这个虽然public,但无需为其绑定变量,直接运行,默认调用,显示本地摄像机的名称
WebCamTexture webCam;
void Start()
{
filepath = Application.persistentDataPath + "/test.txt";
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
webCam = new WebCamTexture(deviceName, 400, 300, 12);
Renderer renderer = GetComponent
renderer.material.mainTexture = webCam;
webCam.Play(); //可以通过按键来控制打开或者关闭摄像头
}
//以上是控制摄像头开关的代码
public void TakePhoto()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, PHOTOHRAPH);
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
}
//以上这段代码 打开摄像机,手动拍照,这段代码是Android端的代码,在unity里面通过下面代码进行调用
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); //获取unity的Java类,只能调用静态方法,获取静态属性
AndroidJavaObject jo = jc.GetStatic
//一开始就调用安卓选择图片
jo.Call("TakePhoto");
Android与unity3d交互的XML文件,用下载实例中的xml,只需修改包名就可以了