Lua协同

TestCoroutine2.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. //两套协同勿交叉使用,类unity原生,大量使用效率低
  5. public class TestCoroutine2 : LuaClient
  6. {
  7. string script =
  8. @"
  9. function CoExample()
  10. WaitForSeconds(1)
  11. print('WaitForSeconds end time: '.. UnityEngine.Time.time)
  12. WaitForFixedUpdate()
  13. print('WaitForFixedUpdate end frameCount: '..UnityEngine.Time.frameCount)
  14. WaitForEndOfFrame()
  15. print('WaitForEndOfFrame end frameCount: '..UnityEngine.Time.frameCount)
  16. Yield(null)
  17. print('yield null end frameCount: '..UnityEngine.Time.frameCount)
  18. Yield(0)
  19. print('yield(0) end frameCime: '..UnityEngine.Time.frameCount)
  20. local www = UnityEngine.WWW('http://www.baidu.com')
  21. Yield(www)
  22. print('yield(www) end time: '.. UnityEngine.Time.time)
  23. local s = tolua.tolstring(www.bytes)
  24. print(s:sub(1, 128))
  25. print('coroutine over')
  26. end
  27. function TestCo()
  28. StartCoroutine(CoExample)
  29. end
  30. local coDelay = nil
  31. function Delay()
  32. local c = 1
  33. while true do
  34. WaitForSeconds(1)
  35. print('Count: '..c)
  36. c = c + 1
  37. end
  38. end
  39. function StartDelay()
  40. coDelay = StartCoroutine(Delay)
  41. end
  42. function StopDelay()
  43. StopCoroutine(coDelay)
  44. coDelay = nil
  45. end
  46. ";
  47. protected override void OnLoadFinished()
  48. {
  49. base.OnLoadFinished();
  50. luaState.DoString(script, "TestCoroutine2.cs");
  51. LuaFunction func = luaState.GetFunction("TestCo");
  52. func.Call();
  53. func.Dispose();
  54. func = null;
  55. }
  56. //屏蔽,例子不需要运行
  57. protected override void CallMain() { }
  58. bool beStart = false;
  59. void OnGUI()
  60. {
  61. if (GUI.Button(new Rect(50, 50, 120, 45), "Start Counter"))
  62. {
  63. if (!beStart)
  64. {
  65. beStart = true;
  66. LuaFunction func = luaState.GetFunction("StartDelay");
  67. func.Call();
  68. func.Dispose();
  69. }
  70. }
  71. else if (GUI.Button(new Rect(50, 150, 120, 45), "Stop Counter"))
  72. {
  73. if (beStart)
  74. {
  75. beStart = false;
  76. LuaFunction func = luaState.GetFunction("StopDelay");
  77. func.Call();
  78. func.Dispose();
  79. }
  80. }
  81. }
  82. }

?