Error #2070: Security sandbox violation in AIR with local file

I’m trying to load Unity content in a bigger AIR-based application using

var loaderParams:UnityLoaderParams = new UnityLoaderParams(true);
var unityContentLoader:UnityContentLoader = 
    new UnityContentLoader(currentAssetVO.name, this, loaderParams, false);
unityContentLoader.contentLoaderInfo
    .addEventListener(Event.COMPLETE, onUnityLoaderComplete, false, 0, true);
unityContentLoader.contentLoaderInfo
    .addEventListener(ProgressEvent.PROGRESS, onUnityProgress, false, 0, true);
unityContentLoader.contentLoaderInfo
    .addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true); 
unityContentLoader.loadUnity();

Later when I try to do

[Bindable]
public var movieClipContainer:UIComponent;

< … snip snip … >

movieClipContainer.addChild(loader);

I get the following error:

  [trace] *** Security Sandbox Violation ***
  [trace] SecurityDomain 'file:///Users/username/tmp/UnityCubeMoveIntro.swf' tried to access incompatible context 'app:/MissionTesterLauncher.swf'
  [Fault] exception, information=SecurityError: Error #2070: Security sandbox violation: caller file:///Users/myusernamehere/tmp/UnityCubeMoveIntro.swf cannot access Stage owned by app:/MissionTesterLauncher.swf.
  at flash.display::Stage/set scaleMode()
  at com.unity::UnityContent/setupStage()
  at com.unity::UnityContent/onAddedToStage()
  at flash.display::DisplayObjectContainer/addChildAt()
  at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::$addChildAt()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7291]
  at mx.core::UIComponent/addChild()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7171]

Note that both SWF files are local, so nothing is loaded from a server. Since this is an AIR application, I can’t simply use Security.allowDomain as a quick hack.

I poked at the UnityShared.swc file and noticed this:

 native public override function loadBytes(bytes:flash.utils.ByteArray,
     context:flash.system.LoaderContext = null):void;

So I then tried the following:

 var loaderContext:LoaderContext = new LoaderContext(false,
     new ApplicationDomain(ApplicationDomain.currentDomain), 
     SecurityDomain.currentDomain);
 loaderContext.allowCodeImport = true;
 unityContentLoader.loadBytes(byteArray, loaderContext)

But this didn’t work at all: i.e. the onUnityProgress event listener never gets called and so on. Maybe the loadBytes implementation is simply an empty stub?

How do I work around this? The AIR application is somewhat complicated and contains several different loaded SWFs, all of which have been loaded with a proper LoaderContext so they work fine, but I can’t seem to get Unity content working at all.

Are you using UnityContentHost from UnityShared.swc? Try referring to this example on the forums to see if you can successfully embed your unity content this way:

http://forum.unity3d.com/threads/116650-UnityShared.swc-Communicating-between-Unity-Flash-Content-and-AS3.?p=922340&viewfull=1#post922340