Tag Archives: Plugin

Using Android WebView to display a webpage on top of the Unity App view

Hello and welcome to my tutorial on how to show a WebView on top of your Android Unity App, while still allowing the user to interact with your Unity UI.

You can watch the video of this tutorial at https://youtu.be/r1hLo5C50wE.

This tutorial assumes a reasonable knowledge of Unity, C#, Android Studio and Java. The source code for this tutorial can be found at https://github.com/cwgtech/androidwebview.

The plan is to extend the plugin created in my previous tutorials by adding a method that will create an android layout containing an Android WebView object and a blank TextView. We’ll adjust the height of the TextView to create space at the top of the layout that will allow the user to still see and interact with a portion of the Unity viewspace. We’ll add this layout to our App’s content view which will place it on top of the Unity view.

We’ll also add a method to remove this layout from the content view, returning the full screen to Unity.

Get started by loading up the previous version of this project in Unity and the MyPlugin project in Android Studio. If you don’t have it, you can download it from https://github.com/cwgtech/AndroidActivityResult.

Using Android Studio, open the MyPlugin java source and add the following variable declarations above the first method definition:

private LinearLayout webLayout;
private TextView webTextView;
private WebView webView;

We’re going to use these vars to store references to the objects we create when the webview is displayed. This will allow the plugin to close and deallocate those objects when the webview is closed.

Add the following method to the body of the plugin:

public void showWebView(final String webURL, final int pixelSpace)
{
    mainActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Log.i(LOGTAG,"Want to open webview for " + webURL);
        if (webTextView==null)
            webTextView = new TextView(mainActivity);
        webTextView.setText("");
        if (webLayout==null)
            webLayout = new LinearLayout(mainActivity);
        webLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        mainActivity.addContentView(webLayout,layoutParams);
        if (webView==null)
            webView = new WebView(mainActivity);
        webView.setWebViewClient(new WebViewClient());
        layoutParams.weight = 1.0f;
        webView.setLayoutParams(layoutParams);
        webView.loadUrl(webURL);
        webLayout.addView(webTextView);
        webLayout.addView(webView);
        if (pixelSpace>0)
            webTextView.setHeight(pixelSpace);
        }
    });
}

The method showWebView takes two parameters, the URL of the webpage you want to display, and the number of screen pixels the layout needs to reserve for the Unity UI. This version assumes that the Unity UI is at the top of the screen and pushes the WebView down, you’ll need to modify the order the views are added to the layout if this is not what you want.

First, we create the TextView and set its contents to an empty string.

Next we create the LinearLayout and set its orientation to vertical, and its layout so that it will fill its parent object, and then add it to the ContentView for our activity.

Lastly, we create the actual WebView and we assign the WebViewClient to be a default WebViewClient. This tells our WebView how to handle links, and with the default client, the links will be opened in our WebView. Without this, when the user clicks on a link, Android will pop-up a chooser asking the user what app they want to send the link to.

We also set the weight of the WebView to 1, which means the layout system will give our WebView as much space as it can. Finally, we tell the WebView to load the URL passed to this method.

The TextView and WebView are added to the LinearLayout object, with the order they are added determining the order they will appear on screen, and the height of the TextView is set to the number of screen pixels we want to push the WebView down by.

We need to add one more method that will allow our Unity app to remove the Layout when it’s no longer needed. Add the following code:

public void closeWebView(final ShareImageCallback callback)
{
    mainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (webLayout!=null)
            {
                webLayout.removeAllViews();
                webLayout.setVisibility(View.GONE);
                webLayout = null;
                webView = null;
                webTextView = null;
                callback.onShareComplete(1);
            }
            else
                callback.onShareComplete(0);
        }
    });
}

This method is going to reuse the ShareImage callback interface. We could create a new interface just for this method, but there is no harm in using an existing interface that can do the same job, which is to let Unity know when we’ve closed the layout.

To remove the layout, first remove all it’s child views, then set it’s visibility state to GONE. This will cause it to remove itself from its parent and mark it for garbage collection. Setting the vars that hold the references to our views to NULL will also allow the garbage collection system to free the memory used by them.

Lastly, trigger the supplied callback passing a 1 if the close happened as expected, or a 0 if the LinearLayout had already been closed.

