Wondering, what are the options if I want to share my current game screenshot?
I would like to generate/make screenshot of game scene or specific layer and then save it to .png.
Hey Alex
You can use a CCRenderTexture and the Visit method of you layer to create a texture. You can also just call Visit on some of you nodes in the layer if you do not want to share the entire layer. To create the image as stream you can do the following: public void Share(CCLayer layer) { CCRenderTexture rt = new CCRenderTexture(VisibleBoundsWorldspace.Size, VisibleBoundsWorldspace.Size, CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8); rt.BeginWithClear(CCColor4B.Transparent); layer.Visit(); rt.End(); rt.Sprite.Position = VisibleBoundsWorldspace.Center; CCRenderCommand shareCommand = new CCCustomCommand(() => { using (MemoryStream ms = new MemoryStream()) { rt.Texture.SaveAsPng(ms, (int)layer.VisibleBoundsWorldspace.Size.Width, (int)layer.VisibleBoundsWorldspace.Size.Height); ShareControl.ShareImage(ms); } }); Renderer.AddCommand(shareCommand); }
This will give you a Stream which you can make into an image on your specific platform. That is what my ShareControl.ShareImage(ms); method does.
Take note that the SaveAsPng method on CCTexture2D objects is not very stable. It can be very slow and it will crash on a lot of Android devices. This is a bitmap memory issue if I remember correctly.
I have only implemented a share method for Android and iOS. If someone has a good solution for Windows Phone I would love to see it. My static ShareControl class looks like this:
#if (__IOS__)
using UIKit;
using Foundation;
#endif
#if (__ANDROID__)
using Android.Content;
using PuzzleRektAndroid;
#endif
public static class ShareControl
{
public static void ShareImage(Stream stream)
{
stream.Position = 0;
#if (__IOS__)
var img = NSData.FromStream(stream);
UIActivityViewController activityVC = new UIActivityViewController(new NSObject[] { img }, null);
if (activityVC.PopoverPresentationController != null)
activityVC.PopoverPresentationController.SourceView = UIApplication.SharedApplication.KeyWindow.RootViewController.PresentingViewController.View;
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityVC, true, null);
#endif
#if (__ANDROID__)
var drawable = Android.Graphics.Drawables.Drawable.CreateFromStream(stream, "srcName");
Android.Graphics.Bitmap bitmap = ((Android.Graphics.Drawables.BitmapDrawable)drawable).Bitmap;
String path = Android.Provider.MediaStore.Images.Media.InsertImage(MainActivity.Self.ContentResolver, bitmap, "title", null);
Android.Net.Uri screenshotUri = Android.Net.Uri.Parse(path);
Intent share = new Intent(Intent.ActionSend);
share.SetType("*/*");
share.PutExtra(Intent.ExtraText, "Some text you want to share");
share.PutExtra(Intent.ExtraStream, screenshotUri);
MainActivity.Self.StartActivity(Intent.CreateChooser(share, "Share your game image"));
#endif
}
}
This will ask the user by what means they want to share the image. They choose by all the apps that support image sharing like Facebook, Twitter and mail.
When I try to share an image from my game, I can present the activity selector and share, but when I come back to my game (either by sharing, or pressing Cancel), the game graphics is not updating anymore. Music is playing, and I can press buttons (I have sound connected to my buttons so I can hear them), but UI is totally frozen. I've tested on iOS simulator and on a real iPhone 5S.
Thanks @AlexS , that means there's hope
What version of CocosSharp did you manage to make this work in? Both iOS and Android?
If you have a working sample I would very much appreciate it (then I could try to spot the diff with my non-working app).
@JonJarn said:
When I try to share an image from my game, I can present the activity selector and share, but when I come back to my game (either by sharing, or pressing Cancel), the game graphics is not updating anymore. Music is playing, and I can press buttons (I have sound connected to my buttons so I can hear them), but UI is totally frozen. I've tested on iOS simulator and on a real iPhone 5S.
I'm facing this, for example, choose whatsapp to share,from there I return to my application through the back button,
and so it occurs the same reported, Has anyone managed to solve this?
Posts
Hey Alex
You can use a
CCRenderTexture
and theVisit
method of you layer to create a texture. You can also just callVisit
on some of you nodes in the layer if you do not want to share the entire layer. To create the image as stream you can do the following:public void Share(CCLayer layer) { CCRenderTexture rt = new CCRenderTexture(VisibleBoundsWorldspace.Size, VisibleBoundsWorldspace.Size, CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8); rt.BeginWithClear(CCColor4B.Transparent); layer.Visit(); rt.End(); rt.Sprite.Position = VisibleBoundsWorldspace.Center; CCRenderCommand shareCommand = new CCCustomCommand(() => { using (MemoryStream ms = new MemoryStream()) { rt.Texture.SaveAsPng(ms, (int)layer.VisibleBoundsWorldspace.Size.Width, (int)layer.VisibleBoundsWorldspace.Size.Height); ShareControl.ShareImage(ms); } }); Renderer.AddCommand(shareCommand); }
This will give you a
Stream
which you can make into an image on your specific platform. That is what myShareControl.ShareImage(ms);
method does.Take note that the
SaveAsPng
method onCCTexture2D
objects is not very stable. It can be very slow and it will crash on a lot of Android devices. This is a bitmap memory issue if I remember correctly.Amazing, exactly what I was looking for.
Btw, what are you using for your sharing control?
I have only implemented a share method for Android and iOS. If someone has a good solution for Windows Phone I would love to see it. My static ShareControl class looks like this:
This will ask the user by what means they want to share the image. They choose by all the apps that support image sharing like Facebook, Twitter and mail.
When I try to share an image from my game, I can present the activity selector and share, but when I come back to my game (either by sharing, or pressing Cancel), the game graphics is not updating anymore. Music is playing, and I can press buttons (I have sound connected to my buttons so I can hear them), but UI is totally frozen. I've tested on iOS simulator and on a real iPhone 5S.
@PeterKrusager or @AlexS Are you seeing this problem as well?
Any hints, anyone?
(I'm using the code above from Peter)
I really wanted to know how to do this in android, so I'll try it soon
To Share in Windows Phone you can do something like this:
@JonJarn late answer but I have not seen such a problem.
Thanks @AlexS , that means there's hope
What version of CocosSharp did you manage to make this work in? Both iOS and Android?
If you have a working sample I would very much appreciate it (then I could try to spot the diff with my non-working app).
I ended up using 1.6.2 or 1.6.1. Don't have a sample to share with you, unfortunately.
I'm facing this, for example, choose whatsapp to share,from there I return to my application through the back button,
and so it occurs the same reported, Has anyone managed to solve this?
Thanks