Example #1
0
    //IEnumerator 是所有非泛型列舉值的基底介面。
    public static IEnumerator Save(string fileName, Action successCallback, Action failCallbacks, string albumName = "MyScreenshots", bool callback = false)
    {
        bool photoSaved = false;

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

        ScreenShotNumber++;

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

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

#if UNITY_IOS
        Rect screenArea = new Rect(0, 0, Screen.width, Screen.height);
        Instance.StartCoroutine(Instance.IOSScreenshot(albumName, fileName, screenArea, Done => {
            if (Done == SaveStatus.SAVED)
            {
                successCallback();
            }
            else
            {
                failCallbacks();
            }
        }));
        yield return(new WaitForSeconds(.5f));
#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);
            ScreenCapture.CaptureScreenshot(androidPath);
            ImagePath = androidPath;

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

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


                yield return(new WaitForSeconds(.5f));
            }
        }
        else
        {
            ScreenCapture.CaptureScreenshot(screenshotFilename);
        }
#else
        while (!photoSaved)
        {
            yield return(new WaitForSeconds(.5f));

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

            photoSaved = true;
        }
#endif
#if UNITY_IOS
#elif UNITY_ANDROID
        if (callback)
        {
            if (photoSaved)
            {
                successCallback();
            }
            else
            {
                failCallbacks();
            }
        }
#endif
        GameObject.Find("Manager").GetComponent <AddPicture>().PickImage2(screenshotFilename);
        print("photo");
    }