HtmlTexturePlugin

From Unify Community Wiki
Jump to: navigation, search

Contents

Author info

Created by Rob Terrell of Stinkbot LLC. You can contact him at robterrell@gmail.com.


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 most recent version, 0.5 (25 Jan 2008), supports multiple textures and URLs. You can create as many HTML textures as you need. The framerate, even with multiple Flash animations, is quite good.


Limitations

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() with the ID of that texture. 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);
	m_Texture.Apply();
	htmlTexture_start(m_Texture.GetInstanceID(), width, height, "http://google.com");
	// put the texture on something
	transform.renderer.sharedMaterial.mainTexture = m_Texture;
}
 
void Update() {
	htmlTexture_update( m_Texture.GetInstanceID() );
}
 
void OnApplicationQuit() {
	htmlTexture_stop();
}


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(m_Texture.GetInstanceID(), x, y );
	}
}

The object this is attached to needs to have a mesh collider for this to work.

Download

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


Copyright

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
  • 0.5 Added support for multiple textures, getURL, and returning a result from javascript calls.

To do

Support an arbitrary number of textures and webviews: DONE. Windows version: in progress.

Interface Declaration

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.Text;
 
public class TexturePlayback : MonoBehaviour {
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_start( int textID, int width, int height, string url );
	//	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_TEXTURE2D
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_stop();
	// releases all windows and webview; use on quit
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_update ( int texID );
	// makes bitmap of the webview and loads it into openGL with the given texture ID
 
	//
	// browser functions
	//
	// as you'd expect, handles any URL, HTML and CSS that webkit can
	//
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_setURL( int texID, string url);
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_getURL( int texID, StringBuilder url, int stringCapacity);
	// to get the current URL, you need to pass in a StringBuilder and its capacity (to
	// avoid buffer overflow errors)
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_goBack( int texID );
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_goForward( int texID );
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_sendJavascript( int texID, string js);
	// sends a string to the webview's javascript interpreter
	// will return a string result as soon as I get the Marshall class figured out
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_sendJavascript( int texID, string js, StringBuilder result, int stringCapacity);
	// sends a string to the webview's javascript interpreter
	// will return a string result 
 
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_sendKeypress( int texID, string s);
 
 
	//
	// these functions simulate a mouse event in the webview
	//
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_mousemoved( int texID, int _x, int _y );
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_mousedown( int texID,  int _x, int _y );
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_mouseup( int texID, int _x, int _y );
 
	[DllImport ("htmlTexture")]
	private static extern void htmlTexture_leftclick( int texID, int _x, int _y );
	// leftclick = a mousemoved, mousedown, and mouseup 
 
	// 
	// End of the plugin interface 
	// 
 
	// Your code goes here!
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools