Portal belajar membuat aplikasi Augmented Reality dan Virtual Reality

Senin, 24 Oktober 2016

Screenshot Manager

03.22 Posted by Dicky Rachmat Iskandar No comments
#pragma warning disable 0168 // variable declared but not used.
#pragma warning disable 0219 // variable assigned but not used.


using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;

public class ScreenshotManager : MonoBehaviour
{

    public static event Action ScreenshotFinishedSaving;
    public static event Action ImageFinishedSaving;


#if UNITY_IPHONE

[DllImport("__Internal")]
    private static extern bool saveToGallery( string path );

#endif




    public void saveGallery()
    {
        StartCoroutine(Save("MyFolder", "MyFolder", true));
    }




    public static IEnumerator Save(string fileName, string albumName = "Humanoid4", bool callback = false)
    {
        bool photoSaved = false;

        string date = System.DateTime.Now.ToString("dd-MM-yy");

        ScreenshotManager.ScreenShotNumber++;

        string screenshotFilename = fileName + "_" + ScreenshotManager.ScreenShotNumber + "_" + date + ".png";

        Debug.Log("Save screenshot " + screenshotFilename);

#if UNITY_IPHONE

if(Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log("iOS platform detected");

string iosPath = Application.persistentDataPath + "/" + screenshotFilename;

Application.CaptureScreenshot(screenshotFilename);

while(!photoSaved)
{
photoSaved = saveToGallery( iosPath );

yield return new WaitForSeconds(.5f);
}

UnityEngine.iOS.Device.SetNoBackupFlag( iosPath );

} else {

Application.CaptureScreenshot(screenshotFilename);

}

#elif UNITY_ANDROID

        if (Application.platform == RuntimePlatform.Android)
        {
            Debug.Log("Android platform detected");

            string androidPath = "/../../../../DCIM/" + albumName + "/" + screenshotFilename;
            string path = Application.persistentDataPath + androidPath;
            string pathonly = Path.GetDirectoryName(path);
            Directory.CreateDirectory(pathonly);
            Application.CaptureScreenshot(androidPath);

            AndroidJavaClass obj = new AndroidJavaClass("com.ryanwebb.androidscreenshot.MainActivity");

            while (!photoSaved)
            {
                photoSaved = obj.CallStatic<bool>("scanMedia", path);

                yield return new WaitForSeconds(.5f);
            }

        }
        else
        {

            Application.CaptureScreenshot(screenshotFilename);

        }
#else

while(!photoSaved)
{
yield return new WaitForSeconds(.5f);

Debug.Log("Screenshots only available in iOS/Android mode!");

photoSaved = true;
}

#endif

        if (callback)
            ScreenshotFinishedSaving();
    }


    public static IEnumerator SaveExisting(string filePath, bool callback = false)
    {
        bool photoSaved = false;

        Debug.Log("Save existing file to gallery " + filePath);

#if UNITY_IPHONE

if(Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log("iOS platform detected");

while(!photoSaved)
{
photoSaved = saveToGallery( filePath );

yield return new WaitForSeconds(.5f);
}

UnityEngine.iOS.Device.SetNoBackupFlag( filePath );
}

#elif UNITY_ANDROID

        if (Application.platform == RuntimePlatform.Android)
        {
            Debug.Log("Android platform detected");

            AndroidJavaClass obj = new AndroidJavaClass("com.ryanwebb.androidscreenshot.MainActivity");

            while (!photoSaved)
            {
                photoSaved = obj.CallStatic<bool>("scanMedia", filePath);

                yield return new WaitForSeconds(.5f);
            }

        }

#else

while(!photoSaved)
{
yield return new WaitForSeconds(.5f);

Debug.Log("Save existing file only available in iOS/Android mode!");

photoSaved = true;
}

#endif

        if (callback)
            ImageFinishedSaving();



    }


    public static int ScreenShotNumber
    {
        set { PlayerPrefs.SetInt("screenShotNumber", value); }

        get { return PlayerPrefs.GetInt("screenShotNumber"); }
    }
}

Senin, 10 Oktober 2016

Unity Swipe to Rotate GameObject Script

22.54 Posted by Dicky Rachmat Iskandar No comments
using UnityEngine;
using System.Collections;

public class rotObj : MonoBehaviour 
{
    float rotSpeed = 20;

    void OnMouseDrag()
    {
        float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
        float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

        transform.RotateAround(Vector3.up, -rotX);
        transform.RotateAround(Vector3.right, rotY);
    }
}