Unity3d 开发之 ulua 坑的总结

相同的 lua 代码在安卓上能正常运行,但在 IOS 上可能不会正常运行而导致报红,崩溃等,我在使用 lua 编程时遇到的一些坑总结如下:

1. File.ReadAllText, 诸如以下代码在 ios 上会崩:

local path="C:/1.txt";
local file=luanet.import_type("System.IO.File");
local text=file.ReadAllText(path);

正确写法如下:

local path="C:/1.txt";
local file=luanet.import_type("System.IO.File");
local encoding=luanet.import_type("System.Text.Encoding");
local text=file.ReadAllText(path,encoding.UTF8);

需指定编码格式。

2. DateTime.AddMinutes 在 ios 会找不到 AddMinutes 方法,但 AddSeconds 方法是可以的。

local DateTime = luanet.import_type("System.DateTime");
local startTime=DateTime.Parse("...");
--local endTime=startTime:AddMinutes(1);  -- 在 ios 上报红:找不到 AddMinutes 方法
local endTime=startTime:AddSeconds(60);

  

3. 在 ios 64位 release 版本上,使用 Color 会导致崩溃。

local uiLabel=this.transform:Find("Label"):GetComponent("UILabel");
uiLabel.color=Color.red;    -- 其中,Color是ulua提供的类,此行代码将导致崩溃

4. 诸如以下代码在 ios 64 位 release 版本上会 crash,其中 c# 代码为:

public class LoadCardHandler
{
        public GameObject Load(string name, Transform parent, int depth = 0, string defaultName = null)
        {
           ...
        }
}

然后 LoadCardHandler 类通过 wraps 的方式映射到 lua 中,在 lua 中使用如下:

local loader=LoadCardHandler.New();
loader:Load(m_activityItemConfig.ad_pic,m_advertisementParent,100,"11400008");  -- 这行代码将导致崩溃。

5. 诸如以下代码会在 ios 上报红:

local button=rootTrans:GetComponent("UIButton");
button.onClick:Clear();                -- 在ios上此方法会报错

  

6. 以下代码会导致 ios 上闪退:

LuaUtils.ShowLoadSystemAnim("LoadAndClose", DelegateFactory.Action(Conceal));

7. c#的长整型传到lua代码中时会有一点误差,所以最好是把在c#里把长整形转成字符串再传到lua里。

8. 以下方法通过 wrap 的方式映射到 lua 中,在 lua 中调用。在用 xcode6.4 打包的 release 版本,在 64 位手机上运行此方法时将导致未响应。

其中c#的代码为:

    public static string[] StringSplit(string src, params string[] separator)
    {
        return src.Split(separator, StringSplitOptions.RemoveEmptyEntries);
    }

lua代码为:

local lines=LuaHelper.StringSplit(text,"
");        -- 此行代码: ios xcode6.4 出的包,在64位手机上导致未响应。

转载请注明出处:http://www.cnblogs.com/jietian331/p/4971120.html

原文地址:https://www.cnblogs.com/jietian331/p/4971120.html