async_test.lua.txt 1.6 KB

1234567891011121314151617181920212223242526272829303132
  1. -- Tencent is pleased to support the open source community by making xLua available.
  2. -- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. -- Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  4. -- http://opensource.org/licenses/MIT
  5. -- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  6. local util = require 'xlua.util'
  7. local message_box = require 'message_box'
  8. -------------------------async_recharge-----------------------------
  9. local function async_recharge(num, cb) --模拟的异步充值
  10. print('requst server...')
  11. cb(true, num)
  12. end
  13. local recharge = util.async_to_sync(async_recharge)
  14. -------------------------async_recharge end----------------------------
  15. local buy = function()
  16. message_box.alert("您余额不足,请充值!", "余额提醒")
  17. if message_box.confirm("确认充值10元吗?", "确认框") then
  18. local r1, r2 = recharge(10)
  19. print('recharge result:', r1, r2)
  20. message_box.alert("充值成功!", "提示")
  21. else
  22. print('cancel')
  23. message_box.alert("取消充值!", "提示")
  24. end
  25. print('recharge finished')
  26. end
  27. --将按钮监听点击事件,绑定buy方法
  28. CS.UnityEngine.GameObject.Find("Button"):GetComponent("Button").onClick:AddListener(util.coroutine_call(buy))