Sharing: Scripting

This section provides a guide to work with Sharing module scripting API.

You can access the Sharing module API via the Sharing class under the EasyMobile namespace.

Screenshot Capturing

To capture the device's screenshot, you have a few options.

Screenshot as PNG Image

To capture and save a screenshot of the whole device screen, simply specify the file name to be saved. This screenshot will be saved as a PNG image in the directory pointed by Application.persistentDataPath. Note that this method, as well as other screenshot capturing methods, needs to be called at the end of a frame (when the rendering has done) for it to produce a proper image. Therefore you should call it within a coroutine after WaitForEndOfFrame().

// Coroutine that captures and saves a screenshot
IEnumerator SaveScreenshot()
{
    // Wait until the end of frame
    yield return new WaitForEndOfFrame();

    // The SaveScreenshot() method returns the path of the saved image
    // The provided file name will be added a ".png" extension automatically
    string path = Sharing.SaveScreenshot("screenshot");
}

You can also captures and saves just a portion of the screen:

// Coroutine that captures and saves a portion of the screen
IEnumerator SaveScreenshot()  
{  
    // Wait until the end of frame  
    yield return new WaitForEndOfFrame();

    // Capture the portion of the screen starting at (50, 50),
    // has a width of 200 and a height of 400 pixels.
    string path = Sharing.SaveScreenshot(50, 50, 200, 400, "screenshot");
}

Screenshot as Texture2D

In some cases you may want to capture a screenshot and obtain a Texture2D object of it instead of saving to disk, e.g. to create a sprite from the texture and display it in-game.

// Coroutine that captures a screenshot and generates a Texture2D object of it  
IEnumerator CaptureScreenshot()  
{  
    // Wait until the end of frame  
    yield return new WaitForEndOfFrame();

    // Create a Texture2D object of the screenshot using the CaptureScreenshot() method
    Texture2D texture = Sharing.CaptureScreenshot();
}

Similar to the case above, you can also capture only a portion of the screen.

// Coroutine that captures a portion of the screenshot and generates a Texture2D object of it  
IEnumerator CaptureScreenshot()  
{  
    // Wait until the end of frame  
    yield return new WaitForEndOfFrame();

    // Create a Texture2D object of the screenshot using the CaptureScreenshot() method
    // The captured portion starts at (50, 50) and has a width of 200, a height of 400 pixels.
    Texture2D texture = Sharing.CaptureScreenshot(50, 50, 200, 400);
}

Note that screenshot capturing should be done at the end of the frame.

Sharing

To share an image you also have a few options. You can also attach a message to be shared with the image.

Due to Facebook policy, pre-filled messages will be ignored when sharing to this network, i.e. sharing messages must be written by the user.

Share a Saved Image

You can share a saved image by specifying its path.

// Share a saved image
// Suppose we have a "screenshot.png" image stored in the persistentDataPath,
// we'll construct its path first
string path = System.IO.Path.Combine(Application.persistentDataPath, "screenshot.png");

// Share the image with the path, a sample message and an empty subject
Sharing.ShareImage(path, "This is a sample message");

Share a Texture2D

You can also share a Texture2D object obtained some point before the sharing time. Internally, this method will also create a PNG image from the Texture2D, save it to the persistentDataPath, and finally share that image.

// Share a Texture2D
// sampleTexture is a Texture2D object captured some time before
// This method saves the texture as a PNG image named "screenshot.png" in persistentDataPath,
// then shares it with a sample message and an empty subject
Sharing.ShareTexture2D(sampleTexture, "screenshot", "This is a sample message");

Share a Text

You can share a text-only message using the ShareText method. Note that Facebook doesn't allow pre-filled sharing messages, so the text will be discarded when sharing to this particular network.

// Share a text
Sharing.ShareText("Hello from Easy Mobile!");

Share a URL

To share a URL, use the ShareURL method. On networks like Facebook or Twitter, a summary of the page will be shown if the shared URL points to a website. URLs are also useful to share GIF images hosted on sites like Giphy (see the GIF > Scripting section).

// Share a URL
Sharing.ShareURL("www.sglibgames.com");

results matching ""

    No results matching ""