HotfixTest.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using XLua;
  3. namespace XLuaTest
  4. {
  5. [Hotfix]
  6. public class HotfixTest : MonoBehaviour
  7. {
  8. LuaEnv luaenv = new LuaEnv();
  9. private int tick = 0;
  10. // Use this for initialization
  11. void Start()
  12. {
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. if (++tick % 50 == 0)
  18. {
  19. Debug.Log(">>>>>>>>Update in C#, tick = " + tick);
  20. }
  21. }
  22. void OnGUI()
  23. {
  24. if (GUI.Button(new Rect(10, 10, 300, 80), "Hotfix"))
  25. {
  26. luaenv.DoString(@"
  27. xlua.hotfix(CS.XLuaTest.HotfixTest, 'Update', function(self)
  28. self.tick = self.tick + 1
  29. if (self.tick % 50) == 0 then
  30. print('<<<<<<<<Update in lua, tick = ' .. self.tick)
  31. end
  32. end)
  33. ");
  34. }
  35. string chHint = @"在运行该示例之前,请细致阅读xLua文档,并执行以下步骤:
  36. 1.宏定义:添加 HOTFIX_ENABLE 到 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'。
  37. (注意:各平台需要分别设置)
  38. 2.生成代码:执行 'XLua > Generate Code' 菜单,等待Unity编译完成。
  39. 3.注入:执行 'XLua > Hotfix Inject In Editor' 菜单。注入成功会打印 'hotfix inject finish!' 或者 'had injected!' 。";
  40. string enHint = @"Read documents carefully before you run this example, then follow the steps below:
  41. 1. Define: Add 'HOTFIX_ENABLE' to 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'.
  42. (Note: Each platform needs to set this respectively)
  43. 2.Generate Code: Execute menu 'XLua > Generate Code', wait for Unity's compilation.
  44. 3.Inject: Execute menu 'XLua > Hotfix Inject In Editor'.There should be 'hotfix inject finish!' or 'had injected!' print in the Console if the Injection is successful.";
  45. GUIStyle style = GUI.skin.textArea;
  46. style.normal.textColor = Color.red;
  47. style.fontSize = 16;
  48. GUI.TextArea(new Rect(10, 100, 500, 290), chHint, style);
  49. GUI.TextArea(new Rect(10, 400, 500, 290), enHint, style);
  50. }
  51. }
  52. }