/GarlicWebview-Unity

A simple webview for unity3d engine

Primary LanguageObjective-CMIT LicenseMIT

GarlicWebview

A simple webview plugin for unity3d engine.

Supports both Android and iOS.

Install

Download GarlicWebview-Unity.unitypackage file and import, then enjoy!

Android


dependencies {
    ...
    
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    
    ...
}

iOS

  • Requires iOS version >= 9.0
  • This plugin uses WKWebView and built using Swift 5.

How to use

Callback Receiver

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

Webview Options

Functions below are used to set options for webview. You should call those functions after initialize webview, and before showing webview.

SetMargins(int left, int right, int top, int bottom)

Sets margins around webview. Units are in px. Example :


int marginPx = (int)GarlicUtils.DPToPx (30f);
GarlicWebview.Instance.SetMargins(marginPx, marginPx, marginPx, marginPx);

SetFixedRatio(int width, int height)

Webview will be shown in Fixed Ratio(Width:Height). Margins will also still be applied.

UnsetFixedRatio()

Call this function if you want to unset fixed ratio options.

Show Webview

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.