|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using UnityEngine; |
| 5 | +using XLua; |
| 6 | + |
| 7 | +namespace XLuaTest |
| 8 | +{ |
| 9 | + [Hotfix] |
| 10 | + public partial class HotfixAsyncAwaitTest : MonoBehaviour |
| 11 | + { |
| 12 | + [Hotfix] |
| 13 | + [ContextMenu("Method1")] |
| 14 | + public void Method1() |
| 15 | + { |
| 16 | + Debug.Log($"Method1 in C#!"); |
| 17 | + } |
| 18 | + |
| 19 | + [Hotfix] |
| 20 | + [ContextMenu("AsyncMethod1")] |
| 21 | +#pragma warning disable CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行 |
| 22 | + public async void AsyncMethod1() |
| 23 | +#pragma warning restore CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行 |
| 24 | + { |
| 25 | + Debug.Log($"AsyncMethod1 in C#!"); |
| 26 | + } |
| 27 | + |
| 28 | + [Hotfix] |
| 29 | + [ContextMenu("AsyncMethod2")] |
| 30 | + public async void AsyncMethod2() |
| 31 | + { |
| 32 | + Debug.Log($"AsyncMethod2 in C#! 001"); |
| 33 | + await Task.Delay(1000); |
| 34 | + Debug.Log($"AsyncMethod2 in C#! 002"); |
| 35 | + } |
| 36 | + |
| 37 | + [Hotfix] |
| 38 | + [ContextMenu("AsyncMethod3")] |
| 39 | + public async void AsyncMethod3() |
| 40 | + { |
| 41 | + Debug.Log($"AsyncMethod3 in C#! 001"); |
| 42 | + await Task.Delay(1000); |
| 43 | + Debug.Log($"AsyncMethod3 in C#! 002"); |
| 44 | + await MyTask(); |
| 45 | + Debug.Log($"AsyncMethod3 in C#! 003"); |
| 46 | + } |
| 47 | + |
| 48 | + [Hotfix] |
| 49 | + [ContextMenu("AsyncMethod4")] |
| 50 | + public async void AsyncMethod4() |
| 51 | + { |
| 52 | + Debug.Log($"AsyncMethod4 in C#! 001"); |
| 53 | + var result = await MyTask1(); |
| 54 | + Debug.Log($"AsyncMethod4 in C#! 002 MyTask1 Result:{result}"); |
| 55 | + } |
| 56 | + |
| 57 | + [Hotfix] |
| 58 | + [ContextMenu("MyTask")] |
| 59 | + public async Task MyTask() |
| 60 | + { |
| 61 | + Debug.Log($"MyTask in C#! 001"); |
| 62 | + await Task.Delay(1000); |
| 63 | + Debug.Log($"MyTask in C#! 002"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + [Hotfix] |
| 68 | + [ContextMenu("MyTask1")] |
| 69 | + public async Task<int> MyTask1() |
| 70 | + { |
| 71 | + Debug.Log($"MyTask1 in C#! 001"); |
| 72 | + await Task.Delay(1000); |
| 73 | + Debug.Log($"MyTask1 in C#! 002"); |
| 74 | + return 9999; |
| 75 | + } |
| 76 | + |
| 77 | + [Hotfix] |
| 78 | + [ContextMenu("MyTask2")] |
| 79 | + public Task<int> MyTask2() |
| 80 | + { |
| 81 | + Debug.Log($"MyTask2 in C#! 001"); |
| 82 | + return Task.FromResult(9998); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public partial class HotfixAsyncAwaitTest |
| 87 | + { |
| 88 | + public TextAsset Method1Lua; |
| 89 | + public TextAsset AsyncMethod1Lua; |
| 90 | + public TextAsset AsyncMethod2Lua; |
| 91 | + public TextAsset AsyncMethod3Lua; |
| 92 | + public TextAsset AsyncMethod4Lua; |
| 93 | + public TextAsset MyTaskLua; |
| 94 | + public TextAsset MyTask1Lua; |
| 95 | + public TextAsset MyTask2Lua; |
| 96 | + |
| 97 | + internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only! |
| 98 | + |
| 99 | + [ContextMenu("DoHotfix Method1")] |
| 100 | + public void DoHotfixMethod1() |
| 101 | + { |
| 102 | + // 执行脚本 |
| 103 | + luaEnv.DoString(Method1Lua.text); |
| 104 | + } |
| 105 | + |
| 106 | + [ContextMenu("DoHotfix AsyncMethod1")] |
| 107 | + public void DoHotfixAsyncMethod1() |
| 108 | + { |
| 109 | + // 执行脚本 |
| 110 | + luaEnv.DoString(AsyncMethod1Lua.text); |
| 111 | + } |
| 112 | + |
| 113 | + [ContextMenu("DoHotfix AsyncMethod2")] |
| 114 | + public void DoHotfixAsyncMethod2() |
| 115 | + { |
| 116 | + // 执行脚本 |
| 117 | + luaEnv.DoString(AsyncMethod2Lua.text); |
| 118 | + } |
| 119 | + |
| 120 | + [ContextMenu("DoHotfix AsyncMethod3")] |
| 121 | + public void DoHotfixAsyncMethod3() |
| 122 | + { |
| 123 | + // 执行脚本 |
| 124 | + luaEnv.DoString(AsyncMethod3Lua.text); |
| 125 | + } |
| 126 | + |
| 127 | + [ContextMenu("DoHotfix AsyncMethod4")] |
| 128 | + public void DoHotfixAsyncMethod4() |
| 129 | + { |
| 130 | + // 执行脚本 |
| 131 | + luaEnv.DoString(AsyncMethod4Lua.text); |
| 132 | + } |
| 133 | + |
| 134 | + [ContextMenu("DoHotfix MyTask")] |
| 135 | + public void DoHotfixMyTask() |
| 136 | + { |
| 137 | + // 执行脚本 |
| 138 | + luaEnv.DoString(MyTaskLua.text); |
| 139 | + } |
| 140 | + |
| 141 | + [ContextMenu("DoHotfix MyTask1")] |
| 142 | + public void DoHotfixMyTask1() |
| 143 | + { |
| 144 | + // 执行脚本 |
| 145 | + luaEnv.DoString(MyTask1Lua.text); |
| 146 | + } |
| 147 | + |
| 148 | + [ContextMenu("DoHotfix MyTask2")] |
| 149 | + public void DoHotfixMyTask2() |
| 150 | + { |
| 151 | + // 执行脚本 |
| 152 | + luaEnv.DoString(MyTask2Lua.text); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + void OnGUI() |
| 157 | + { |
| 158 | + Vector2 size = new Vector2(200, 40); |
| 159 | + Vector2 position = new Vector2(600, 100); |
| 160 | + Vector2 rightPposition = position + new Vector2(240, 0); |
| 161 | + int inteval = 50; |
| 162 | + |
| 163 | + |
| 164 | + if (GUI.Button(new Rect(position, size), nameof(Method1))) |
| 165 | + { |
| 166 | + Method1(); |
| 167 | + } |
| 168 | + |
| 169 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMethod1))) |
| 170 | + { |
| 171 | + DoHotfixMethod1(); |
| 172 | + } |
| 173 | + |
| 174 | + position.y += inteval; |
| 175 | + rightPposition.y += inteval; |
| 176 | + |
| 177 | + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod1))) |
| 178 | + { |
| 179 | + AsyncMethod1(); |
| 180 | + } |
| 181 | + |
| 182 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod1))) |
| 183 | + { |
| 184 | + DoHotfixAsyncMethod1(); |
| 185 | + } |
| 186 | + |
| 187 | + position.y += inteval; |
| 188 | + rightPposition.y += inteval; |
| 189 | + |
| 190 | + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod2))) |
| 191 | + { |
| 192 | + AsyncMethod2(); |
| 193 | + } |
| 194 | + |
| 195 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod2))) |
| 196 | + { |
| 197 | + DoHotfixAsyncMethod2(); |
| 198 | + } |
| 199 | + |
| 200 | + position.y += inteval; |
| 201 | + rightPposition.y += inteval; |
| 202 | + |
| 203 | + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod3))) |
| 204 | + { |
| 205 | + AsyncMethod3(); |
| 206 | + } |
| 207 | + |
| 208 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod3))) |
| 209 | + { |
| 210 | + DoHotfixAsyncMethod3(); |
| 211 | + } |
| 212 | + |
| 213 | + position.y += inteval; |
| 214 | + rightPposition.y += inteval; |
| 215 | + |
| 216 | + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod4))) |
| 217 | + { |
| 218 | + AsyncMethod4(); |
| 219 | + } |
| 220 | + |
| 221 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod4))) |
| 222 | + { |
| 223 | + DoHotfixAsyncMethod4(); |
| 224 | + } |
| 225 | + |
| 226 | + position.y += inteval; |
| 227 | + rightPposition.y += inteval; |
| 228 | + |
| 229 | + if (GUI.Button(new Rect(position, size), nameof(MyTask))) |
| 230 | + { |
| 231 | + MyTask(); |
| 232 | + } |
| 233 | + |
| 234 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask))) |
| 235 | + { |
| 236 | + DoHotfixMyTask(); |
| 237 | + } |
| 238 | + |
| 239 | + position.y += inteval; |
| 240 | + rightPposition.y += inteval; |
| 241 | + |
| 242 | + if (GUI.Button(new Rect(position, size), nameof(MyTask1))) |
| 243 | + { |
| 244 | + MyTask1(); |
| 245 | + } |
| 246 | + |
| 247 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask1))) |
| 248 | + { |
| 249 | + DoHotfixMyTask1(); |
| 250 | + } |
| 251 | + |
| 252 | + position.y += inteval; |
| 253 | + rightPposition.y += inteval; |
| 254 | + |
| 255 | + if (GUI.Button(new Rect(position, size), nameof(MyTask2))) |
| 256 | + { |
| 257 | + MyTask2(); |
| 258 | + } |
| 259 | + |
| 260 | + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask2))) |
| 261 | + { |
| 262 | + DoHotfixMyTask2(); |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + string chHint = @"在运行该示例之前,请细致阅读xLua文档,并执行以下步骤: |
| 267 | +
|
| 268 | +1.宏定义:添加 HOTFIX_ENABLE 到 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'。 |
| 269 | +(注意:各平台需要分别设置) |
| 270 | +
|
| 271 | +2.生成代码:执行 'XLua > Generate Code' 菜单,等待Unity编译完成。 |
| 272 | +
|
| 273 | +3.注入:执行 'XLua > Hotfix Inject In Editor' 菜单。注入成功会打印 'hotfix inject finish!' 或者 'had injected!' 。"; |
| 274 | + string enHint = @"Read documents carefully before you run this example, then follow the steps below: |
| 275 | +
|
| 276 | +1. Define: Add 'HOTFIX_ENABLE' to 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'. |
| 277 | +(Note: Each platform needs to set this respectively) |
| 278 | +
|
| 279 | +2.Generate Code: Execute menu 'XLua > Generate Code', wait for Unity's compilation. |
| 280 | +
|
| 281 | +
|
| 282 | +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."; |
| 283 | + GUIStyle style = GUI.skin.textArea; |
| 284 | + style.normal.textColor = Color.red; |
| 285 | + style.fontSize = 16; |
| 286 | + GUI.TextArea(new Rect(10, 100, 500, 290), chHint, style); |
| 287 | + GUI.TextArea(new Rect(10, 400, 500, 290), enHint, style); |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | + |
| 293 | + |
| 294 | + |
| 295 | + |
0 commit comments