Forgot password?
 立即注册
EasyAR Portal Tutorial View Content

EasyAR Developing Skills----Screenshot

AlbertLee 2017-9-8 15:55

EasyAR Developing Skills----Screenshot

Screenshot is a common function for AR App. Like screen-recording, they both are to save wonderful AR moments and send them to the circle of friends online. Here I introduce three screenshots methods for your reference.


Step 1:Development Preparation

We develop screenshot functions based on HelloAR. Download HelloAR Sample from official website and import it into Unity.



Open HelloARscene and fill in Key. Design UI. We still use that image that are displayed in screenshot

 


We create "GameObject" like that:



Step 2:Screenshot 1

We write a script "GameCon" in "GameObject". For the first screenshot method, we use the most traditional and simplest way in Unity:

CaptureScreenshot()

    public void Capture()
    {
        Application.CaptureScreenshot ("ScreenShot.png",0);
    }

Put this script on Button. Click on Button and save screenshot and name it “ScreenShot.png”. It can be saved in our project file so we check it in our computer. The saving path is mobile device (such Android platform) is:

/sdcard/Android/data/your app package information. My saving path is

/sdcard/Android/data/com.mars.jieping: You can find saved screenshots in files. Of course, you can also define your own saving path.

PS: It is very important for us to know screenshot-saved path, especially for sharing screenshots to the circle of friends online.




This method is very simple and only a line of script is enough. However, it has disadvantage that your UI also is in screenshot. So it is not desired experience for users.

Step 3:Screenshot Plug-in

We use screenshot pug-in: GalleryScreenshot. It is very popular plug-in especially for Android iOS. It is used in many AR development.


LINK: https://drive.google.com/open?id=0B6vjKDkZUGVsWDQtUHcyb3VLUms

We import this plug-in into Unity. Here the most important script is "GalleryScreenshotExample.cs" for screenshot. The original script is run by OnGUI. Let’s modify it and put screenshot script on Button


public void Capture()
    {
        StartCoroutine (ScreenshotManager.Save ("MyScreenshot""MyApp"true));
    }

The screenshot is saved in album after being tested so this method is very convenient. But its advantage lies in screenshot all.


Step 4:The most perfect screenshot method

The screenshot methods we talked above are very easy but users can’t get desired experience. The third screenshot is the best solution. We delete UI from camera so we can save the screenshot what we see in ARCamera.

We achieve this function based on "GameCon". Create a method:

void CaptureCamera(Camera camera, Rect rect) 
    {
        
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);

        camera.targetTexture = rt;
        camera.Render();

        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
        screenShot.ReadPixels(rect, 00);
        screenShot.Apply();

        camera.targetTexture = null;

        RenderTexture.active = null
        GameObject.Destroy(rt);
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/Screenshot.png";
        System.IO.File.WriteAllBytes(filename, bytes);
    }

Import two parameters:

1.Camera layer that needs screenshot

2.Screen size

First, define Camera in the beginning:

public Camera camera;

Write a method to use it:


public void Capture2()
    {
        CaptureCamera (camera,new RectScreen.width*0fScreen.height*0fScreen.width*1fScreen.height*1f));

    }

Assign ARCamera in Unity:


You will find no UI longer after testing. It is wonderful experience for users. Of course, there are many screenshot methods. You can leave your comments here if you have any good ideas!

We achieved the most two functions in AR development by EasyAR:



We are going to achieve Share function(to social platform online) in our next tutorials.