近期热门
粉丝927
关注 209
获赞 1023
Flash和untiy3D集成

[U3D] Flash和untiy3D集成

[复制链接]
5815 2 0 0 12年前 举报
将Unity生成的flash内容集成到大型flash项目中
embeddingapi.swc
如果你想将Unity生成的flash内容集成到大型flash项目中,你可以通过embeddingapi.swc,这个SWC提供了加载Unity生成的flash,以及和flash交互的方法,在这个文件中,有两个类和接口,当你的Unity-flash工程被编译,这个文件回合swf一起生成,你能够将这个当做一个swc使用,SWC怎么用就不多说了。
Stage3D的限制
当将Unity生成的flash对象集成到Flash项目中是,必须懂得flash显示模式,所有的stage3D内容是显示在Flash stage后面的,也就是也就是说所有Flash显示内容都将显示在你的3D内容前面,具体你还是看看Stage3D的说明文档吧,starling里面也有介绍。
IUnityContent接口
IUnityContent 是被Unity3D生成的swf执行的接口,这个接口是你如何和Unity的flash交互,以及如何修改它等。
方法:
getTextureFromNativeId(id : int) : TextureBase;
允许获取纹理,在论坛里能找到一个很大的例子工程。
sendMessage(objectPath : String, methodName : String, value : Object = null) : Boolean;
这个方法用户调用Unity内容中的某个对象的某个方法
setContentHost(contentHost : IUnityContentHost) : void;
为Unity内容设置主程(必须实现IUnityContentHost)这个接口),这个主持能够监听Unity的内容是否被加载或者开始。
setSize(width : int, height : int) : void;
修改Unity3D的大小
setPosition(x:int = 0, y:int = 0):void;
设置Unity3D在主程中的位置
startFrameLoop() : void;
启动Unity的内容
stopFrameLoop() : void;
停止Unity的内容
forceUnload():void;
强制卸载
IUnityContentHost
任何Unity内容的载体都必须实现这个接口
方法:
unityInitComplete() : void;
当Unity引擎启动完、第一层内容被加载时调用。
unityInitStart() : void;
当Unity内容被加载以及Uinity 引擎启动
UnityContentLoader
这是个能够加载解析Unity生成的flash内容的类,它继承自Loader类,作为标准的AS3 loader实例,你当然可以通过它的contentLoaderInfo 监听加载进度和是否完成
构造函数:
UnityContentLoader(contentURL : String, contentHost : IUnityContentHost = null, params : UnityLoaderParams = null, autoLoad : Boolean = true) : void;
contentURL: swf加载路径.
contentHost: 内容载体,必须实现IUnityContentHost.接口.
params: 如果想改写某些参数,用它吧,少年.
autoLoad: 如果设置为true,这个对象一旦创建,那么就开始加载,但是你的监听事件可能没有加,那就不知道进度和是否加载完了,所以设置为false,添加好监听事件之后,在调用loadUnity()
属性: unityContent : IUnityContent;
一旦内容完成加载,你能够通过这个接口发送消息,上面有介绍
方法: loadUnity() : void;
根据构造是传入的参数,开始加载Unity内容
forceUnload() : void;
强制从载体上卸载
unload() : void;
重写as3 的loader类,会调用forceUnload方法
unloadAndStop(gc:Boolean = true):void
卸载Unity内容,然后再调用接口unloadAndStop(gc)
UnityLoaderParams
该对象支持UnityContentLoader 创建时提供加载配置数据
function UnityLoaderParams(scaleToStage : Boolean = false, width : int = 640, height : int = 480, usePreloader : Boolean = false, autoInit : Boolean = true, catchGlobalErrors : Boolean = true) : void;
scaleToStage: Unity3D内容大小为固定或者父flash窗口变化.
width: Unity内容宽度.
height: Unity内容高度.
usePreloader:是否显示Unity的加载条.
autoInit: 暂时没有用上
catchGlobalErrors: 是否捕获错误,同时显示在左上角的红色窗中。
例子
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  rogressEvent):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});
}
...
}
flash传递数据给Unity
如果你希望从flash传递数据给Unity,那么该数据必须是支持的类型,当然,你也可以创建类去代替数据,通过和C#或者Javascript匹配的实现方式
首先,创建一个ActionScript文件夹在你的工程下,在创建一个AS3对象,放到刚刚说的文件夹下面
public class ExampleObject
{
public var anInt : int;
public var someString : String;
public var aBool : Boolean;
}
好了,你也要创建一个和AS3相匹配的C#或者javascript类,这个和以前AS3跟java,C++的通信没两样
这个NotRenamed属性用来防止影响构造函数,方法,域或者属性
NotConverted属性用户指令编译管道不要讲类型或者成员变成目标平台,一般情况下,如果你不标注,你的所有C#或者javascript脚本都将被编译成Actionscript脚本,如果在加了这个属性,那么就允许编译成自己希望的as脚本了,你提供的伪C#或者javascript 允许Unity知道每个类的统一标示,比如说那个类应该被调用,然后你的as文件提供了这些功能的实现,当然这些Actionscript只有在你编译成flash的时候才生效,在编辑器或者其他平台时中是不生效的。
C#
[NotConverted]
[NotRenamed]
public class ExampleObject
{
[NotRenamed]
public int anInt;
[NotRenamed]
public string someString;
[NotRenamed]
public bool aBool;
}
JavaScript
@NotConverted
@NotRenamed
class ExampleObject
{
@NotRenamed
public var anInt : int;
@NotRenamed
public var someString : String;
@NotRenamed
public var aBool : boolean;
}
现在我们需要一个方法来获取这个对象
public static function getExampleObject() : ExampleObject
{
return new ExampleObject();
}
这样我们就能够获取这个对象,并且获取该对象的数据
ExampleObject exampleObj = UnityEngine.Flash.ActionScript.Expression<ExampleObject>("MyStaticASClass.getExampleObject()");
Debug.Log(exampleObj.someString);
Unity通过不同的方式调用AS3的方法
下面的例子主要讲如何从Unity通过不同的方式调用AS3的方法,这个时候我们将会遇到3种脚本语言。
一个叫做ExampleClass.as是包含不同方法的例子,这个必须放在工程中一个叫做ActionScript的文件夹下面一个C#/JavaScript的类(ExampleClass.cs/js)将模仿AS3的实现,当然,你只要二选一就好了,当编译成Flash的时候,这个AS3的类就会被使用了,如果只是在编辑器里面或者编译别的平台应用时,那就调用C#或Javascript啦,
通过创建一个AS3版本的类,这将使用你在变成flash player时能够使用原生态的AS3库,这个玩意对你想和.net的库交互,但是又不支持flash player输出的工做来说,真的是件好事
ExampleClass.as
public class ExampleClass
{
public static function aStaticFunction() : void
{
trace("aStaticFunction - AS3 Implementation");
}
public static function aStaticFunctionWithParams(a : int) : void
{
trace("aStaticFunctionWithParams - AS3 Implementation");
&#65533;&#65533;}
public static function aStaticFunctionWithReturnType() : int
{
trace("aStaticFunctionWithReturnType - AS3 Implementation");
return 1;
}
public function aFunction() : void
{
trace("aFunction - AS3 Implementation");
}
}
你也可以创建C#和JavaScript的伪类,他们实现非常详细,给两个例子
C# Implementation (ExampleClass.cs)
using UnityEngine;
[NotRenamed]
[NotConverted]
public class ExampleClass
{
[NotRenamed]
public static void aStaticFunction()
{
Debug.Log("aStaticFunction - C# Implementation");
}
[NotRenamed]
public static void aStaticFunctionWithParams(int a)
{
Debug.Log("aStaticFunctionWithParams - C# Implementation");
}
[NotRenamed]
public static int aStaticFunctionWithReturnType()
{
Debug.Log("aStaticFunctionWithReturnType - C# Implementation");
return 1;
}
[NotRenamed]
public void aFunction()
{
Debug.Log("aFunction - C# Implementation");
}
}
JavaScript Implementation (ExampleClass.js)
@NotConverted
@NotRenamed
class ExampleClass
{
@NotRenamed
static function aStaticFunction()
{
Debug.Log("aStaticFunction - JS Implementation");
}
@NotRenamed
static function aStaticFunctionWithParams(a : int)
{
Debug.Log("aStaticFunctionWithParams - JS Implementation");
}
@NotRenamed
static function aStaticFunctionWithReturnType() : int
{
Debug.Log("aStaticFunctionWithReturnType - JS Implementation");
return 1;
}
@NotRenamed
function aFunction()
{
Debug.Log("aFunction - JS Implementation");
}
}
调用方式
还是那句话,我们下面来介绍下Unity生成的flash中是如何调用ActionScript类的,当然只允许在导出是flash的工程时哦,别的情况将会继续调用C#和JavaSript
ExampleClass.aStaticFunction();
ExampleClass.aStaticFunctionWithParams(1);
int returnedValue = ExampleClass.aStaticFunctionWithReturnType();
ExampleClass exampleClass = new ExampleClass();
exampleClass.aFunction();
这个例子将告诉兄弟里浏览器AS3如何和JavaScript交互,说实话,这个貌似和Unity没有关系,不过既然Unity要调用javascript(在Flash Player中),那么就说说吧,当运行的时候,BrowserCommunicator.TestCommunication()方法将注册一个回调函数,当as调用到js时,将弹出一个警告窗口,然后再通过注册的回调函数交互
下面是js部分,需要放swf所在的html里面,这个你懂的
<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)
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#的伪实现类
[NotConverted]
[NotRenamed]
public class BrowserCommunicator
{
[NotRenamed]
public static void TestCommunication()
{
}
}
你也可以在C#和JS中访问Flash的stage
ActionScript.Import("com.unity.UnityNative");
ActionScript.Statement("trace(UnityNative.stage);");
下面是个例子,告诉老兄你C#如何输出flash的一些参数
ActionScript.Import("flash.display.LoaderInfo");
ActionScript.Statement(
"var params:Object = LoaderInfo(UnityNative.stage.loaderInfo).parameters;" +
"var key:String;" +
"for (key in params) {" +
"trace(key + '=' + params[key]);" +
"}"
);

0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论2
您需要登录后才可以回帖 登录

谢谢
12年前
回复

使用道具 举报

这是不错的资料,多谢分享。
12年前
回复

使用道具 举报

您当前使用的浏览器IE内核版本过低会导致网站显示错误

请使用高速内核浏览器或其他浏览器