Posted on

Address Book – Cross Platform Native Plugins: Essential Kit

Download

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

0:00 Welcome to Address Book CPNP2
0:18 Overview
0:34 Setup
1:05 Usage

The Address Book allows the user to access their contacts inside your game. This can be helpful in sharing your game.

IG_AddressBookService.cs

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

public class IG_AddressBookService : MonoBehaviour
{
    public static IG_AddressBookService instance;

    AddressBookContactsAccessStatus status;

    public IAddressBookContact[] allContacts; //IG_AddressBookService.instance.allContacts[index];

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

    //IG_AddressBookService.instance.ReadContacts();
    public void ReadContacts()
    {
        status = AddressBook.GetContactsAccessStatus();
        if (status == AddressBookContactsAccessStatus.NotDetermined)
        {
            AddressBook.RequestContactsAccess(callback: OnRequestContactsAccessFinish);
        }
        if(status == AddressBookContactsAccessStatus.Authorized)
        {
            AddressBook.ReadContacts(OnReadContactsFinish);
        }
    }

    private void OnRequestContactsAccessFinish(AddressBookRequestContactsAccessResult result, Error error)
    {
        Debug.Log("Request for contacts access finished.");
        Debug.Log("Address book contacts access status: " + result.AccessStatus);
        if (result.AccessStatus == AddressBookContactsAccessStatus.Authorized)
        {
            AddressBook.ReadContacts(OnReadContactsFinish);
        }
    }

    private void OnReadContactsFinish(AddressBookReadContactsResult result, Error error)
    {
        if (error == null)
        {
            allContacts = result.Contacts;

            var contacts = result.Contacts;
            Debug.Log("Request to read contacts finished successfully.");
            Debug.Log("Total contacts fetched: " + contacts.Length);
            Debug.Log("Below are the contact details (capped to first 10 results only):");
            for (int iter = 0; iter < contacts.Length && iter < 10; iter++)
            {
                Debug.Log(string.Format("[{0}]: {1}", iter, contacts[iter]));
            }
        }
        else
        {
            Debug.Log("Request to read contacts failed with error. Error: " + error);
        }
    }
}
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);
                }
            });
        }
    }
}
Posted on

Native UI – Cross Platform Native Plugins: Essential Kit

Download

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

0:00 Welcome to Native UI CPNP2
0:22 Overview
0:56 Setup
1:09 Usage

Native UIs can be used to show special messages in your game with native Android or iOS windows.

IG_NativeUI.cs

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

public class IG_NativeUI : MonoBehaviour
{
    public static IG_NativeUI instance;

    public DateTime currentDateTime; //IG_NativeUI.instance.currentDateTime;
   
    // Start is called before the first frame update
    void Start()
    {
        instance = this;
    }

    //IG_NativeUI.instance.ShowAlertDialog("Are you sure?", "Would you like to proceed with this action?");
    public void ShowAlertDialog(string title, string message)
    {
        AlertDialog dialog = AlertDialog.CreateInstance();
        dialog.Title = title;
        dialog.Message = message;
        dialog.AddButton("Yes", () =>
        {
            Debug.Log("Yes button clicked");
        });
        dialog.AddCancelButton("No", () =>
        {
            Debug.Log("Cancel button clicked");
        });
        dialog.Show(); //Show the dialog
    }

    //IG_NativeUI.instance.ShowDatePicker();
    public void ShowDatePicker()
    { 
        DatePicker datePicker = DatePicker.CreateInstance(DatePickerMode.Date);
        datePicker.SetOnValueChange((date) =>
        {
            Debug.Log("Date Time change : " + date);
            currentDateTime = (DateTime)date;
        });
        datePicker.SetOnCloseCallback((result) =>
        {
            Debug.Log("Dismissed the picker with selected date : " + result.SelectedDate);
            currentDateTime = (DateTime)result.SelectedDate;
        });
        datePicker.Show();
    }

   
}