That completes the modifications to the plugin, so you can go ahead and let Gradle build it and copy the updated AAR to the Plugin folder in the Unity project.

Switch back to Unity where we’ll modify the canvas object in the hierarchy view to include a new layer for our WebView, but first double click the script PluginTest to open it in Visual Studio.
Add the following two lines to the C# code, after the other public UI vars:

public RectTransform webPanel;
public RectTransform buttonStrip;

These will hold references to the UI objects we’ll create later. The webPanel is the root UI object that will contain all the objects that will be displayed when the WebView is on screen, and buttonStrip holds the title text, and the close button.

Now add the following methods that will call our Java methods, but only if we’re on an Android platform:

public void OpenWebView(string url, int pixelShift)
{
    if (Application.platform == RuntimePlatform.Android)
    {
        PluginInstance.Call("showWebView", new object[] { url, pixelShift });
    }
}

public void CloseWebView(System.Action<int> closeComplete)
{
    if (Application.platform == RuntimePlatform.Android)
    {
        PluginInstance.Call("closeWebView", new object[] { new ShareImageCallback(closeComplete) });
    }
    else
        closeComplete(0);
}

These methods are just wrappers for the Java code and pass the parameters directly to the plugin.
Next, add the method we’ll connect to a UI button that will figure out how much space to reserve at the top of the display and then pass that with the URL to our Java wrapper.

public void OpenWebViewTapped()
{
    Canvas parentCanvas = buttonStrip.GetComponentInParent<canvas>();
    int stripHeight = (int)(buttonStrip.rect.height * parentCanvas.scaleFactor + 0.5f);
    webPanel.gameObject.SetActive(true);
    OpenWebView("http://www.cwgtech.com", stripHeight);
}

We get a reference to the Canvas object that our buttonStrip belongs to, and then use it’s scaling factor along with the height of our ButtonStrip to calculate how many screen pixels we need to push the webview down by. Enable the WebPanel and pass the URL and height to our Java wrapper.

Add the following method:

public void CloseWebViewTapped()
{
    CloseWebView((int result) =>
    {
        webPanel.gameObject.SetActive(false);
    });
}

This method will be connected to the close button child of the buttonStrip. It simply calls our Java wrapper, using the inline function to hide the WebPanel object once the Android views have been cleaned up and removed.

Save the file and return to Unity. Wait a few seconds to let Unity recompile the C# code and then expand the Canvas object.

Right click on the Canvas object, and select UI, then Button and left click. This will create a button in the middle of the screen called Button (1). Rename it to browseButton and expand it. Change the default text on the child Text object to ‘Browse’.

Highlight the browseButton again click the + button on the On Click list of the button script. Now drag the Main Camera object into the reference holder. Click the function selector, click PluginTest and then OpenWebViewTapped.

