001 Scaleform -> Unity function call

Post date: 2012/11/1 上午 02:49:41

Invoking C# functions from ActionScript

The integration makes it easy to declare C# functions that can be called from ActionScript using

ExternalInterface. For example, suppose you have a simple Flash file Button.swf that contains a

movieclip. You want the position of the movieclip to change according to the position of the player every

time you click a button. This logic can be implemented as follows:

Define a mouseclick handler in ActionScript:

function handleClick(event:MouseEvent):void


if (ExternalInterface.available) {

ExternalInterface.call("UpdatePosition", player_mc);



Now in C#, create a class that derives from Movie and declare a function “UpdatePosition” that takes a Value as a parameter.

public class UI_Scene_Movie: Movie


public UpdatePosition(Value movieRef)


// Get Player Position using Game API, use GetDisplayInfo to get the

displayInfo object corresponding to movieRef and use SetDisplayInfo to

reset the displayinfo after modifying it according to the player




Internally, Unity integration uses C# reflection to figure out the correct C# function to call during an ExternalInterface callback. For this to work, you need to declare a function in a Movie derived class that has the same signature (same name and parameters) as that used in the ExternalInterface.call invocation. If no matching function is found, the ExternalInterface invocation will silently fail.

Direct Access API

Direct Access API (DAPI) is used to directly access and modify flash objects and their properties. DAPI eliminates the need for creating functions in ActionScript and invoking them every time some property of a flash object needs to be accessed, thereby dramatically improving speed and efficiency. The Value interface is used by Scaleform to represent simple types such as Int, Bool as well as complex types such as DisplayObject. The Unity Integration provides a wrapper layer for the entire DAPI interface, making it possible for the user to directly access flash objects in C#. DAPI enables you to write most of your UI logic in C# without having to write a lot of ActionScript code. For more information, please refer to Value.cs and look at the implementation of the Hud and Pause Menu in the StarShip demo.

Using the Integration This section enumerates the key steps you need to perform in order to use Scaleform in Unity. The first step is to create a game object and attach a script to it that initializes Scaleform runtime and instantiates the Scaleform Manager. The “Main Camera” game object performs this role in the StarshipDown demo. The “ScaleformCamera” script is attached to this object. Together with its base class, SFCamera, Scaleform Camera performs all the steps needed to initialize Scaleform and hook into the Unity Event system. SFCamera exposes an InitParams structure, which is inspectable in the editor and can be used to set various Scaleform states. For example, you can set whether you want to initialize Video/Sound subsystems, which version of ActionScript (AS2/3/both) to use etc. The second step is to create Scaleform movies by instantiating Movie derived classes. Movie already contains the core functionality necessary to perform various movie related tasks such as creating/destroying movies, calling advance/display by interacting with the Scaleform plugin. The third step is to add custom event handlers in your Movie derived classes. Through the use of reflection in C#, the integration makes it easy to add event handlers. You just need to add functions that have the same name and accept the same parameters as the corresponding externalInterface callback. The StarshipDown demo makes extensive use of this pattern. For examples, look at UI_Scene_HUD.cs file.

Simple Lan Connection Menu

hey yo

heres a quick and dirty way to make a scaleform menu to connect over a LAN.

it isnt a detailed explination of everything involved in making a menu, there is plenty of info about that already.


place a TextInput with an instance name of ip_mc (properties).

a Button called join_mc.

a Button called host_mc



import flash.external.ExternalInterface; // variable to hold the hosts ip address var ip:String; // client /////// // call Joining function when join button is clicked join_mc.addEventListener("click", this, "joining"); function joining() { ip = ip_mc.text; // make ip variable whatevers typed into the TextInput // call JoinFunction in unreal script and send ip ExternalInterface.call("JoinFunction", ip); } // Host//////////////// // call hosting function when host button is clicked host_mc.addEventListener("click", this, "hosting"); function hosting() { // call HostFunction in unreal script ExternalInterface.call("HostFunction"); }

Unreal Script


class GFxTeglegFrontEnd extends GFxMoviePlayer; //join a game function JoinFunction(string ip) { //open the ip address in the textinput ConsoleCommand("open "$ip); } // Host a game function HostFunction() { //open a map as a listen server, change DM-Deck to your map ConsoleCommand("open DM-Deck?Listen"); }

theres probably other functions you need to include in GFxTeglegFrontEnd to make it work, only the connection functions are shown here.

have fun