UI事件

Editor目录下MyGenericTypesStaticList.cs

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using System;
  8. using System.Linq;
  9. using System.Reflection;
  10. using XLua;
  11. [LuaCallCSharp]
  12. public static class MyGenericTypeStaticList
  13. {
  14. [CSharpCallLua]
  15. public static List<Type> mymodule_lua_call_cs_list = new List<Type>()
  16. {
  17. typeof(UnityEngine.EventSystems.IPointerDownHandler),
  18. };
  19. [Hotfix]
  20. public static List<Type> by_property
  21. {
  22. get
  23. {
  24. return (from type in Assembly.GetExecutingAssembly().GetTypes()
  25. where type.Namespace == "UnityEngine.EventSystems"
  26. select type).ToList();
  27. }
  28. }
  29. }

Resources目录下LuaUIEventLocal.lua.txt

  1. LuaPanel = {}
  2. local this = LuaPanel
  3. function LuaPanel.Awake(self, csObj)
  4. print("LuaPanel Awake")
  5. this.self = csObj
  6. this.Button = this.self.transform:GetComponentInChildren(typeof(CS.UnityEngine.UI.Button));
  7. this.Button.onClick:AddListener(this.OnBtnClick)
  8. this.Text = this.self.transform:Find("Text"):GetComponent("Text");
  9. this.Text.text = "LuaPanel"
  10. end
  11. function LuaPanel.OnDestroy()
  12. print("LuaPanel OnDestroy")
  13. this.Button.onClick:RemoveListener(this.OnBtnClick)
  14. this.Button.onClick:Invoke()
  15. end
  16. function LuaPanel.OnBtnClick()
  17. print("LuaPanel OnBtnClick")
  18. this.Text.text = "Button Clicked"
  19. end
  20. function LuaPanel.OnPanelClick(eventData)
  21. print("LuaPanel OnPanelClick")
  22. this.Text.text = "LuaPanel"
  23. end
  24. function LuaPanel.OnPointerDown(eventData)
  25. this.OnPanelClick(eventData)
  26. end

Resources目录下UIEventGlobal.lua.txt

  1. this = {}
  2. function Awake(luaEvent)
  3. this.self = luaEvent
  4. this.Button = this.self.transform:GetComponentInChildren(typeof(CS.UnityEngine.UI.Button))
  5. this.Button.onClick:AddListener(OnBtnClick)
  6. this.Text = this.self.transform:Find("Text"):GetComponent("Text")
  7. print("Lua Awake!")
  8. end
  9. function Start()
  10. print("Lua Start!")
  11. end
  12. function Update()
  13. print("Lua Update!")
  14. end
  15. function OnDestroy()
  16. this.Button.onClick:RemoveListener(OnBtnClick)
  17. this.Button.onClick:Invoke()
  18. print("Lua OnDestroy!")
  19. end
  20. function OnBtnClick()
  21. this.Text.text = "Lua OnBtnClick"
  22. print("Lua OnBtnClick!")
  23. end
  24. function OnPanelClick(eventData)
  25. this.Text.text = "Lua OnPanelClick"
  26. print("Lua OnPanelClick!")
  27. end
  28. function OnPointerDown(eventData)
  29. OnPanelClick(eventData)
  30. end

LuaUIEventGlobal.cs

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using System;
  9. using XLua;
  10. namespace shenjun
  11. {
  12. [CSharpCallLua]
  13. public delegate void AwakeSet(LuaUIEventGlobal self);
  14. [CSharpCallLua]
  15. public delegate void OnPanelClick(PointerEventData eventData);
  16. public class LuaUIEventGlobal : MonoBehaviour, IPointerDownHandler {
  17. LuaEnv luaEnv = new LuaEnv();
  18. AwakeSet luaAwake;
  19. Action luaStart;
  20. Action luaUpdate;
  21. Action luaOnDestroy;
  22. OnPanelClick onPanelClickDel;
  23. private void Awake()
  24. {
  25. luaEnv.DoString("require 'UIEventGlobal'");
  26. luaAwake = luaEnv.Global.Get<AwakeSet>("Awake");
  27. luaAwake(this);
  28. luaStart = luaEnv.Global.Get<Action>("Start");
  29. luaUpdate = luaEnv.Global.Get<Action>("Update");
  30. luaOnDestroy = luaEnv.Global.Get<Action>("OnDestroy");
  31. onPanelClickDel = luaEnv.Global.Get<OnPanelClick>("OnPanelClick");
  32. }
  33. void Start () {
  34. luaStart();
  35. }
  36. void Update () {
  37. luaUpdate();
  38. }
  39. private void OnDestroy()
  40. {
  41. luaAwake = null;
  42. luaStart = null;
  43. luaUpdate = null;
  44. luaOnDestroy();
  45. luaOnDestroy = null;
  46. onPanelClickDel = null;
  47. luaEnv.Dispose();
  48. }
  49. public void OnPointerDown(PointerEventData eventData)
  50. {
  51. if (onPanelClickDel != null)
  52. onPanelClickDel(eventData);
  53. }
  54. }
  55. }

LuaUIEventLocal.cs

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using XLua;
  9. namespace shenjun
  10. {
  11. //[CSharpCallLua]
  12. //public delegate void OnPanelClickDel(PointerEventData eventData);
  13. [LuaCallCSharp]
  14. public class LuaUIEventLocal : MonoBehaviour, IPointerDownHandler {
  15. LuaEnv luaEnv = new LuaEnv();
  16. ILuaPanel panel;
  17. IPointerDownHandler panelPointDown;
  18. public void OnPointerDown(PointerEventData eventData)
  19. {
  20. //if (panel.OnPanelClick != null)
  21. //panel.OnPanelClick(eventData);
  22. if (panelPointDown != null)
  23. panelPointDown.OnPointerDown(eventData);
  24. }
  25. void Awake()
  26. {
  27. luaEnv.DoString("require 'LuaUIEventLocal'");
  28. panel = luaEnv.Global.Get<ILuaPanel>("LuaPanel");
  29. panelPointDown = luaEnv.Global.Get<IPointerDownHandler>("LuaPanel");
  30. //panel.self = this; // lua中也会有self的键值
  31. panel.Awake(this);
  32. }
  33. void Start () {
  34. }
  35. void Update () {
  36. if(luaEnv != null)
  37. {
  38. luaEnv.Tick();
  39. }
  40. }
  41. void OnDestroy()
  42. {
  43. panel.OnDestroy();
  44. //if(panel.OnPanelClick != null)
  45. //{
  46. // panel.OnPanelClick = null;
  47. //}
  48. if (panelPointDown != null)
  49. panelPointDown = null;
  50. luaEnv.Dispose();
  51. }
  52. }
  53. [CSharpCallLua]
  54. interface ILuaPanel
  55. {
  56. //LuaUIEventLocal self { get; set; }
  57. //OnPanelClickDel OnPanelClick { get; set; }
  58. void Awake(LuaUIEventLocal self);
  59. void OnDestroy();
  60. }
  61. }

?