Right click on the Canvas object, then click UI, then Panel. Rename the panel created to WebPanel. Click on the color gizmo and set the color to pink (#FFD4F7) and alpha to 255.

Right click on WebPanel and then click UI, Image. Click on the Rect Transform gizmo, and select top center, horizontal stretch while hold shift and alt (option on mac). This will move the image to the top of the screen and make it fill the view horizontally. Set the height to 50 and the color to black. Rename the Image to ButtonStrip.

Right click on ButtonStrip and then UI, Text. Click the Rect Transform gizmo and set stretch for vertical and horizontal, while holding shift & alt (option on mac). This will cause the Text object to fill the image area. Set the font size to 28, the color to White, and the text to ‘Web Page View’.

Click the checkboxes to center the text both horizontally and vertically.

Right click on the ButtonStrip again and click UI,Button. Click the Rect Transform gizmo and then top right anchor with shift & alt held (option on mac). Set the width and height of the button to 24. Expand the button object and set the default text of the Text child to ‘X’. Rename the button to ‘CloseButton’.

Just like we did for the BrowseButton, connect the OnClick event for the CloseButton to the CloseWebViewTapped method of PluginTest.

Highlight the Main Camera, and in the PluginTest script object, drag the WebPanel object to the Web Panel holder and the ButtonStrip object to the Button Strip holder. Highlight the WebPanel object in the hierarchy and disable it.

Save and run the scene. If you click the Browse button, you’ll see the WebPanel appear and the close button will hide it. There will be no actual webview as we’re not yet running on an Android platform. Stop execution of the player.

Click ‘File’ and then ‘Build Settings’. Click on ‘Player Settings’ and then ‘Other Settings’. Scroll down to the configuration area and change ‘Internet Access’ from ‘Auto’ to ‘Require’.

Now click ‘Build and Run’ to build the Android version and run it on your connected device. In my case, I’m running it on an emulator I started earlier. We need to tell Unity to include the Internet permission as Unity is unaware that our plugin is making calls to fetch content from the web and won’t add it by itself.

With the app running, tapping on the browse button will make the WebPanel appear, which is why we made it pink, and if you tap a link in the web content, you’ll see the webview follow the link. Tapping the close button will close the Android webView and also disable the WebPanel, allowing our app to behave as before.

I hope you found this tutorial useful. You can use this to show a help page, or information page directly in your Unity app that is either stored as a HTML file, or is downloaded from a website. You can add more controls to the Unity Canvas to allow you to navigate forwards and backwards, and maybe jump to a specific URL.

As always, you can follow me on Twitter @cwgtech, or check out my youtube channel at https://www.youtube.com/channel/UCdrrB0J4ovI4xQkqiK4HEiw. Please feel free to leave any comments or suggestions below, or let me know how you customized this technique for your own purpose. Subscribe to my youtube channel to get notified when I post a new tutorial.

Sharing an image via ActivityViewController with iOS and Unity3D

Welcome to my tutorial on how to share a screen image on an iOS device by extending our Unity Plugin.

We will add a share function to our plugin code that will make use of the standard iOS ActivityViewController. Using this View Controller is quite complex and powerful, and will require a setup that is dependent on the device being used.

Once again this tutorial assumes a reasonable familiarity with unity, C sharp, objective-c, iOS programming and XCode.

Let’s get started by loading up our previous project in Unity and adding a share button to the scene.

In the Hierarchy view, click ‘Create’ and select UI->Button. This will create a Canvas and add a button directly to it.

Select the button, adjust it’s anchor to be top-center and set the Y pos to -15, which should put the top of the button against the top of the screen.

Expand the button in the hierarchy and click on Text and change the text string to ‘Share’.

Now double click our PluginTest script to open it up in Visual Studio so we can add the C# share function.

First, let’s add the reference to the external IOS method that we’ll use. In the section with our other extern declarations, add the following two lines:

[DllImport("__Internal")]
 private static extern void IOSshareScreenImage(byte[] imagePNG, long imageLen, string caption, intCallback callback);

We’re going to need a public reference to our UI button. So add the line

 public Button shareButton;

We’ll also need to add the appropriate Using statement, so you can right click on Button, select Quick Fix and then ‘using UnityEngine.UI;’.

Comment out the line in our Start method that randomly shows the alert dialog. We’ll use the alert dialog to let the user know when the share method is finished along with the completion result later.

Let’s add the method that our Button will hook into. This method will call into another method that will actually create the screenshot and share it. To make our screenshot clean, we’ll hide the button before we take the screenshot and then restore it when the screenshot has been shared.

//called when the user taps the 'share' button
public void ShareScreenTapped()
{
  if (shareButton != null)
    shareButton.gameObject.SetActive(false);
  ShareScreenShot(Application.productName + " screenshot", (int result) =>;
    {
      Debug.Log("share completed with: " + result);
      CreateIOSAlert(new string[] { "Share Complete", "Share completed with result " + result, "Ok" });
      if (shareButton != null)
        shareButton.gameObject.SetActive(true);
    });
}

We’re using an anonymous function that will be called when the share function completes. This lets us re-enable the button and pop-up an alert view with the result from the share function.

This next section is quite complex. It involves saving off a reference to the passed function, a callback function that will receive the success/fail status from the iOS ActivityShare function, a method to take the screenshot and then a co-routine to wait for the end of the frame before grabbing a copy of the frame buffer and passing it to our iOS method as a PNG.

First, let’s create two static variables. One to hold the passed function reference:

static System.Action ShareCompleteAction;

And one to hold a bool state that will be true while we’re sharing a screenshot:

static bool isSharingScreenShot;

This is to prevent the method being called while we’re waiting for a previous call to complete.
Now we’ll add the callback function that will be called from iOS. In order to do that, we need to mark it with MonoPInvokeCallback so the compiler knows to marshal the call correctly.

[AOT.MonoPInvokeCallback(typeof(intCallback))]
static void shareCallBack(int result)
{
  Debug.Log("Unity: share completed with result: " + result);
  if (ShareCompleteAction != null)
    ShareCompleteAction(result);
  isSharingScreenShot = false;
}

This is similar to the callback function used by the AlertView, and in fact uses the same intCallback type. When this function is called, it will trigger the stored reference to the function passed into ShareScreenShot, if it’s not null, and clear the isSharingScreenShot flag as we’re finished.

We’re now ready to add the ShareScreenShot method. This method will make sure we’re not already sharing a screenshot and then start a co-routine that will create a texture containing the current frame buffer. As recommended by Unity, we’ll wait for the end of the next frame to ensure the frame buffer is fully rendered. Add the following lines:

public void ShareScreenShot(string caption, System.Action shareComplete)
{
  if (isSharingScreenShot)
  {
    Debug.LogError("Already sharing screenshot - aborting");
    return;
  }
  isSharingScreenShot = true;
  ShareCompleteAction = shareComplete;

  //grab the screenshot & send it to iOS
  StartCoroutine(waitForEndOfFrame(caption));
}

The co-routine will wait for the end of the next frame and then send the texture we create to our iOS method. Add the following lines to complete our C# modifications:

IEnumerator waitForEndOfFrame(string caption)
{
  yield return new WaitForEndOfFrame();
  Texture2D image = ScreenCapture.CaptureScreenshotAsTexture();
  Debug.Log("Image size: " + image.width + " x " + image.height);
  byte[] imagePNG = image.EncodeToPNG();
  Debug.Log("png size: " + imagePNG.Length);
  if (Application.platform == RuntimePlatform.IPhonePlayer)
    IOSshareScreenImage(imagePNG, imagePNG.Length, caption, shareCallBack);
  Object.Destroy(image);
}

After waiting until the end of frame, we grab the frame buffer into a 2D texture. From this, we construct a PNG rendition of the image, and then after ensuring we’re on an iOS platform, call into the iOS method, passing the PNG, it’s length, the caption and a pointer to our shareCallBack function.

With the C# modifications completed, we need to hook up our button and also set the reference to it for our script. I had to adjust my Unity layout so you could see how I connected the button to the ShareScreenTapped method.

Now it’s time to update our iOS code. Double click the file ‘MyPlugin’ in the Plugins/iOS folder. This will launch XCode and open our file.

First, add a method to the MyPlugin class that will package the PNG image and caption into an NSArray and pass them to an ActivityViewController.
Before we do that, we need to add a few extra variables to our class. In the @interface declaration at the top of the file add these two lines:

INT_CALLBACK shareCallBack;
UIPopoverController *popover;

The shareCallBack variable will hold the pointer to our C# callback function, while the popover variable will point to the popover controller we’ll use if the code is running on an iPad device.

With that done, we can add the method’s we’ll need to the main class by adding the following lines before the @end statement for the MyPlugin @implementation:

-(void) shareScreenImage:(const unsigned char*) imagePNG_in length:(long)length caption:(const char*) caption_in callback:(INT_CALLBACK) callback
{
  NSMutableArray *shareableItems = [NSMutableArray arrayWithCapacity:2];
  //This array will hold the caption and image so we can send it to the share activity.
  NSString *caption;
  UIImage *image;
  if (caption_in!=nil)
  {
      caption = [MyPlugin createNSString:caption_in];
      [shareableItems addObject:caption];
  }
  if (imagePNG_in!=nil)
  {
      NSData* pngData = [NSData dataWithBytes:imagePNG_in length:length];
      image = [UIImage imageWithData:pngData];
      [shareableItems addObject:image];
      pngData = nil;
  }

This will convert the caption, if it’s set, to an NSString and the PNG data to a UIImage. Note we have to first convert the supplied byte array into a NSData object, and we need the length of the array to achieve this.

shareCallBack = callback;
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:shareableItems applicationActivities:nil];
activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    NSLog(@"Activity %@ completed: %d",activityType,completed);
    if (activityError!=nil)
        NSLog(@"Error: %@",[activityError localizedDescription]);
    if (shareCallBack!=nil)
        shareCallBack(completed);
};

