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

webgl 使用transform:Translate 出现 attempt to call a nil value (method 'Translate'): #980

Closed
HiEdison opened this issue Apr 20, 2022 · 1 comment

Comments

@HiEdison
Copy link

unity2019.4.24f1c1
xlua 2022.4.20 下载的版本(最新)
lua中调用transform:Translate,编辑器、windows(打包后exe)工作正常
webgl 出现错误

LuaException: [string "Cube (1)"]:7: attempt to call a nil value (method 'Translate')
stack traceback:
[string "Cube (1)"]:7: in function <[string "Cube (1)"]:5>
at XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) [0x00000] in <00000000000000000000000000000000>:0

(Filename: currently not available on il2cpp Line: -1)

### lua: 保存一个lua文件
`UnityEngine =CS.UnityEngine

speed = 3
movingRight=true
function Update()
if movingRight then
transform:Translate(UnityEngine.Vector3.right * speed * UnityEngine.Time.deltaTime)
if transform.position.x >= 5 then
movingRight = false
end
else
transform:Translate(UnityEngine.Vector3.left * speed * UnityEngine.Time.deltaTime)
if transform.position.x <= -5 then
movingRight = true;
end
end
end`

c#

` using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class dome1 : MonoBehaviour
{
private XLua.LuaEnv luaenv;
public TextAsset luaTxt;// 依赖上方lua文件
private LuaTable _self;

  void Start()
  {
      //实例化虚拟机
      luaenv = new XLua.LuaEnv();
      //创建表和元表
      _self = luaenv.NewTable();
      LuaTable meta = luaenv.NewTable();
      meta.Set("__index", luaenv.Global);
      _self.SetMetaTable(meta);
      meta.Dispose();

      _self.Set("self", this);
      _self.Set("transform", transform);
      luaenv.DoString(luaTxt.text, transform.name, _self);

      _self.Get("Update", out onUpdate);
  }

  private void OnDestroy()
  {
      onUpdate = null;
      luaenv.Dispose();
  }

  private Action onUpdate;

  // Update is called once per frame
  void Update()
  {
      if (onUpdate != null)
      {
          onUpdate.Invoke();
      }
  }

}`

@HiEdison
Copy link
Author

// #if UNITY_ANDROID
new List(){"UnityEngine.Light", "SetLightDirty"},
new List(){"UnityEngine.Light", "shadowRadius"},
new List(){"UnityEngine.Light", "shadowAngle"},
// #endif
我在ExampleGenConfig.cs 中注释了宏.点击xlua生成代码,再次打包webgl,它正常工作了。

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