分享

从Unity Game在Android应用程序上共享图像

 印度阿三17 2019-10-12

玩家死亡时,我已经在游戏中捕获了屏幕截图.我正在按照以下代码捕获屏幕快照.

RenderTexture rt = new RenderTexture (800, 600, 24);
    MainCamera.targetTexture = rt;
    Texture2D texture = new Texture2D (800, 600, TextureFormat.RGB24, false);
    MainCamera.Render ();
    RenderTexture.active = rt;
    texture.ReadPixels (new Rect (0, 0, 800, 600), 0, 0);
    MainCamera.targetTexture = null;
    RenderTexture.active = null;
    Destroy (rt);
    byte[] bytes = texture.EncodeToPNG ();
    Directory.CreateDirectory (Application.persistentDataPath   "/GameOverScreenShot");
    File.WriteAllBytes (Application.persistentDataPath   "/GameOverScreenShot"   "/DiedScreenShot.png", bytes);

我正在使用以下代码保存屏幕截图.

byte[] bytes = File.ReadAllBytes (Application.persistentDataPath  "/GameOverScreenShot"   "/BirdDiedScreenShot.png");

Texture2D texture = new Texture2D (800, 600, TextureFormat.RGB24, false);
RectOffset tempOffset = new RectOffset (5, 5, 5, 5);
texture.filterMode = FilterMode.Trilinear;
texture.LoadImage (bytes);
Sprite sprite = Sprite.Create (texture, new Rect (0, 0, 800, 400), new Vector2 (0.5f, 0.0f), 2.0f);
ScreenShot_Image.GetComponent<Image> ().sprite = sprite;

现在,我想在Android应用程序上共享此屏幕截图.根据我的研究,我已经获得了以下代码,但是它返回的是空白图像.

//instantiate the class Intent
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");

//instantiate the object Intent
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

//call setAction setting ACTION_SEND as parameter
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));

//instantiate the class Uri
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");

//instantiate the object Uri with the parse of the url's file
string destination = Application.persistentDataPath   "/GameOverScreenShot"   "/DiedScreenShot.png";
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" destination);

//call putExtra with the uri object of the file
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);

//set the type of file
intentObject.Call<AndroidJavaObject>("setType", "image/*");

//instantiate the class UnityPlayer
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

//instantiate the object currentActivity
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

//call the activity with our Intent
currentActivity.Call("startActivity", intentObject);

在这方面我应该改变什么?请帮助,提前谢谢

解决方法:

只需调用takeScreenShotAndShare()即可截屏并共享它.如果您已经有要共享的图像,只需调用StartCoroutine(shareScreenshot(path));.并传递图像的路径/位置.这仅支持png图像.要共享jpeg,请更改

intentObject.Call<AndroidJavaObject>("setType", "image/png");

intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");

整个代码:

void takeScreenShotAndShare()
{
    StartCoroutine(takeScreenshotAndSave());
}

private IEnumerator takeScreenshotAndSave()
{
    string path = "";
    yield return new WaitForEndOfFrame();

    Texture2D screenImage = new Texture2D(Screen.width, Screen.height);

    //Get Image from screen
    screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    screenImage.Apply();

    //Convert to png
    byte[] imageBytes = screenImage.EncodeToPNG();


    System.IO.Directory.CreateDirectory(Application.persistentDataPath   "/GameOverScreenShot");
    path = Application.persistentDataPath   "/GameOverScreenShot"   "/DiedScreenShot.png";
    System.IO.File.WriteAllBytes(path, imageBytes);

    StartCoroutine(shareScreenshot(path));
}

private IEnumerator shareScreenshot(string destination)
{
    string ShareSubject = "Picture Share";
    string shareLink = "Test Link"   "\nhttps:///questions/36512784/share-image-on-android-application-from-unity-game";
    string textToShare = "Text To share";

    Debug.Log(destination);


    if (!Application.isEditor)
    {

        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"), textToShare   shareLink);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), ShareSubject);
        intentObject.Call<AndroidJavaObject>("setType", "image/png");
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        currentActivity.Call("startActivity", intentObject);
    }
    yield return null;
}
来源:https://www./content-1-504951.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多