Unity WebGL MoonSharp崩溃问题
当前Unity的代码更新方案基本都选择的ULua,而我们项目还需要考虑Web平台,ULua不支持WebGL,所以决定选择MoonSharp。MoonSharp(http://www.moonsharp.org/)是一个纯C#实现的Lua解释器,支持多种平台。
测试环境:Unity5.4 + WebGL + MoonSharp 1.6.0.0
测试代码:
double MoonSharpFactorial()
{
string script = @"
-- defines a factorial function
function fact (n)
if (n == 0) then
return 1
else
return n*fact(n - 1)
end
end return fact(5)"; DynValue res = Script.RunString(script);
return res.Number;
}
在Web浏览器中却崩溃了,后经过定位发现是MoonSharp.Interpreter.Loaders.UnityAssetsScriptLoader.LoadResourcesWithReflection出错,Bug和我的修改已经在GitHub提交给作者了,预计很快会合并到主干代码中。
void LoadResourcesWithReflection(string assetsPath)
{
try
{
Type resourcesType = Type.GetType("UnityEngine.Resources, UnityEngine");
Type textAssetType = Type.GetType("UnityEngine.TextAsset, UnityEngine"); MethodInfo textAssetNameGet = textAssetType.GetProperty("name").GetGetMethod();
MethodInfo textAssetTextGet = textAssetType.GetProperty("text").GetGetMethod(); MethodInfo loadAll = resourcesType.GetMethod("LoadAll",
new Type[] { typeof(string), typeof(Type) }); Array array = (Array)loadAll.Invoke(null, new object[] { assetsPath, textAssetType });//此处崩溃,估计是Unity导出代码有Bug for (int i = ; i < array.Length; i++)
{
object o = array.GetValue(i); string name = textAssetNameGet.Invoke(o, null) as string;
string text = textAssetTextGet.Invoke(o, null) as string; m_Resources.Add(name, text);
}
}
catch (Exception ex)
{
#if !PCL
Console.WriteLine("Error initializing UnityScriptLoader : {0}", ex);
#endif
}
}
可以改为
void LoadResourcesWithReflection(string assetsPath)
{
try
{
#if UNITY_WEBGL
UnityEngine.TextAsset[] arrayTextAsset = UnityEngine.Resources.LoadAll<UnityEngine.TextAsset>(assetsPath);
for (int i = ; i < arrayTextAsset.Length; i++)
{
UnityEngine.TextAsset text = arrayTextAsset[i];
m_Resources.Add(text.name, text.text);
}
return;
#endif Type resourcesType = Type.GetType("UnityEngine.Resources, UnityEngine");
Type textAssetType = Type.GetType("UnityEngine.TextAsset, UnityEngine"); MethodInfo textAssetNameGet = textAssetType.GetProperty("name").GetGetMethod();
MethodInfo textAssetTextGet = textAssetType.GetProperty("text").GetGetMethod(); MethodInfo loadAll = resourcesType.GetMethod("LoadAll",
new Type[] { typeof(string), typeof(Type) }); Array array = (Array)loadAll.Invoke(null, new object[] { assetsPath, textAssetType }); for (int i = ; i < array.Length; i++)
{
object o = array.GetValue(i); string name = textAssetNameGet.Invoke(o, null) as string;
string text = textAssetTextGet.Invoke(o, null) as string; m_Resources.Add(name, text);
}
}
catch (Exception ex)
{
#if !PCL
Console.WriteLine("Error initializing UnityScriptLoader : {0}", ex);
#endif
}
}
Unity WebGL MoonSharp崩溃问题的更多相关文章
- 关于 Unity WebGL 的探索(二)
关于 Unity WebGL 的探索(二) 上一篇博客记录了关于 WebGL 移植的第一步:部分 C/C++ 插件的编译,目前项目中的部分插件使用该方法通过,接下来比较大的一部分工作量是网络模块 We ...
- 关于 Unity WebGL 的探索(一)
到今天为止,项目已经上线一个多月了,目前稳定运行,各种 bug 也是有的.至少得到了苹果的两次推荐和 TapTap 一次首页推荐,也算是结项后第一时间对我们项目的一个肯定. 出于各种各样的可描述和不可 ...
- Unity WebGL 窗口自适应
unity 打包好WebGL后,用文本编辑器编辑打包生成的 index.html 文件 在生成的html里面修改代码 <script type="text/javascript ...
- Unity WebGL请求Http接口出现的Cors跨域问题
1.运行环境 (1)WebGL运行浏览器:Firfox Quantum 67.0(64位) (2)服务端API运行环境:IIS,.Net Core 2.1 API 2.问题:CORS 头缺少Acces ...
- Unity WebGL WebSocket
在线示例 http://39.105.150.229/UnityWebSocket/ 快速开始 安装环境 Unity 2018.3 或更高. 无其他SDK依赖. 安装方法 通过 OpenUPM 安装 ...
- Unity WebGL
路过弄了个unity Unity导出WebGL不支持c#socket和unity的network 可以用javascript的websocket实现... c#一般通过www从phpserver获取. ...
- Unity发布WebGL时如何修改默认的载入进度条
Unity发布WebGL版本后,需要去除Unity的Logo,首先关闭Splash Image去除Made with Unity启动画面(在File->Build Settings->Pl ...
- Unity发布WebGl注意事项
unity 版本是5.5,不过看了2017的文档好像也是差不多,绝大部分都是根据官方文档,希望有帮助,如果有错误或者你知道更多这方面的只是,请告知下,大恩言谢. 1:对webgl发布的工程文件说明 ...
- 【Unity】开发WebGL内存概念具体解释和遇到的问题
自增加unity WebGL平台以来.Unity的开发团队就一直致力于优化WebGL的内存消耗. 我们已经在Unity使用手冊上有对于WebGL内存管理的详尽分析,甚至在Unite Europe 20 ...
随机推荐
- Websocket通讯简析
什么是Websocket Websocket是一种全新的协议,不属于HTTP无状态协议,协议名为"ws",这意味着一个Websocket连接地址会是这样的写法:ws://**.We ...
- hibernate中一对多关系中的inverse,cascade属性
举例说明: 一对多关系的两张表:boy.girl(一个男孩可以多个女朋友) boy表结构 Field Type ------ ----------- name varcha ...
- 整合SSM时报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
在整合Spring和MyBatis时,只进行了简单的插入操作,结果报了这个错,顿时整个人都崩溃了 -- 一点都看不懂嘛!只有网上搜索 结果是在使用MyBatis3.x和Spring4.x整合是 导入的 ...
- iOS 应用数据存储方式(XML属性列表-plist)
iOS 应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) ...
- python环境变量自动配置脚本(setx使用)
前言 setx不是windows系统自带的工具,需要到微软官网下载,但是有的系统也会自带.(是官方提供的,可放心食用) set和setx都可以用来配置环境变量.他们的不同点在于,set只是临时的修改环 ...
- Spring MVC基础入门
Spring MVC简介 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱 ...
- Android版:验证手机号码的正则表达式 (转)
/** * 验证手机格式 */ public static boolean isMobileNO(String mobiles) { /* 移动:134.135.136.137 ...
- hdu分类 Dynamic Programming(这是一场漫长的旅途)
下面是difficulty 1的题 1003 Max Sum 最长递增子序列.非常经典,最棒的解法是在线算法O(n)的复杂度. 贴的呢,是用dp做的代码. 先是一个高亮的dp递推式,然后找到最大处 ...
- 【Oracle】oracle10g以后利用q-quote特性简化包含单引号后双引号的字符串写法
The Q-quote delimiter can be any single- or multibyte character except space, tab, and return. If th ...
- [转]passport.js学习笔记
概述 passport.js是Nodejs中的一个做登录验证的中间件,极其灵活和模块化,并且可与Express.Sails等Web框架无缝集成.Passport功能单一,即只能做登录验证,但非常强大, ...