Screenshot and share

Hello all,

I am struggling trying to implement a series of functions;

  1. Take a screenshot within my game (if a screenshot exists, then over write previous one)
  2. Show that screenshot on the UI splash screen (if no screenshot exists, use a default texture?)
  3. Share that screenshot on twitter (if no screenshot exists, use a default texture?)

I have found a 2 separate scripts which behave the way I want to and work, however they work independently

  1. The take screenshot script (ScreenshotHandler.cs) calls in the function TakeScreenshot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenshotHandler : MonoBehaviour {

    private static ScreenshotHandler instance;

    private Camera myCamera;
    private bool takeScreenshotOnNextFrame;

    private void Awake()
    {
        instance = this;
        myCamera = gameObject.GetComponent<Camera>();
    }

    private void OnPostRender()
    {
        if (takeScreenshotOnNextFrame) {
            takeScreenshotOnNextFrame = false;
            RenderTexture renderTexture = myCamera.targetTexture;

            Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
            Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
            renderResult.ReadPixels(rect, 0, 0);

            byte[] byteArray = renderResult.EncodeToPNG();

            System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot.png", byteArray);
            Debug.Log("Saved CameraScreenshot.png");

            //Clear up image
            RenderTexture.ReleaseTemporary(renderTexture);
            myCamera.targetTexture = null;
        }

    }

    private void TakeScreenshot(int width, int height) {
        myCamera.targetTexture = RenderTexture.GetTemporary(width, height, 16);
        takeScreenshotOnNextFrame = true;
    }

    public static void TakeScreenshot_Static(int width,int height) {

        instance.TakeScreenshot(width, height);
    }
}

However this saves it as a .png whereas the script below is able to generate a Texture that is not a .png

  1. The VoxelBustersManager.cs then shares an image on twitter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using VoxelBusters;
using VoxelBusters.NativePlugins;

public class VoxelBustersManager : MonoBehaviour
{

    public static VoxelBustersManager Instance;

    private bool isSharing = false;

    public void ShareSocialMedia()
    {
        isSharing = true;
    }

    void LateUpdate()
    {
        if (isSharing == true)
        {
            isSharing = false;

            StartCoroutine(CaptureScreenShoot());
        }
    }

    public IEnumerator CaptureScreenShoot()
    {
        yield return new WaitForEndOfFrame();

        Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

        ShareSheet(texture);

        Object.Destroy(texture);

    }

    private void ShareSheet(Texture2D texture)
    {
        ShareSheet _shareSheet = new ShareSheet();

        _shareSheet.Text = "Hello world!!!";
        _shareSheet.AttachImage(texture);
        _shareSheet.URL = "https://twitter.com/tweet";

        NPBinding.Sharing.ShowView(_shareSheet, FinishSharing);
    }

    private void FinishSharing(eShareResult _result)
    {
        Debug.Log(_result);
    }
}

Also I am not sure when I call in the function TakeScreenshot, how to show this in my UI Camera splash screen, which is 2)

Any help or pointing me in the right direction would be much appreciated!

kind Regards,

SLC

I would give up on the first script and focus on the second.

public class VoxelBustersManager : MonoBehaviour {

    public static VoxelBustersManager Instance;
    private bool isSharing = false;
    public Texture2D texture = null;  // drag default in Inpsector
    public void ShareSocialMedia()
    {
         isSharing = true;
    }
    void LateUpdate()
    {
         if (isSharing == true)
         {
             StartCoroutine(CaptureScreenShoot());
         }
    }
    public IEnumerator CaptureScreenShoot()
    {
         yield return new WaitForEndOfFrame();
         if(this.texture != null) { Texture2D.Destroy(this.texture);} // if already, remove
         this.texture = ScreenCapture.CaptureScreenshotAsTexture();
         isSharing = false;
    }
    public void ShareSheet()
    {
          if(this.texture == null) { Debug.Log("No image"); return; }
          ShareSheet _shareSheet = new ShareSheet();
          _shareSheet.Text = "Hello world!!!";
          _shareSheet.AttachImage(this.texture);
          _shareSheet.URL = "https://twitter.com/tweet";
          NPBinding.Sharing.ShowView(_shareSheet, FinishSharing);
     }
     private void FinishSharing(eShareResult _result)
     { ()
          Debug.Log(_result);
      }
}

What has changed: when taking the capture, you are not sending right away for tweet, it gets stored in the texture reference of the class. If there was already a texture, it removes it.

When calling the sharing method, if the texture is null, it won’t do anything. This is no likely to happen if you dragged a default in the inspector (but just in case).

I also moved the isSharing to be reset once the picture is actually taken and not when the process of taking it is started. Just a detail though.

The rest is much of the same.