Posted on

Media Library – Cross Platform Native Plugins: Essential Kit

Download

Documentation: https://assetstore.essentialkit.voxelbusters.com/

0:00 Welcome to Media Library CPNP2
0:20 Overview
0:52 Setup
1:15 Usage

For this lesson on how to use the Cross Platform Native Plugin in Unity, I will show you how to use the Media Library and Address Book services. The Media Library is a great thing to add to your game as it allows the user to take pictures with their camera or they can pick photos from their gallery. These photos are then turned into a Texture2D and so you can use it in Unity however you would like.

IG_MediaService.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBusters.EssentialKit;

public class IG_MediaService : MonoBehaviour
{
    public static IG_MediaService instance;

    GalleryAccessStatus readAccessStatus;
    GalleryAccessStatus readWriteAccessStatus;
    CameraAccessStatus cameraAccessStatus;

    public Texture2D currentImage; //IG_MediaService.instance.currentImage;

    // Start is called before the first frame update
    void Start()
    {
        instance = this;
    }

    //IG_MediaService.instance.GetImageFromGallery();
    public void GetImageFromGallery()
    {
        readAccessStatus = MediaServices.GetGalleryAccessStatus(GalleryAccessMode.Read);

        if (readAccessStatus == GalleryAccessStatus.NotDetermined)
        {

            MediaServices.RequestGalleryAccess(GalleryAccessMode.Read, callback: (result, error) =>
            {
                Debug.Log("Request for gallery access finished.");
                Debug.Log("Gallery access status: " + result.AccessStatus);
            });
        }
        if(readAccessStatus == GalleryAccessStatus.Authorized)
        {
            MediaServices.SelectImageFromGallery(canEdit: true, (textureData, error) =>
            {
                if (error == null)
                {
                    Debug.Log("Select image from gallery finished successfully.");
                    //textureData.GetTexture() // This returns the texture
                    currentImage = textureData.GetTexture();
                }
                else
                {
                    Debug.Log("Select image from gallery failed with error. Error: " + error);
                }
            });
            
        }
        
    }

    //IG_MediaService.instance.TakePictureWithCamera();
    public void TakePictureWithCamera()
    {
        cameraAccessStatus = MediaServices.GetCameraAccessStatus();

        if (cameraAccessStatus == CameraAccessStatus.NotDetermined)
        {
            MediaServices.RequestCameraAccess(callback: (result, error) =>
            {
                Debug.Log("Request for camera access finished.");
                Debug.Log("Camera access status: " + result.AccessStatus);
            });
        }
        if(cameraAccessStatus == CameraAccessStatus.Authorized)
        {
            MediaServices.CaptureImageFromCamera(true, (textureData, error) =>
            {
                if (error == null)
                {
                    Debug.Log("Capture image using camera finished successfully.");
                    currentImage = textureData.GetTexture();
                }
                else
                {
                    Debug.Log("Capture image using camera failed with error. Error: " + error);
                }
            });
        }
    }
    //IG_MediaService.instance.SaveImageToGallery(textureValue);
    public void SaveImageToGallery(Texture2D texture)
    {
        readWriteAccessStatus = MediaServices.GetGalleryAccessStatus(GalleryAccessMode.ReadWrite);

        if (readWriteAccessStatus == GalleryAccessStatus.NotDetermined)
        {
            MediaServices.RequestGalleryAccess(GalleryAccessMode.ReadWrite, callback: (result, error) =>
            {
                Debug.Log("Request for gallery access finished.");
                Debug.Log("Gallery access status: " + result.AccessStatus);
            });
        }
        if (readWriteAccessStatus == GalleryAccessStatus.Authorized)
        {
            MediaServices.SaveImageToGallery(texture, (result, error) =>
            {
                if (error == null)
                {
                    Debug.Log("Save image to gallery finished successfully.");
                }
                else
                {
                    Debug.Log("Save image to gallery failed with error. Error: " + error);
                }
            });
        }
    }
}