Make a game scene screenshot using CocosSharp?

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.

Thank you in advance

Posts

  • PeterKrusagerPeterKrusager DKMember ✭✭
    edited October 2015

    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.

  • AlexSAlexS USUniversity ✭✭✭

    Amazing, exactly what I was looking for.
    Btw, what are you using for your sharing control?

  • PeterKrusagerPeterKrusager DKMember ✭✭

    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.

  • JonJarnJonJarn SEMember
    edited July 2016

    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)

  • angerurothangeruroth USMember

    I really wanted to know how to do this in android, so I'll try it soon :smile:

    To Share in Windows Phone you can do something like this:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // This code in the initialization method, even if it's not OnNavigatedTo
        DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
        dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
    }
    
    private async void DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
        DataRequest request = args.Request;
        request.Data.Properties.Title = "Title";
        request.Data.Properties.ApplicationName = "AppName";
        request.Data.SetText("Text Message");
        DataRequestDeferral deferral = request.GetDeferral();
    
        // Make sure we always call Complete on the deferral.
        try
        {
            StorageFolder installFolder = ApplicationData.Current.RoamingFolder;
            StorageFile shareImage = await installFolder.GetFileAsync("fileToShare.jpg");
    
            List<IStorageItem> storageItems = new List<IStorageItem>();
            storageItems.Add(shareImage);
            request.Data.SetStorageItems(storageItems);
        }
        finally
        {
            deferral.Complete();
        }
    }
    
    // Call it with
    DataTransferManager.ShowShareUI();
    
  • AlexSAlexS USUniversity ✭✭✭

    @JonJarn late answer but I have not seen such a problem.

  • JonJarnJonJarn SEMember

    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).

  • AlexSAlexS USUniversity ✭✭✭

    I ended up using 1.6.2 or 1.6.1. Don't have a sample to share with you, unfortunately.

  • Junior_OliveiraJunior_Oliveira USMember ✭✭

    @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.

    @PeterKrusager or @AlexS Are you seeing this problem as well?

    Any hints, anyone?

    (I'm using the code above from Peter)

    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

Sign In or Register to comment.