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();
    }

   
}
Posted on

Notifications – Cross Platform Native Plugins: Essential Kit

Download

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

0:00 Welcome to Notifications CPNP2
0:20 Overview
1:27 Setup
2:45 iOS
3:05 Android
4:29 Usage

For this lesson on how to use the Cross Platform Native Plugins 2 in Unity, I will show you how to use the Notification services. Notifications are important to have in your game as they will help in boosting audience retention. Notification can be used to remind your players to come back and keep playing.

IG_Notifications.cs

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

public class IG_Notifications : MonoBehaviour
{
    public static IG_Notifications instance;

    NotificationSettings settings;

    bool isRegistered;

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

        VoxelBusters.EssentialKit.NotificationServices.GetSettings((result) =>
        {
            settings = result.Settings;
            // update console
            Debug.Log(settings.ToString());

            Debug.Log("Permission status : " + settings.PermissionStatus);
        });

        if(!settings.PushNotificationEnabled)
        {
            VoxelBusters.EssentialKit.NotificationServices.UnregisterForPushNotifications();
            isRegistered = false;
        }
         
    }

    //IG_Notifications.instance.CreateLocalNotification("GoldMindFull", "Your gold minds are full!", 3600, false);
   public void CreateLocalNotification(string notificationID, string notificationTitle, double time, bool canRepeat)
    {
        if(settings.PermissionStatus == NotificationPermissionStatus.NotDetermined)
        {
            VoxelBusters.EssentialKit.NotificationServices.RequestPermission(NotificationPermissionOptions.Alert | NotificationPermissionOptions.Sound | NotificationPermissionOptions.Badge, callback: (result, error) =>
            {
                Debug.Log("Request for access finished.");
                Debug.Log("Notification access status: " + result.PermissionStatus);
            });
        }
        if(settings.PermissionStatus == NotificationPermissionStatus.Denied)
        {
            Utilities.OpenApplicationSettings();
        }
        if (settings.PermissionStatus == NotificationPermissionStatus.Authorized)
        {


            INotification notification = NotificationBuilder.CreateNotification(notificationID)
        .SetTitle(notificationTitle)
        .SetTimeIntervalNotificationTrigger(time, repeats: canRepeat)
        .Create();

            VoxelBusters.EssentialKit.NotificationServices.ScheduleNotification(notification, (error) =>
            {
                if (error == null)
                {
                    Debug.Log("Request to schedule notification finished successfully.");
                }
                else
                {
                    Debug.Log("Request to schedule notification failed with error. Error: " + error);
                }
            });
        }
    }


    private void OnEnable()
    {
        VoxelBusters.EssentialKit.NotificationServices.RegisterForPushNotifications((result, error) =>
        {   
            if (error == null)
            {   
                Debug.Log("Remote notification registration finished successfully. Device token: " + result.DeviceToken);
            }   
            else
            {
                Debug.Log("Remote notification registration failed with error. Error: " + error.Description);
            }
        });
        isRegistered = VoxelBusters.EssentialKit.NotificationServices.IsRegisteredForPushNotifications();
    }

    private void OnDisable()
    {
        if(isRegistered)
            VoxelBusters.EssentialKit.NotificationServices.UnregisterForPushNotifications();
    }
}