Vuforia应用之屏幕截图功能

       关于AR应用,一个简单的屏幕截图功能可以让你把原本绚丽多彩分享给其他小伙伴,一起快乐的玩耍,科技生活,有趣世界。不说了,直接上代码:

using UnityEngine;
using System.Collections;
using System.IO;

public class GalleryScreenshotExample : MonoBehaviour {
	
	public Texture2D texture;
	bool saved = false;
	bool saved2 = false;
	
	void Start ()
	{
		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", "Czd", true));
		}
		
		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;
	}
}


你可能感兴趣的:(Vuforia应用之屏幕截图功能)