Unity3D is a excellent 3D games development system, but lacks the ability to leverage existing Flash based content.
This article describes my work so far, to enable Flash and Unity3D content to be used together.
As both a Flash and Unity3D developer, I’ve always wished that there was a way to use Flash content inside Unity, as Flash is a great tool for quickly creating an animation or 2D game.
However on the SuperVolei Brazil site, Flash is only used as a menuing system before the Unity game is activated. The background of the Flash and the Unity content is the same pre-rendered image, so that when the browser switches between between the Flash and Unity DIV’s the player doesn’t see any visual change.
The big limitation of this technique is that the switch between Flash and Unity can only occur at times when the there is a pre-rendered background for both the Flash and Unity content, e.g. at the start and end of games.
While reading the Unity3D docs, I realised that there was a way to improve on the methods employed by Aquiris.
The technique employs texture.readPixels to take a snapshot of the user’s current view of the Unity scene, and using it as a background for the Flash content. Hence making it possible to switch from Unity to Flash at virtually any time.
The basis of the c# code for Unity is shown here.
int width = Screen.width; int height = Screen.height; Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false ); tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 ); // Read screen contents into the texture tex.Apply(); byte  imgBytes = tex.EncodeToPNG();// Encode texture into PNG Destroy( tex ); sendImageData(imgBytes);// Send data to Flash to use as a background
Using the technique as describe above, I craeted this prototype / proof of concept. Where the user can move around a 3D scene and can activate Flash based popups by clicking on any active item.
Note. In the 3D scene, movement is controlled using the arrow keys. Objects are selected using the mouse. There is currently only one Flash item , the “Variables test” app.
It’s launched from both the computer screen, that is in the middle of the Unity scene when the prototype first loads, and again on the coffee pot, which is to the left of the computer screen. Clicking on either of these objects launches the same Flash app, however it is possible to launch different Flash apps off different hotspots.
It should be noted that the main caveat of this method is that the Unity scene needs to be “paused”, and some visual metaphor e.g. a darkening of the Unity content needs to be used, to inform the player that this has happened so that they don’t expect things to still be animating in the Unity scene. However I’ll discuss that in another posting.
In the next part of this series I’ll cover the method used to pass the image data to Flash as well as the operation Flash based swf launcher, which makes use of the background image.
To be continued…