|
1 |
| -using System.Collections; |
2 |
| -using System.Collections.Generic; |
3 |
| -using UnityEngine; |
| 1 | +using UnityEngine; |
| 2 | +using UnityEngine.UI; |
| 3 | +using System.Collections; |
4 | 4 |
|
5 | 5 | public class NativeScreenshotShareUsingFileProvider : MonoBehaviour
|
6 | 6 | {
|
7 |
| - // Start is called before the first frame update |
8 |
| - void Start() |
9 |
| - { |
10 |
| - |
11 |
| - } |
12 |
| - |
13 |
| - // Update is called once per frame |
14 |
| - void Update() |
15 |
| - { |
16 |
| - |
17 |
| - } |
| 7 | + public Button shareButton; |
| 8 | + |
| 9 | + private bool isFocus = false; |
| 10 | + |
| 11 | + private string shareSubject, shareMessage; |
| 12 | + private bool isProcessing = false; |
| 13 | + private string screenshotName; |
| 14 | + |
| 15 | + void Start () { |
| 16 | + shareButton.onClick.AddListener (OnShareButtonClick); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + void OnApplicationFocus (bool focus) { |
| 21 | + isFocus = focus; |
| 22 | + } |
| 23 | + |
| 24 | + public void OnShareButtonClick () { |
| 25 | + |
| 26 | + screenshotName = "fireblock_highscore.png"; |
| 27 | + shareSubject = "I challenge you to beat my high score in Fire Block"; |
| 28 | + shareMessage = "I challenge you to beat my high score in Fire Block. " + |
| 29 | + ". Get the Fire Block app from the link below. \nCheers\n" + |
| 30 | + "\nhttp://onelink.to/fireblock"; |
| 31 | + |
| 32 | + ShareScreenshot (); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + private void ShareScreenshot () { |
| 37 | + |
| 38 | + #if UNITY_ANDROID |
| 39 | + if (!isProcessing) { |
| 40 | + StartCoroutine (ShareScreenshotInAnroid ()); |
| 41 | + } |
| 42 | + |
| 43 | + #else |
| 44 | + Debug.Log("No sharing set up for this platform."); |
| 45 | + #endif |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + #if UNITY_ANDROID |
| 51 | + public IEnumerator ShareScreenshotInAnroid () { |
| 52 | + |
| 53 | + isProcessing = true; |
| 54 | + // wait for graphics to render |
| 55 | + yield return new WaitForEndOfFrame (); |
| 56 | + |
| 57 | + string screenShotPath = Application.persistentDataPath + "/" + screenshotName; |
| 58 | + ScreenCapture.CaptureScreenshot (screenshotName, 1); |
| 59 | + yield return new WaitForSeconds (0.5f); |
| 60 | + |
| 61 | + if (!Application.isEditor) { |
| 62 | + //current activity context |
| 63 | + AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); |
| 64 | + AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity"); |
| 65 | + |
| 66 | + //Create intent for action send |
| 67 | + AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent"); |
| 68 | + AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent"); |
| 69 | + intentObject.Call<AndroidJavaObject> ("setAction", intentClass.GetStatic<string> ("ACTION_SEND")); |
| 70 | + |
| 71 | + //old code which is not allowed in Android 8 or above |
| 72 | + //create image URI to add it to the intent |
| 73 | + //AndroidJavaClass uriClass = new AndroidJavaClass ("android.net.Uri"); |
| 74 | + //AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject> ("parse", "file://" + screenShotPath); |
| 75 | + |
| 76 | + //create file object of the screenshot captured |
| 77 | + AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", screenShotPath); |
| 78 | + |
| 79 | + //create FileProvider class object |
| 80 | + AndroidJavaClass fileProviderClass = new AndroidJavaClass("android.support.v4.content.FileProvider"); |
| 81 | + |
| 82 | + object[] providerParams = new object[3]; |
| 83 | + providerParams[0] = currentActivity; |
| 84 | + providerParams[1] = "com.agrawalsuneet.unityclient.provider"; |
| 85 | + providerParams[2] = fileObject; |
| 86 | + |
| 87 | + //instead of parsing the uri, will get the uri from file using FileProvider |
| 88 | + AndroidJavaObject uriObject = fileProviderClass.CallStatic<AndroidJavaObject>("getUriForFile", providerParams); |
| 89 | + |
| 90 | + //put image and string extra |
| 91 | + intentObject.Call<AndroidJavaObject> ("putExtra", intentClass.GetStatic<string> ("EXTRA_STREAM"), uriObject); |
| 92 | + intentObject.Call<AndroidJavaObject> ("setType", "image/png"); |
| 93 | + intentObject.Call<AndroidJavaObject> ("putExtra", intentClass.GetStatic<string> ("EXTRA_SUBJECT"), shareSubject); |
| 94 | + intentObject.Call<AndroidJavaObject> ("putExtra", intentClass.GetStatic<string> ("EXTRA_TEXT"), shareMessage); |
| 95 | + |
| 96 | + //additionally grant permission to read the uri |
| 97 | + intentObject.Call<AndroidJavaObject> ("addFlags", intentClass.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION") ); |
| 98 | + |
| 99 | + AndroidJavaObject chooser = intentClass.CallStatic<AndroidJavaObject> ("createChooser", intentObject, "Share your high score"); |
| 100 | + currentActivity.Call ("startActivity", chooser); |
| 101 | + } |
| 102 | + |
| 103 | + yield return new WaitUntil (() => isFocus); |
| 104 | + isProcessing = false; |
| 105 | + } |
| 106 | + #endif |
18 | 107 | }
|
0 commit comments