Compose Tweet in Unity with specific Image



Hello! I would like to compose a Tweet but instead of using a screenshot I would like to provide my own Image (Texture2D) Is there a way to achieve that? I’ve my png file in the Resources folder of the project but I can’t get it’s path to apply it to imageUri.

Thank you


Can you paste some example code?


Hello Adam,

I’m actually using the same code of the demo:

public void LoginCompleteWithCompose(TwitterSession session) {
		UnityEngine.Debug.Log ("Screenshot location=" + Application.persistentDataPath + "/Screenshot.png");
        string imageUri = "file://" + Application.persistentDataPath + "/Screenshot.png";
        Twitter.Compose (session, imageUri, "Welcome to", new string[]{"#TwitterKitUnity"},
			(string tweetId) => { UnityEngine.Debug.Log ("Tweet Success, tweetId=" + tweetId); },
			(ApiError error) => { UnityEngine.Debug.Log ("Tweet Failed " + error.message); },
			() => { Debug.Log ("Compose cancelled"); }

My question is how should I modified this code (probably the path to the screenshot.png) so I can use my own custom PNG instead of the screenshot.

I’ve tried:

string imageUri = "file://" + Application.persistentDataPath + "/Assets/Resources/myImage.png";

But It didn’t work.
It is not possible to pass the Texture directly instead of the path of the texture?

Thank you for your help


It is not possible to pass the Texture directly instead of the path of the texture?

You will need to pass the path of the png since the Composer requires a URI.

In Unity you need to first confirm that is the correct URI (path) to your png file.
I.e., using that path are you able to load the texture like this?


Hello Adam,

Thank you very much for your answer. You put me in the right track and now is working!
This is how I’ve made it:

public void MyPNG()
        // Create a new texture (we need to know the size of our image)
        Texture2D tex = new Texture2D(512, 512, TextureFormat.BGRA32, false);

        // Assign my Texture to tex
        tex = twitterImage;

        // Encode texture into PNG
        var bytes = tex.EncodeToPNG();

        // Write to a file in the project folder
        File.WriteAllBytes(Application.persistentDataPath + "/../myImage.png", bytes);

And now I just need to pass the Uri: Application.persistentDataPath + “/…/myImage.png”


Where did you find your demo code and does it work for PC game or just mobile version ?


Any news about this issue?

@rodlaiz, could you fix your issue?