Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException: This delegate must add to CSharpCallLua:System.Action #24

Closed
Jayatubi opened this issue Jan 5, 2017 · 6 comments

Comments

@Jayatubi
Copy link
Contributor

Jayatubi commented Jan 5, 2017

我参照Examples里面的02_U3DScripting,将LuaBehaviour中的Awake方法复制到我的代码当中:

void Awake()
{
    m_scriptEnv = s_luaEnv.NewTable();

    LuaTable meta = s_luaEnv.NewTable();
    meta.Set("__index", s_luaEnv.Global);
    m_scriptEnv.SetMetaTable(meta);
    meta.Dispose();

    m_scriptEnv.Set("self", this);
    foreach (var injection in m_injections)
    {
        m_scriptEnv.Set(injection.name, injection.value);
    }

    s_luaEnv.DoString(m_luaScript.text, "LuaBehaviour", m_scriptEnv);

    Action luaAwake = m_scriptEnv.Get<Action>("awake");

    if (luaAwake != null)
    {
        luaAwake();
    }
}

并且依附了一个简答的lua脚本:

function awake()
    print('hello')
end

当时在程序启动的时候出现错误:

InvalidCastException: This delegate must add to CSharpCallLua:System.Action

我对比了我的代码和LuaBehaviour没有什么特别的区别,请问是否还有别的需要设置的部分?

@chexiongsheng
Copy link
Collaborator

faq上有

@Jayatubi
Copy link
Contributor Author

Jayatubi commented Jan 6, 2017

@chexiongsheng 谢谢你的回复。

但是我已经确保清除了生成的代码(没有Gen目录),在编辑器里面直接把Examples里面的LuaBehaviour复制到我的工程当中,仍然提示这个错误。

请问是否还有别的需要注意的部分?

@Jayatubi
Copy link
Contributor Author

Jayatubi commented Jan 6, 2017

@chexiongsheng 找到区别了。除了需要加上[CSharpCallLua]或[LuaCallSharp]标记之外,还需要将ExampleGenConfig.cs里面关于系统类型的注册一并加入到自己的工程里面。希望能在FAQ里面补充这部分说明。

//C#静态调用Lua的配置(包括事件的原型),仅可以配delegate,interface
[CSharpCallLua]
public static List<Type> CSharpCallLua = new List<Type>() {
            typeof(Action),
            typeof(Func<double, double, double>),
            typeof(Action<string>),
            typeof(Action<double>),
            typeof(UnityEngine.Events.UnityAction),
        };

@chexiongsheng
Copy link
Collaborator

异常本身已经提示了解决办法了:“This delegate must add to CSharpCallLua:System.Action”
FAQ上貌似也一直都有:
解决办法,确认XXX(类型名)加上CSharpCallLua后,清除代码后运行。

@Jayatubi
Copy link
Contributor Author

Jayatubi commented Jan 6, 2017

如果FAQ能在这个部分,提供一个指向ExampleGenConfig.cs的链接的话,意思会更明确一些。

@chexiongsheng
Copy link
Collaborator

FAQ关于这问题写得多了,反而人更没耐心看了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants