Code Monkey home page Code Monkey logo

unitynativegallery's Introduction

Unity Native Gallery Plugin

This plugin helps you save your images and/or videos to device Gallery on Android and Photos on iOS. It only takes 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: enter a Photo Library Usage Description in Xcode

PhotoLibraryUsageDescription

  • for iOS: also enter a Photo Library Additions Usage Description, if exists (see: yasirkula#3)
  • That's it; lol!

How To

There are three variants of NativeGallery.SaveToGallery function:

  1. NativeGallery.SaveToGallery( byte[] mediaBytes, string directoryName, string filenameFormatted, bool isImage ): use this function if you have the raw bytes of the image/video.
  • On Android, your images are saved at DCIM/directoryName/filenameFormatted. On iOS, directoryName has no effect.
  • filenameFormatted is string.Format'ed to avoid overwriting the same file on Android, if desired. If, for example, you want your images to be saved in a format like "My img 1.png", "My img 2.png" and etc., you can set the filenameFormatted as "My img {0}.png". {0} here is replaced with a unique number to avoid overwriting an existing file. If you don't use a {0} in your filenameFormatted parameter and a file with the same name exists at that path, the file will be overwritten. On the other hand, a saved image/video is never overwritten on iOS.
  • isImage should be true for images and false for videos.
  1. NativeGallery.SaveToGallery( string existingMediaPath, string directoryName, string filenameFormatted, bool isImage ): use this function if the image/video is already saved on disk. Enter the file's path to existingMediaPath. The file will be copied to DCIM/directoryName/filenameFormatted on Android and temporarily copied to Application.persistentDataPath/filenameFormatted on iOS (copied file will automatically be deleted after saving the image/video as iOS keeps a separate copy of its media files in its internal directory).

  2. NativeGallery.SaveToGallery( Texture2D image, string directoryName, string filenameFormatted ): use this function to easily save a Texture2D to Gallery/Photos. If filenameFormatted ends with ".jpeg" or ".jpg", texture will be saved as JPEG; otherwise, it will be saved as PNG.

  3. for Android only: NativeGallery.DeleteFromGallery( string path, bool isImage ): you can use this function to remove a file located at path from the Gallery. For security reasons, this function is not available on iOS.

Example Code

The following code captures the screenshot of the game and saves it to Gallery/Photos whenever you tap the screen:

void Update()
{
	if( Input.GetMouseButtonDown( 0 ) )
		StartCoroutine( TakeSS() );
}
	
private IEnumerator TakeSS()
{
	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();

	NativeGallery.SaveToGallery( ss, "GalleryTest", "My img {0}.png" );
}

unitynativegallery's People

Contributors

yasirkula avatar

Watchers

Daniel García avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.