AsyncTest

AsyncTest.cs

  1. using UnityEngine;
  2. using XLua;
  3. using System.Collections.Generic;
  4. using System;
  5. public class AsyncTest : MonoBehaviour
  6. {
  7. LuaEnv luaenv = null;
  8. void Start()
  9. {
  10. luaenv = new LuaEnv();
  11. luaenv.DoString("require 'async_test'");
  12. }
  13. void Update()
  14. {
  15. if (luaenv != null)
  16. {
  17. luaenv.Tick();
  18. }
  19. }
  20. }

async_test.lua.txt

  1. local util = require 'xlua.util'
  2. local message_box = require 'message_box'
  3. -------------------------async_recharge-----------------------------
  4. local function async_recharge(num, cb) --模拟的异步充值
  5. print('requst server...')
  6. cb(true, num)
  7. end
  8. local recharge = util.async_to_sync(async_recharge)
  9. -------------------------async_recharge end----------------------------
  10. local buy = function()
  11. message_box.alert("余额提醒","您余额不足,请充值!")
  12. if message_box.confirm("确认充值10元吗?","确认框" ) then
  13. local r1, r2 = recharge(10)
  14. print('recharge result', r1, r2)
  15. message_box.alert("提示","充值成功!")
  16. else
  17. print('cancel')
  18. message_box.alert("提示","取消充值!")
  19. end
  20. end
  21. --将按钮监听点击事件,绑定buy方法
  22. CS.UnityEngine.GameObject.Find("Button"):GetComponent("Button").onClick:AddListener(util.coroutine_call(buy))

message_box.lua.txt

  1. local util = require 'xlua.util'
  2. local sync_alert = util.async_to_sync(CS.MessageBox.ShowAlertBox)
  3. local sync_confirm = util.async_to_sync(CS.MessageBox.ShowConfirmBox)
  4. --构造alertconfirm函数
  5. return {
  6. alert = function(title, message)
  7. if not message then
  8. title, message = message, title
  9. end
  10. sync_alert(message,title)
  11. end;
  12. confirm = function(title, message)
  13. local ret = sync_confirm(title,message)
  14. return ret == true
  15. end;
  16. }

MessageBox.cs

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using XLua;
  4. using System.Collections.Generic;
  5. using System;
  6. using UnityEngine.Events;
  7. public class MessageBox : MonoBehaviour{
  8. public static void ShowAlertBox(string message, string title, Action onFinished = null)
  9. {
  10. var alertPanel = GameObject.Find("Canvas").transform.Find("AlertBox");
  11. if (alertPanel == null)
  12. {
  13. alertPanel = (Instantiate(Resources.Load("AlertBox")) as GameObject).transform;
  14. alertPanel.gameObject.name = "AlertBox";
  15. alertPanel.SetParent(GameObject.Find("Canvas").transform);
  16. alertPanel.localPosition = new Vector3(-6f, -6f, 0f);
  17. }
  18. alertPanel.Find("title").GetComponent<Text>().text = title;
  19. alertPanel.Find("message").GetComponent<Text>().text = message;
  20. alertPanel.gameObject.SetActive(true);
  21. if (onFinished != null)
  22. {
  23. var button = alertPanel.Find("alertBtn").GetComponent<Button>();
  24. UnityAction onclick = null;
  25. onclick = () =>
  26. {
  27. onFinished();
  28. alertPanel.gameObject.SetActive(false);
  29. button.onClick.RemoveListener(onclick);
  30. };
  31. button.onClick.RemoveAllListeners();
  32. button.onClick.AddListener(onclick);
  33. }
  34. }
  35. public static void ShowConfirmBox(string message, string title, Action<bool> onFinished = null)
  36. {
  37. var confirmPanel = GameObject.Find("Canvas").transform.Find("ConfirmBox");
  38. if (confirmPanel == null)
  39. {
  40. confirmPanel = (Instantiate(Resources.Load("ConfirmBox")) as GameObject).transform;
  41. confirmPanel.gameObject.name = "ConfirmBox";
  42. confirmPanel.SetParent(GameObject.Find("Canvas").transform);
  43. confirmPanel.localPosition = new Vector3(-8f, -18f, 0f);
  44. }
  45. confirmPanel.Find("confirmTitle").GetComponent<Text>().text = title;
  46. confirmPanel.Find("conmessage").GetComponent<Text>().text = message;
  47. confirmPanel.gameObject.SetActive(true);
  48. if (onFinished != null)
  49. {
  50. var confirmBtn = confirmPanel.Find("confirmBtn").GetComponent<Button>();
  51. var cancelBtn = confirmPanel.Find("cancelBtn").GetComponent<Button>();
  52. UnityAction onconfirm = null;
  53. UnityAction oncancel = null;
  54. Action cleanup = () =>
  55. {
  56. confirmBtn.onClick.RemoveListener(onconfirm);
  57. cancelBtn.onClick.RemoveListener(oncancel);
  58. confirmPanel.gameObject.SetActive(false);
  59. };
  60. onconfirm = () =>
  61. {
  62. onFinished(true);
  63. cleanup();
  64. };
  65. oncancel = () =>
  66. {
  67. onFinished(false);
  68. cleanup();
  69. };
  70. confirmBtn.onClick.RemoveAllListeners();
  71. cancelBtn.onClick.RemoveAllListeners();
  72. confirmBtn.onClick.AddListener(onconfirm);
  73. cancelBtn.onClick.AddListener(oncancel);
  74. }
  75. }
  76. }
  77. public static class MessageBoxConfig
  78. {
  79. [CSharpCallLua]
  80. public static List<Type> CSharpCallLua = new List<Type>()
  81. {
  82. typeof(Action),
  83. typeof(Action<bool>),
  84. typeof(UnityAction),
  85. };
  86. }

?