Unity3D保存图片到本地(IOS Andriod)Unity Native Gallery Plugin插件
程序员文章站
2024-03-23 11:57:46
...
GitHab: https://github.com/yasirkula/UnityNativeGallery。
This plugin helps you save your images and/or videos to device Gallery on Android and Photos on iOS. It is also possible to pick an image or video from Gallery/Photos. It takes only a couple of steps to set everything up:
- Import NativeGallery.unitypackage to your project
- for Android: set Write Permission to External (SDCard) in Player Settings
- for iOS: there are two ways to set up the plugin on iOS:
a. Automated Setup for iOS
- change the value of PHOTO_LIBRARY_USAGE_DESCRIPTION in Plugins/NativeGallery/Editor/NGPostProcessBuild.cs(optional)
- if your minimum Deployment Target (iOS Version) is at least 8.0, set the value of MINIMUM_TARGET_8_OR_ABOVE to truein NGPostProcessBuild.cs
b. Manual Setup for iOS
- set the value of ENABLED to false in NGPostProcessBuild.cs
- build your project
- enter a Photo Library Usage Description in Xcode
- also enter a Photo Library Additions Usage Description, if exists (see: https://github.com/yasirkula/UnityNativeGallery/issues/3)
- insert
-weak_framework Photos -framework AssetsLibrary -framework MobileCoreServices -framework ImageIO
to the Other Linker Flags of Unity-iPhone Target (if your Deployment Target is at least 8.0, it is sufficient to insert-framework Photos -framework MobileCoreServices -framework ImageIO
):
- lastly, remove Photos.framework from Link Binary With Libraries of Unity-iPhone Target in Build Phases, if exists
Example Code
The following code has three functions:
- if you click the left one-third of the screen, it captures the screenshot of the game and saves it to Gallery/Photos
- if you click the middle one-third of the screen, it picks an image from Gallery/Photos and puts it on a temporary quad that is placed in front of the camera
- if you click the right one-third of the screen, it picks a video from Gallery/Photos and plays it
void Update()
{
if( Input.GetMouseButtonDown( 0 ) )
{
if( Input.mousePosition.x < Screen.width / 3 )
{
// Take a screenshot and save it to Gallery/Photos
StartCoroutine( TakeScreenshotAndSave() );
}
else
{
// Don't attempt to pick media from Gallery/Photos if
// another media pick operation is already in progress
if( NativeGallery.IsMediaPickerBusy() )
return;
if( Input.mousePosition.x < Screen.width * 2 / 3 )
{
// Pick a PNG image from Gallery/Photos
// If the selected image's width and/or height is greater than 512px, down-scale the image
PickImage( 512 );
}
else
{
// Pick a video from Gallery/Photos
PickVideo();
}
}
}
}
private IEnumerator TakeScreenshotAndSave()
{
yield return new WaitForEndOfFrame();
Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
ss.Apply();
// Save the screenshot to Gallery/Photos
Debug.Log( "Permission result: " + NativeGallery.SaveImageToGallery( ss, "GalleryTest", "My img {0}.png" ) );
// To avoid memory leaks
Destroy( ss );
}
private void PickImage( int maxSize )
{
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery( ( path ) =>
{
Debug.Log( "Image path: " + path );
if( path != null )
{
// Create Texture from selected image
Texture2D texture = NativeGallery.LoadImageAtPath( path, maxSize );
if( texture == null )
{
Debug.Log( "Couldn't load texture from " + path );
return;
}
// Assign texture to a temporary quad and destroy it after 5 seconds
GameObject quad = GameObject.CreatePrimitive( PrimitiveType.Quad );
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
quad.transform.forward = Camera.main.transform.forward;
quad.transform.localScale = new Vector3( 1f, texture.height / (float) texture.width, 1f );
Material material = quad.GetComponent<Renderer>().material;
if( !material.shader.isSupported ) // happens when Standard shader is not included in the build
material.shader = Shader.Find( "Legacy Shaders/Diffuse" );
material.mainTexture = texture;
Destroy( quad, 5f );
// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
Destroy( texture, 5f );
}
}, "Select a PNG image", "image/png", maxSize );
Debug.Log( "Permission result: " + permission );
}
private void PickVideo()
{
NativeGallery.Permission permission = NativeGallery.GetVideoFromGallery( ( path ) =>
{
Debug.Log( "Video path: " + path );
if( path != null )
{
// Play the selected video
Handheld.PlayFullScreenMovie( "file://" + path );
}
}, "Select a video" );
Debug.Log( "Permission result: " + permission );
}