A simple webview plugin for unity3d engine.
Supports both Android and iOS.
Download GarlicWebview-Unity.unitypackage file and import, then enjoy!
- This plugin uses DialogFragment to show Webview.
- Webview layout is built using ConstraintLayout, which means you need to define depencency on gradle file like this :
dependencies {
...
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
...
}
- In Unity, you need to add gradle template file. See 'Providing a custom build.gradle template' paragraph on Unity's documentation page. Also, you can check sample code's main template file.
- You can modify Webview's configuration as you want with modifying code inside GarlicWebdialogFragment.java's OnCreateView method.
- Requires iOS version >= 9.0
- This plugin uses WKWebView and built using Swift 5.
If you want to receive callbacks from webview, implement IGarlicWebviewCallback
interface.
private class GarlicWebviewCallbackReceiver : IGarlicWebviewCallback
{
public void onClose()
{
Debug.Log("GarlicWebview: onClose");
}
public void onPageFinished(string url)
{
Debug.Log("GarlicWebview: onPageFinished [" + url + "]");
}
public void onPageStarted(string url)
{
Debug.Log("GarlicWebview: onPageStarted [" + url + "]");
}
public void onReceivedError(string errorMessage)
{
Debug.Log("GarlicWebview: onReceivedError [" + errorMessage + "]");
}
public void onShow()
{
Debug.Log("GarlicWebview: onShow");
}
}
Then, you can register your interface instance with GarlicWebview.Instance.SetCallbackInterface()
method.
//Instantiate your callback receiver.
var callbackReceiver = new GarlicWebviewCallbackReceiver();
//...Then set receiver with SetCallbackInterface().
//Note : Initialization method will be automatically called when accesing GarlicWebview.Instance
GarlicWebview.Instance.SetCallbackInterface(callbackReceiver);
Functions below are used to set options for webview. You should call those functions after initialize webview, and before showing webview.
Sets margins around webview. Units are in px. Example :
int marginPx = (int)GarlicUtils.DPToPx (30f);
GarlicWebview.Instance.SetMargins(marginPx, marginPx, marginPx, marginPx);
Webview will be shown in Fixed Ratio(Width:Height). Margins will also still be applied.
Call this function if you want to unset fixed ratio options.
Simply call GarlicWebview.Instance.Show()
.
using Garlic.Plugins.Webview;
...
void Start()
{
var callbackReceiver = new GarlicWebviewCallbackReceiver();
//Initialization method will be automatically called when accesing GarlicWebview.Instance
GarlicWebview.Instance.SetCallbackInterface(callbackReceiver);
GarlicWebview.Instance.Show("Your URL Here");
}
...
See GarlicWebviewSample project to learn more about this plugin. Tested on Samsung Galaxy s8, sample project built with Unity2017.4.26f1 LTS.