Legacy Documentation: Version 4.6.2
Language: English
Example: Calling ActionScript Functions from Unity
Example: Accessing the Stage

Example: Browser JavaScript Communication

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

This example shows how AS3 code can communicate JavaScript in the browser. This example makes use of the ExternalInterface ActionScript class.

When run, the BrowserCommunicator.TestCommunication() function will register a callback that the browser JavaScript can then call. The ActionScript will then call out to the browser JavaScript, causing an alert popup to be displayed. The exposed ActionScript function will then be invoked by the JavaScript, completing the two-way communication test.

Required JavaScript

The following JavaScript needs to be added to the html page that serves the Unity published SWF. It creates the function which will be called from ActionScript:

JavaScript

<script type="text/javascript">

function calledFromActionScript()
{
    alert("ActionScript called Javascript function")

    var obj = swfobject.getObjectById("unityPlayer");
    if (obj)
    {
        obj.callFromJavascript();
    }
}

</script> 


BrowserCommunicator.as (and matching C# class)

ActionScript 3

package
{
    import flash.external.ExternalInterface;
    import flash.system.Security;
  
    public class BrowserCommunicator
    {
        //Exposed so that it can be called from the browser JavaScript.
        public static function callFromJavascript() : void
        {
            trace("Javascript successfully called ActionScript function.");
        }
    
        //Sets up an ExternalInterface callback and calls a Javascript function.
        public static function TestCommunication() : void
        {
            if (ExternalInterface.available)
            {
                try
                {
                    ExternalInterface.addCallback("callFromJavascript", callFromJavascript);
                }
                catch (error:SecurityError)
                {
                    trace("A SecurityError occurred: " + error.message);
                }
                catch (error:Error)
                {
                    trace("An Error occurred: " + error.message);
                }
        
                ExternalInterface.call('calledFromActionScript');
            }
            else
            {
                trace("External interface not available");
            }
        } 
    }
}


C# dummy implementation of the class:

C#

[NotConverted]
[NotRenamed]
public class BrowserCommunicator
{
   [NotRenamed]
   public static void TestCommunication()
   {
   }
}


How to test

Simply call BrowserCommunicator.TestCommunication() and this will invoke the two-way communication test.

Potential Issues

Security Sandbox Violation

A SecurityError occurred: Error #2060: Security sandbox violation

This happens when your published SWF does not have permission to access your html file. To fix this locally, you can either:

For more information on the Flash Security Sandboxes, please refer to the Adobe documentation.

Example: Calling ActionScript Functions from Unity
Example: Accessing the Stage