在好多AR项目中都会用到拍照功能,把AR展示的东西进行一下分享,下面就说一下在unity环境下,结合vuforia sdk和GalleryScreenshot插件实现拍照截图功能。 我们使用的插件是GalleryScreenshot
这个插件可以支持win、安卓、ios等平台。不仅仅是可以进行保存而且还是可以保存到相册中,保存功能我们一般都可以通过Unity提供的api实现,但是不好实现的是保留的路径,安卓和ios不一样,这个插件就帮我们解决这个问题。 功能实现过程,先要导出vuforia sdk到项目中,在导入 GalleryScreenshot插件。
和制作普通AR应用一样,只不过在加上拍照就可以
GUI上面就是拍成功能实现代码
using UnityEngine;
using System.Collections;
using System.IO;
public class GalleryScreenshotExample : MonoBehaviour {
public Texture2D texture;
bool saved = false;
bool saved2 = false;
public AudioSource audio;
void Start ()
{
texture = null;
ScreenshotManager.ScreenshotFinishedSaving = ScreenshotSaved;
ScreenshotManager.ImageFinishedSaving = ImageSaved;
}
void OnGUI ()
{
//GUILayout.Label('Example scene showing: \n1. how to save a screenshot\n'
// '2. how to save an image from your assets');
if(GUILayout.Button ('Take Screenshot', GUILayout.Width (200), GUILayout.Height(80)))
{
StartCoroutine(ScreenshotManager.Save('MyScreenshot', 'MyApp', true));
audio.Play();
}
if(saved) GUILayout.Label ('Screenshot was successfully saved');
GUILayout.Space(40);
GUILayout.Label(texture);
//if(GUILayout.Button ('Save ' texture.name, GUILayout.Width (200), GUILayout.Height(80)))
//{
// StartCoroutine('SaveAssetImage');
//}
if(saved2) GUILayout.Label(texture.name ' was successfully saved');
}
IEnumerator SaveAssetImage ()
{
byte[] bytes = texture.EncodeToPNG();
string path = Application.persistentDataPath '/MyImage.png';
File.WriteAllBytes(path, bytes);
yield return new WaitForEndOfFrame();
StartCoroutine(ScreenshotManager.SaveExisting(path, true));
}
void ScreenshotSaved()
{
Debug.Log ('screenshot finished saving');
saved = true;
}
void ImageSaved()
{
Debug.Log (texture.name ' finished saving');
saved2 = true;
}
}
注意一下权限的设置。在导出安卓,经过测试完全可以使用。
在安卓系统下它会存在你手机相册里,你打开相册就能看到。
|
|
来自: W_R_F > 《KnowLedge》