Toast.lua.txt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local Toast = {}
  2. local DESTORY_TIME = 2.5
  3. local funDestory
  4. local function StopAllCoroutine()
  5. if funDestory then require("Base/CoroutineHelper.lua"):Stop(funDestory) end
  6. end
  7. local function DestoryToast()
  8. funDestory = require("Base/CoroutineHelper.lua"):Start(function ()
  9. require("Base/CoroutineHelper.lua"):Wait(DESTORY_TIME)
  10. CS.UnityEngine.GameObject.Destroy(Toast.rootPanel)
  11. Toast.rootPanel = nil
  12. end)
  13. end
  14. local function OpenToast()
  15. local canvas = CS.UnityEngine.GameObject.Find('Canvas')
  16. Toast._rootCanvas = canvas
  17. local prefabTaost = LoadResource('CommonUtils/Prefabs/ZUIToastPanel.prefab')
  18. local rootPanel = CS.UnityEngine.GameObject.Instantiate(prefabTaost, canvas.transform)
  19. Toast.rootPanel = rootPanel
  20. end
  21. local function RefreshToastImg(szToastPath)
  22. if nil == szToastPath then
  23. szToastPath = "CommonUtils/Textures/Toast/prompt.png"
  24. end
  25. local rootPanel = Toast.rootPanel
  26. local img = rootPanel.transform:Find("Image").gameObject
  27. require("Base/UIHelper.lua"):LoadSprite(img, szToastPath, szToastPath)
  28. end
  29. function Toast.ToastInfo(szToastPath)
  30. if Toast.rootPanel then
  31. RefreshToastImg(szToastPath)
  32. StopAllCoroutine()
  33. DestoryToast()
  34. return
  35. end
  36. OpenToast()
  37. RefreshToastImg(szToastPath)
  38. DestoryToast()
  39. end
  40. return Toast