创建对象

NewObjFunc.lua.txt

  1. function New()
  2. local obj = CS.UnityEngine.GameObject()
  3. return obj
  4. end
  5. function New(name)
  6. local obj = CS.UnityEngine.GameObject(name)
  7. return obj
  8. end
  9. function NewToParent(parent)
  10. local obj = CS.UnityEngine.GameObject()
  11. obj.transform:SetParent(parent)
  12. return obj
  13. end

NewObj.cs

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class NewObj : MonoBehaviour {
  11. LuaEnv luaEnv = new LuaEnv();
  12. void Start () {
  13. luaEnv.DoString("require 'NewObjFunc'");
  14. GameObject obj1 = CreateObj();
  15. GameObject obj2 = CreateObj("NewObj");
  16. GameObject obj3 = CreateObj(obj2.transform);
  17. }
  18. void Update () {
  19. if(luaEnv != null)
  20. {
  21. luaEnv.Tick();
  22. }
  23. }
  24. private void OnDestroy()
  25. {
  26. luaEnv.Dispose();
  27. }
  28. [XLua.CSharpCallLua]
  29. public delegate GameObject CreateObjDel();
  30. GameObject CreateObj()
  31. {
  32. CreateObjDel del = luaEnv.Global.Get<CreateObjDel>("New");
  33. return del();
  34. }
  35. [XLua.CSharpCallLua]
  36. public delegate GameObject CreateObjByNameDel(string name);
  37. GameObject CreateObj(string name)
  38. {
  39. CreateObjByNameDel del = luaEnv.Global.Get<CreateObjByNameDel>("New");
  40. return del(name);
  41. }
  42. [CSharpCallLua]
  43. public delegate GameObject CreateObjToParentDel(Transform parent);
  44. GameObject CreateObj(Transform parent)
  45. {
  46. CreateObjToParentDel del = luaEnv.Global.Get<CreateObjToParentDel>("NewToParent");
  47. return del(parent);
  48. }
  49. }
  50. }

?