- Home /
How can i make a ScreenShot and Share button in unity for AR(ARCORE)
I am making an AR app. I just want to add a share button before publishing it.. but I can't I tried so many methods but none work for AR(ARCORE) Plz tell me how to do it. I even bought some plugins but they don't seem to work in AR. ANY help will be appreciated.,I am really stuck on this part I have to publish today so plz help me out ASAP. ,
Answer by petarpejovic · Dec 14, 2018 at 02:20 PM
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class ShareScreenshot : MonoBehaviour {
private bool isProcessing = false;
public float startX;
public float startY;
public int valueX;
public int valueY;
public void shareScreenshot(){
if(!isProcessing && GameObject.Find("Ask Friend").GetComponent<Image>().enabled)
StartCoroutine( captureScreenshot() );
}
public IEnumerator captureScreenshot(){
isProcessing = true;
yield return new WaitForEndOfFrame();
Texture2D screenTexture = new Texture2D(Screen.width*valueX/10000, Screen.height*valueY/10000,TextureFormat.RGB24,true);
// put buffer into texture
//screenTexture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height),0,0);
//create a Rect object as per your needs.
screenTexture.ReadPixels(new Rect
(Screen.width*startX, (Screen.height*startY), Screen.width*valueX/10000, Screen.height*valueY/10000),0,0);
// apply
screenTexture.Apply();
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PHOTO
//byte[] dataToSave = Resources.Load<TextAsset>("everton").bytes;
byte[] dataToSave = screenTexture.EncodeToPNG();
string destination = Path.Combine(Application.persistentDataPath,System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
File.WriteAllBytes(destination, dataToSave);
if(!Application.isEditor)
{
// block to open the file and share it ------------START
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), "Which Football Club does this Rebus represent?\n" +
"Download the game on play store at "+"\nhttps://play.google.com/store/apps/details?id=com.TGC.RebusFC&pcampaignid=GPC_shareGame");
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), "Which club is this?");
intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
// option one WITHOUT chooser:
currentActivity.Call("startActivity", intentObject);
// block to open the file and share it ------------END
}
isProcessing = false;
}
}
Create a C# script named ShareScreenshot and paste the below code to it:
Attach the script to Main Camera or any object that is present on the scene.
Call the shareScreenshot() method on click of a button.
HI, Thanks for ur reply I tried this but nothing happens when the button is pressed. I called the on click as I said but nothing happens .. Have u tried this in a AR Project?
Gives out this error
NullReferenceException: Object reference not set to an instance of an object ShareScreenshot.shareScreenshot () (at Assets/_SCRIPTS/ShareScreenshot.cs:20)
Try to delete && GameObject.Find("Ask Friend").GetComponent().enabled. It should work then. I forgot to delete it. Let me know if it works :)
I'll be home soon to try it... have u tried this before in AR?
Your answer
Follow this Question
Related Questions
How to make Glass 0 Answers
Detect system requirements 1 Answer
How to make a flame particle? 1 Answer
a few questions 2 Answers
how to remake a terrain flat ? 1 Answer