Flash: Embedding Unity Generated Flash Content in Larger Flash Projects
Manual     Reference     Scripting   
Unity Manual > Getting Started with Flash Development > Flash: Embedding Unity Generated Flash Content in Larger Flash Projects

Flash: Embedding Unity Generated Flash Content in Larger Flash Projects

If you want to embed your Unity generated Flash content within a larger Flash project, you can do so using the UnityShared.swc. This SWC provides functionality to load and communicate with Unity published Flash content.

When your Unity Flash project is built, a copy of the UnityShared.swc file will be placed in the same location as your built SWF. You can then use this in your Flash projects as per other SWCs. For more details on what SWCs are and how to use them, see Adobe’s documentation.


UnityShared.swc Contents

In the UnityShared.swc file, you will find two classes and two interfaces. Each of these, and their available functions, are described below.

IUnityContent

IUnityContent is implemented by Unity built Flash content. This interface is how you communicate with or modify the Untiy content.

getTextureFromNativeId(id : int) : TextureBase;Enables retrieving of textures. A full example project using this can be found on the forums.
sendMessage(objectPath : String, methodName : String, value : Object = null) : Boolean;The sendMessage function can be used to call a method on an object in the Unity content.
setContentHost(contentHost : IUnityContentHost) : void;Sets the host (which must implement IUnityContentHost) for the Unity content. The host can then listen for when the Unity content has loaded/started.
setSize(width : int, height : int) : void;Modifies the size of the Unity content
startFrameLoop() : void;Starts the Unity content.
stopFrameLoop() : void;Stops the unity content.


IUnityContentHost

This must be implemented by whichever class will host the Unity content.

unityInitComplete() : void;Called when the Unity engine is done initializing and the first level is loaded.
unityInitStart() : void;Called when the content is loaded and the initialization of the Unity engine is started.


UnityContentLoader

The UnityContentLoader class can be used to load Unity published Flash content. As with standard AS3 Loader instances, you can add event listeners to its contentLoaderInfo in order to know the progress of the load and when it is complete.

UnityContentLoader(contentURL : String, contentHost : IUnityContentHost = null, params : UnityLoaderParams = null, autoLoad : Boolean = true) : void;

Creates a UnityContentLoader instance which you can attach event listeners to and use to load the unity content.

loadUnity() : void;Instructs the UnityContentLoader to load the Unity content from the URL supplied in the constructor.
unityContent : IUnityContent;Once the content has finished loading, you can access the Unity content to perform functionality such as sendMessage().


UnityLoaderParams

Parameters can be supplied to the UnityContentLoader when created to provide additional loader configuration.


function UnityLoaderParams(scaleToStage : Boolean = false, width : int = 640, height : int = 480, usePreloader : Boolean = false, autoInit : Boolean = true, catchGlobalErrors : Boolean = true) : void;


Example

The following example shows how to load Unity published Flash content into a host SWF. It shows how to supply custom UnityLoaderParams and track progress of the file load. Once the Unity content has been added to the host, a function in the Unity content is called using the sendMessage function.


public class MyLoader extends Sprite implements IUnityContentHost
{
  private var unityContentLoader:UnityContentLoader;

  public function MyLoader()
  {
      var params:UnityLoaderParams = new UnityLoaderParams(false,720,400,false);
      unityContentLoader = new UnityContentLoader("UnityContent.swf", this, params, false);
      unityContentLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onUnityContentLoaderProgress);
      unityContentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onUnityContentLoaderComplete);
      unityContentLoader.loadUnity();
  }

  private function onUnityContentLoaderProgress(event:ProgressEvent):void
  {
      //Respond to load progress
  }

  private function onUnityContentLoaderComplete(event:Event):void
  {
     addChild(unityContentLoader);
     unityContentLoader.unityContent.setContentHost(this);
  }

  //unityInitStart has to be implemented by whatever implements IUnityContenthost
  //This is called when the content is loaded and the initialization of the unity engine is started.
  public function unityInitStart():void
  {
    //Unity engine started	
  }

  //unityInitComplete has to be implemented by whatever implements IUnityContenthost
  //This is called when the unity engine is done initializing and the first level is loaded.
  public function unityInitComplete():void
  {
     unityContentLoader.unityContent.sendMessage("Main Camera","SetResponder",{responder:this});
  }

  ...

}

Page last updated: 2012-06-27