Skip to content

Binding JavaScript functions to C# callbacks

Rycul edited this page Jul 17, 2013 · 4 revisions

This page assumes that you have succesfully set up an AwesomiumUnityWebTexture to render a webpage within Unity.

The C# code

[RequireComponent(typeof(AwesomiumUnityWebTexture))]
public class JavaScriptCallbackExample : MonoBehaviour 
{
	private AwesomiumUnityWebTexture m_WebTexture = null;

	// Use this for initialization
	void Start () 
	{
		// Obtain the web texture component.
		m_WebTexture = GetComponent<AwesomiumUnityWebTexture>();
		
		// Check to make sure we have an instance.
		if (m_WebTexture == null)
		{
			DestroyImmediate(this);
		}
		
		// Bind a C# function to a javascript function.
		m_WebTexture.WebView.BindJavaScriptCallback("MyFunction", this.Callback_MyFunction);	// Can be called from the HTML page by using: Unity.PlayGame();
	}
	
	void Callback_MyFunction()
	{
		Debug.Log("I was called from JavaScript!");
	}
}

The JavaScript code

Unity.MyFunction();

Clone this wiki locally