Native APIs | Camera & Gallery: Scripting

This section provides a guide to work with Camera & Gallery (Media) scripting APIs.

You can access the Camera & Gallery module APIs via the Media class under the EasyMobile namespace.

Camera

All the Camera APIs can be accessed via Media.Camera property.

Checking Camera Availability

To determine if a camera is available on the current device, simply call the IsCameraAvailable with the camera type you want to check, which is CameraType.Front or Camera.Rear

Currently the camera type specified when taking pictures or recording video is only repspected by iOS, not Android. But you can always switch the camera inside the opened camera app.

CameraType cameraType = CameraType.Front;
bool result = Media.Camera.IsCameraAvailable(cameraType);

Taking Pictures

First specify the camera you want to use to take picture.

CameraType cameraType = CameraType.Front;

Open the device's camera to take picture.

Media.Camera.TakePicture(cameraType, TakePictureCallback);

This callback will be invoked after the user finishes taking picture and returns to the app.

private void TakePictureCallback(string error, MediaResult result)
{
    if (!string.IsNullOrEmpty(error))
    { 
        // This means users failed to take picture,
        // you should show the error to them.
    }
    else
    {
        // This means the picture has been taken successfully.
        // Now can get the image uri and load it manually.
        string uri = result.Uri;

        // Or you can use the built-in method to 
        // load the taken picture whenever needed. 
    }
}

See the Loading Image from MediaResults section below to see how to use the built-in method to load an image in MediaResult.

Recording Video

First specify the camera type you want to use to record video.

CameraType cameraType = CameraType.Front;

Open the device's camera to record video.

Media.Camera.RecordVideo(cameraType, RecordVideoCallback);

This callback will be invoked after the user finishes recording video and returns to the app.

private void RecordVideoCallback(string error, MediaResult result)
{
    if (!string.IsNullOrEmpty(error))
    { 
        // This means users failed to record video,
        // you should show the error to them.
    }
    else
    {
        // This means the video has been recorded successfully.
        // Now you can use the get the video's uri and load it
        string uri = result.Uri;

        // A simple example to display recorded video with its url.
        Handheld.PlayFullScreenMovie(uri,
            Color.black, // Background color
            FullScreenMovieControlMode.Full, // Standard control mode.
            FullScreenMovieScalingMode.None); // Don't scale the video.
    }
}

All the Gallery APIs can be accessed via Media.Gallery property.

Open native UI to pick items from device's Gallery.

Media.Gallery.Pick(PickFromGalleryCallback)

This method will be called after user picked items, close the UI and return to the game.

private void PickFromGalleryCallback(string error, MediaResult[] results)
{
    if (!string.IsNullOrEmpty(error))
    {
        // This means there was an error when picking items from Gallery.
        // You should show this error to users. 
    }
    else
    {
        // Items have been selected successfully.
        // You can access them through the "results" parameter.

        // Loop through all the results.
        foreach(MediaResult result in results)
        {
            // You can use this field to check if the picked item is an image or a video.
            MediaType type = result.Type;

            // You can use this uri to load the item.
            string uri = result.Uri;
        }
    }
}

Specific the name you want the image to be saved with.

string name = "Dummy Image";

Choose format to save the image. You can choose between JPG and PNG.

ImageFormat imageFormat = ImageFormat.JPG;

Save the image into the device.

Media.Gallery.SaveImage(image, name, imageFormat, SaveImageCallback);

This method will be called after the image is saved into your device or if an error occurred.

private void SaveImageCallback(string error)
{
    if (!string.IsNullOrEmpty(error))
    {
        // There was an error, show it to users. 
    }
    else
    {
        // The image's saved successfully. 
    }
}

Requesting Android Permissions

On Android 6.0 and newer platforms which support runtime permissions, you can check and request the android.permission.WRITE_EXTERNAL_STORAGE permission if they're not granted before saving images to the device gallery.

Unity has provided useful APIs for checking and requesting Android permissions in the Permission class of their UnityEngine.Android namespace.

// Check if android.permission.WRITE_EXTERNAL_STORAGE has been granted and request it if not.
// Similar logic can be applied for other permissions.
if (Permission.HasUserAuthorizedPermission("android.permission.WRITE_EXTERNAL_STORAGE"))
{
    // The permission has been granted!
    // Here we can proceed with saving images to the gallery...
}
else
{
    // android.permission.WRITE_EXTERNAL_STORAGE has not been granted,
    // we need to ask for this permission before saving images.
    Permission.RequestUserPermission("android.permission.WRITE_EXTERNAL_STORAGE");

    // Note that the permission won't be granted immediately, so a simple approach
    // is to start a coroutine and keep checking 
    // Permission.HasUserAuthorizedPermission("android.permission.WRITE_EXTERNAL_STORAGE")
    // until it returns true before proceeding with your actions.
}

Loading Image from MediaResult

First of all note that the MediaResult you want to load image from needs to has Type == MediaType.Image, otherwise the loading method won't work.

You can load the image at a specific maximum size.

// 'result' is a MediaResult object obtained from taking pictures or picking images from gallery.
int maxSize = 1024;
Media.Gallery.LoadImage(result, LoadImageCallback, maxSize);

Or you can load the image at its original size.

Media.Gallery.LoadImage(result, LoadImageCallback);

This callback will be invoked after the image is loaded or if an error occurred.

private void LoadImageCallback(string error, Texture2D image)
{
    if (!string.IsNullOrEmpty(error))
    {
        // There was an error, show it to users. 
    }
    else
    {
        // The image's loaded successfully.
        // Now you can access it via the "image" parameter. 
    }
}

results matching ""

    No results matching ""