Unity Asset for accessing native gallery of iOS and Android devices without leaving Unity UI
OS Requirements:
From anywhere call static method EGPicker.OpenPicker()
. Callback will be called with EGPickedItem
. Use picked item to load Texture2D
:
xxxxxxxxxx
EGPicker.OpenPicker ((item) => {
EGPicker.LoadImageAsync (this, item).ContinueWith ((t) => {
if (t.IsCanceled) {
print ("Loading image canceled");
} else if (t.IsFaulted) {
print ($"Failed to load image: {t.Exception}");
} else {
Texture2D texture = t.Result;
// do something with texture
}
}, TaskScheduler.FromCurrentSynchronizationContext ()).LogExceptions();
});
Main class to work with gallery picker. Contains methods to open picker and load textures.
Opens gallery picker on a separate canvas.
xxxxxxxxxx
public static void OpenPicker (Action<EGPickedItem> OnPickedItem, string pickerSkin = "Standard", Action OnCancel = null, bool resetPicker = false, bool multipleMode = false, bool cameraPicker = false, string capturedImagesAlbumName = null, bool showAlbumSelector = true)
Parameter | Description |
---|---|
OnPickedItem (Action | Picked item callback. Can be called multiple times if multipleMode enabled. |
pickerSkin (string) | Picker sking to use. Default: "Standard" |
OnCancel (Action) | Picker dismiss callback. Default: null. |
resetPicker (bool) | If true then picker is recreated when loaded. Otherwise it's preserved from previous use. Default: false. |
multipleMode (bool) | If false, picker closes on first item selection. If true can select multiple items. Default: false. |
cameraPicker (bool) | If true adds camera viewfinder for capturing images. Default: false |
capturedImagesAlbumName (string) | Album name where to put images captured with camera. Default: null (if null then overriden later to Application.productName) |
showAlbumSelector (bool) | If true shows scrollable album selector strip on top of the picker. Default: true |
xxxxxxxxxx
EGPicker.OpenPicker ((item) => {
EGPicker.LoadImageAsync (this, item).ContinueWith ((t) => {
if (t.IsCanceled) {
print ("Loading image canceled");
} else if (t.IsFaulted) {
print ($"Failed to load image: {t.Exception}");
} else {
var tex = t.Result;
if (rawImage.texture != null) {
Destroy (rawImage.texture);
}
rawImage.texture = tex;
aspectRatioFitter.aspectRatio = (float) tex.width / tex.height;
}
}, TaskScheduler.FromCurrentSynchronizationContext ()).LogExceptions();
});
Loads Texture2D asynchronously given item picked from picker.
xxxxxxxxxx
public static Task<Texture2D> LoadImageAsync (MonoBehaviour driver, EGPickedItem pickedItem, int maxResolution = 0)
Parameter | Description |
---|---|
driver (MonoBehaviour) | MonoBehaviour to use for texture loadings coroutine. |
pickedItem (EGPickedItem) | Item to load. |
maxResolution (int) | Limit max resolution. 0 - no limit. Default: 0. |
xxxxxxxxxx
EGPicker.OpenPicker ((item) => {
// load picked item texture
EGPicker.LoadImageAsync (this, item).ContinueWith ((t) => {
if (t.IsCanceled) {
print ("loading image canceled");
} else if (t.IsFaulted) {
print ($"failed to load image: {t.Exception}");
} else {
var tex = t.Result;
if (rawImage.texture != null) {
Destroy (rawImage.texture);
}
rawImage.texture = tex;
aspectRatioFitter.aspectRatio = (float) tex.width / tex.height;
}
}, TaskScheduler.FromCurrentSynchronizationContext ()).LogExceptions();
}, OnCancel: () => {
print ("dismissed picker");
});
Interface to native galleries and camera on Android and iOS + Editor Mockup
Gets albums from native photo gallery on iOS or Android.
xxxxxxxxxx
public static Task<List<EGAlbum>> GetAlbumsAsync ()
Gets thumbnails and image data (width, height) from native gallery in paginated manner. Prepares and returns paths to thumbnail images on native side.
xxxxxxxxxx
public static Task<EGThumbnailsRequestResult> GetThumbnailsAsync (int page, int perPage, string albumId = "", int approximateSize = 150)
Parameter | Description |
---|---|
page (int) | Page to get |
perPage (int) | Items per page |
albumId (string) | Filter by album id. If empty string - no filtering (default). |
approximateSize (int) | Approximate thumbnails size to prepare. |
Returns Texture2D for specified native image id.
xxxxxxxxxx
public static Task<Texture2D> GetImageTextureAsync (string imageId, MonoBehaviour driver, int maxResolution = 0)
Parameter | Description |
---|---|
imageId (string) | Id of image to return |
driver (MonoBehaviour) | MonoBehaviour that will be used for texture-loading coroutine |
maxResolution (int) | Limit resolution of the image. 0 - no limit (can be overriden later to SystemInfo.maxTextureSize if bigger). |
Saves texture to native gallery under specified album.
public static Task SaveImageAsync (Texture2D texture, string albumName)
Parameter | Description |
---|---|
texture (Texture2D) | Texture2D to save |
albumName (string) | Album name where to put image |
Returns permission state for gallery access.
public static EGPermission CheckGalleryPermission ()
Triggers native request for gallery permission.
public static EGPermission RequestGalleryPermission ()
Returns permission state for device camera access.
public static EGPermission CheckCameraPermission ()
Trigger native request for device camera access.
public static EGPermission RequestCameraPermission ()
Determines if its possible to open settings for changing gallery permission.
public static bool CanOpenSettings ()
Opens native Settings on the app's section to change permission.
public static void OpenSettings ()
In this class you can customize constants such as text that appears to ask user for gallery access.