From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(How to use)
(fixed typo)
Line 26: Line 26:
htmlTexture_start("", width, height);
htmlTexture_start("", width, height);
// put the texture on something
// put the texture on something
Transform.renderer.sharedMaterial.mainTexture = m_Texture;
transform.renderer.sharedMaterial.mainTexture = m_Texture;

Revision as of 11:48, 21 January 2008


Author info

Created by Rob Terrell of Stinkbot LLC. You can contact him at

What it does

The htmlTexture plugin renders web content into a texture you can apply to any material. Interaction with the web page is possible through simulated mouse clicks and javascript. Any web content the underlying browser can display (including Flash, Shockwave, PDF, Quicktime movies, etc.) is supported.


The texture appears backwards in Unity, so you need to apply a placement transform of -1.0 in the X dimension. Textures dimensions currently must be a power of two (i.e. 256, 512, 1024).

How to use

Like other texture plugins, your code needs to create a new bitmap at the start, then call htmlTexture_start(). You then update the texture's pixel data every so often -- typically every Update() or FixedUpdate(). The sample below is c#:

public int width =512;
public int height = 512;
void Start() {
	m_Texture = new Texture2D (width, height, TextureFormat.ARGB32, false);
	htmlTexture_start("", width, height);
	// put the texture on something
	transform.renderer.sharedMaterial.mainTexture = m_Texture;

void Update() {
	htmlTexture_update( m_Texture.GetInstanceID() );

void OnApplicationQuit() {

Attach the above script to a game object, and when you Play the scene, its texture will be the current Google home page.

Please note that, as in the Quicktime plugin, the texture is reversed left-to-right when applied to an object. You can flip the texture's appearance on the object by applying a placement size transform of {-1,0} in the object editor.

Simulated mouse events

The htmlTexture plugin can simulate mouse events in the offscreen web view, allowing for basic web browsing (i.e. clicking links).

You can cast a ray from the camera to the location of a mouse click to find the texture coordinate of a click. You can then pass that to the plugin, but since the texture appears backwards, you need to do a little subtraction first.

void OnMouseUp()
	RaycastHit hit;
	if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
		int x = width - (int) (hit.textureCoord.x * width); 
		int y = height - (int) (hit.textureCoord.y * height); 
		htmlTexture_mouseup( x, y );


You can download the current version of the plugin from here. Currently, the plugin is a Mac-only universal binary.


This plugin is Copyright 2007 Stinkbot LLC. Please check with us before including it in release builds of your projects.

Version history

  • 0.1 First working version
  • 0.2 Second working version
  • 0.3 Added mousedown, mouseup, mousemoved, goBack, goForward
  • 0.4 Universal binary

To do

Support an arbitrary number of textures and webviews. Currently, there's only one webview that is rendered onto all textures you pass into htmlTexture_update().

Interface Declaration

//	htmlTexture_start() creates the offscreen window and webview
//	url: initial URL to display, use null for none
//	width, height: currently must be a power of 2 (i.e. 128, 256, 512, 1024) due 
//    to my using GL_TEXTURE_2D

[DllImport ("htmlTexture")]
private static extern void htmlTexture_start( string url, int width, int height);

// htmlTexture_stop() releases the window and webview

[DllImport ("htmlTexture")]
private static extern void htmlTexture_stop();

// htmlTexture_update() -- call this in your MonoBehavior::Update() 
// loads a bitmap of the webview into openGL with the given texture ID

[DllImport ("htmlTexture")]
private static extern void htmlTexture_update ( int texID );

// browser functions
// as you'd expect, handles any URL, HTML and CSS that webkit can

[DllImport ("htmlTexture")]
private static extern void htmlTexture_setURL( string url);

[DllImport ("htmlTexture")]
private static extern void htmlTexture_goBack();
[DllImport ("htmlTexture")]
private static extern void htmlTexture_goForward();

// sends a string to the webview's javascript interpreter
// returns a string ptr result -- use Marshal.PtrToStringAnsi() to read

[DllImport ("htmlTexture")]
private static extern void htmlTexture_sendJavascript( string js);

// these functions simulate a mouse event in the webview

[DllImport ("htmlTexture")]
private static extern void htmlTexture_mousemoved( int _x, int _y );

[DllImport ("htmlTexture")]
private static extern void htmlTexture_mousedown( int _x, int _y );

[DllImport ("htmlTexture")]
private static extern void htmlTexture_mouseup( int _x, int _y );

// htmlTexture_leftclick() = a mousemoved, mousedown, and mouseup

[DllImport ("htmlTexture")]
private static extern void htmlTexture_leftclick( int _x, int _y );
Personal tools