Here we create a UIActivityViewController with the shareableItems array we setup and populated earlier. We also construct a completionWithItemsHandler. This function will be called when the user completes the share function, either by selecting a method, or by cancelling the request.

The completion function will pass the completed bool to our callback method, assuming it’s not nil.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    [UnityGetGLViewController() presentViewController:activityViewController animated:YES completion:^{
        NSLog(@"share presented");
    }];
else
{
    popover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
    UIView *mainView = UnityGetGLView();
    popover.delegate = nil;
    [popover presentPopoverFromRect:CGRectMake(mainView.frame.size.width/2, mainView.frame.size.height-10, 0, 0) inView:mainView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

In order to present the activity view controller, we need to use two different methods, one for iPhone devices and another using a popover controller for iPad devices. We’ll make the choice by checking the result of the macro UI_USER_INTERFACE_IDIOM.

On iPhone devices, we can present the ViewController using the main Unity ViewController directly, while on iPad devices, we first create a popover controller and then use the main Unity View as the parent view to present the popover.

In both cases, the ActivityViewController finish method is used to notify Unity that the share is complete via our supplied callback function.

Finally, we need to add a C style method that our C# code will call into, which will call our new share method on the plugin. Add the following lines to the extern C block:

void IOSshareScreenImage(const unsigned char* imagePNG, long imageLen, const char* caption, INT_CALLBACK callback)
{
    [[MyPlugin sharedInstance] shareScreenImage:imagePNG length:imageLen caption:caption callback:callback];
}

This completes the iOS code modifications, however we need to add a key to the application’s plist file that let’s iOS know we may want to save screenshots into the user’s camera roll.

Click on the Info.plist file in the file hierarchy in Xcode, and then click the + icon to the right of the first line. In the text box that pops up, type the following:

Privacy - Photo Library Additions Usage Description

And then in the string box to the right, add a description that a user might see when they try to save a screenshot. I used:

Allow access to save screenshots

With all these modifications in place, we’re ready to build and test the code. Switch back to Unity, give it a few seconds to rebuild the C# code and then press CMD-B to start the project building.

Once XCode has finished building the project the code will execute on our simulator. Tap the ‘share’ button at the top of the screen and you’ll see the activity controller appear. You can then select what iOS will do with your image.

Go back to XCode and select an iPad as a target device. Now when you run the code and tap the share button, you’ll see a popover appear with the share activities in it. Unity continues to run behind this popover as you’d expect.

Both types of device will display an alert when the share is completed with the bool completion status. A 0 indicates the share failed, while a 1 indicates success.

If you run on a real device, then you’ll get more share options, including iMessage and Facebook if you have the app installed. If you use iMessage, along with the screenshot image you’ll see the caption text we’ve passed through. Notice that the screenshot doesn’t have our ‘share’ button on it, as we hid that before we grabbed the frame buffer.

We’ve now added a share function to Unity that allows the user to send a screenshot to various activities on their device without leaving your App. Use this to let users send highscore images, or new level images to their friends.

I hope you found this tutorial useful and are able to use it to add sharing to your apps. The code for this plugin can be found at https://github.com/cwgtech/iOSUnityShareScreenShot and the video at https://youtu.be/NwphcgWQMhQ.

As always, please feel free to reach out with any comments or questions.

Unity Tutorials

I’ve started working on a series of Unity tutorials. Initially I’m going to focus on some of the low level stuff required to build iOS and Android products with Unity, specifically creating plugins that will allow Unity to access features that are specific to that platform.

You can see the tutorials at my youtube channel https://www.youtube.com/channel/UCdrrB0J4ovI4xQkqiK4HEiw along with some other review videos I’ve posted to Amazon.

I’ll be posting the code from the tutorials on my github page – https://github.com/cwgtech – check back for updates.