From 54094ff7bb1d5930fdcd71b5b704b0483de458ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E5=8D=B4?= <479813005@qq.com> Date: Wed, 4 Sep 2024 20:07:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E6=AD=A5=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=9C=A8lua=E4=B8=AD=E7=83=AD=E6=9B=B4=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=20(#1134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加异步函数热更示例 * 增加异步函数热更示例 * 在readme中增加异步函数热更示例 --- Assets/XLua/Examples/14_HotfixAsyncAwait.meta | 8 + .../HotfixAsyncAwaitTest.cs | 295 ++++++++++++++++++ .../HotfixAsyncAwaitTest.cs.meta | 11 + .../HotfixAsyncAwaitTest.unity | 239 ++++++++++++++ .../HotfixAsyncAwaitTest.unity.meta | 7 + .../14_HotfixAsyncAwait/Resources.meta | 8 + .../Resources/AsyncMethod1.lua.txt | 6 + .../Resources/AsyncMethod1.lua.txt.meta | 7 + .../Resources/AsyncMethod2.lua.txt | 13 + .../Resources/AsyncMethod2.lua.txt.meta | 7 + .../Resources/AsyncMethod3.lua.txt | 16 + .../Resources/AsyncMethod3.lua.txt.meta | 7 + .../Resources/AsyncMethod4.lua.txt | 13 + .../Resources/AsyncMethod4.lua.txt.meta | 7 + .../Resources/Method1.lua.txt | 6 + .../Resources/Method1.lua.txt.meta | 7 + .../Resources/MyTask.lua.txt | 20 ++ .../Resources/MyTask.lua.txt.meta | 7 + .../Resources/MyTask1.lua.txt | 20 ++ .../Resources/MyTask1.lua.txt.meta | 7 + .../Resources/MyTask2.lua.txt | 15 + .../Resources/MyTask2.lua.txt.meta | 7 + .../Resources/await.lua.txt | 25 ++ .../Resources/await.lua.txt.meta | 7 + README.md | 1 + README_EN.md | 1 + 26 files changed, 767 insertions(+) create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt.meta create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt create mode 100644 Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt.meta diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait.meta new file mode 100644 index 000000000..943ffd9fb --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 401821773f114d54c980b6fd8716010c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs new file mode 100644 index 000000000..854b4f0ef --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs @@ -0,0 +1,295 @@ +using System.Collections; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; +using XLua; + +namespace XLuaTest +{ + [Hotfix] + public partial class HotfixAsyncAwaitTest : MonoBehaviour + { + [Hotfix] + [ContextMenu("Method1")] + public void Method1() + { + Debug.Log($"Method1 in C#!"); + } + + [Hotfix] + [ContextMenu("AsyncMethod1")] +#pragma warning disable CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行 + public async void AsyncMethod1() +#pragma warning restore CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行 + { + Debug.Log($"AsyncMethod1 in C#!"); + } + + [Hotfix] + [ContextMenu("AsyncMethod2")] + public async void AsyncMethod2() + { + Debug.Log($"AsyncMethod2 in C#! 001"); + await Task.Delay(1000); + Debug.Log($"AsyncMethod2 in C#! 002"); + } + + [Hotfix] + [ContextMenu("AsyncMethod3")] + public async void AsyncMethod3() + { + Debug.Log($"AsyncMethod3 in C#! 001"); + await Task.Delay(1000); + Debug.Log($"AsyncMethod3 in C#! 002"); + await MyTask(); + Debug.Log($"AsyncMethod3 in C#! 003"); + } + + [Hotfix] + [ContextMenu("AsyncMethod4")] + public async void AsyncMethod4() + { + Debug.Log($"AsyncMethod4 in C#! 001"); + var result = await MyTask1(); + Debug.Log($"AsyncMethod4 in C#! 002 MyTask1 Result:{result}"); + } + + [Hotfix] + [ContextMenu("MyTask")] + public async Task MyTask() + { + Debug.Log($"MyTask in C#! 001"); + await Task.Delay(1000); + Debug.Log($"MyTask in C#! 002"); + return; + } + + [Hotfix] + [ContextMenu("MyTask1")] + public async Task MyTask1() + { + Debug.Log($"MyTask1 in C#! 001"); + await Task.Delay(1000); + Debug.Log($"MyTask1 in C#! 002"); + return 9999; + } + + [Hotfix] + [ContextMenu("MyTask2")] + public Task MyTask2() + { + Debug.Log($"MyTask2 in C#! 001"); + return Task.FromResult(9998); + } + } + + public partial class HotfixAsyncAwaitTest + { + public TextAsset Method1Lua; + public TextAsset AsyncMethod1Lua; + public TextAsset AsyncMethod2Lua; + public TextAsset AsyncMethod3Lua; + public TextAsset AsyncMethod4Lua; + public TextAsset MyTaskLua; + public TextAsset MyTask1Lua; + public TextAsset MyTask2Lua; + + internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only! + + [ContextMenu("DoHotfix Method1")] + public void DoHotfixMethod1() + { + // 执行脚本 + luaEnv.DoString(Method1Lua.text); + } + + [ContextMenu("DoHotfix AsyncMethod1")] + public void DoHotfixAsyncMethod1() + { + // 执行脚本 + luaEnv.DoString(AsyncMethod1Lua.text); + } + + [ContextMenu("DoHotfix AsyncMethod2")] + public void DoHotfixAsyncMethod2() + { + // 执行脚本 + luaEnv.DoString(AsyncMethod2Lua.text); + } + + [ContextMenu("DoHotfix AsyncMethod3")] + public void DoHotfixAsyncMethod3() + { + // 执行脚本 + luaEnv.DoString(AsyncMethod3Lua.text); + } + + [ContextMenu("DoHotfix AsyncMethod4")] + public void DoHotfixAsyncMethod4() + { + // 执行脚本 + luaEnv.DoString(AsyncMethod4Lua.text); + } + + [ContextMenu("DoHotfix MyTask")] + public void DoHotfixMyTask() + { + // 执行脚本 + luaEnv.DoString(MyTaskLua.text); + } + + [ContextMenu("DoHotfix MyTask1")] + public void DoHotfixMyTask1() + { + // 执行脚本 + luaEnv.DoString(MyTask1Lua.text); + } + + [ContextMenu("DoHotfix MyTask2")] + public void DoHotfixMyTask2() + { + // 执行脚本 + luaEnv.DoString(MyTask2Lua.text); + } + + + void OnGUI() + { + Vector2 size = new Vector2(200, 40); + Vector2 position = new Vector2(600, 100); + Vector2 rightPposition = position + new Vector2(240, 0); + int inteval = 50; + + + if (GUI.Button(new Rect(position, size), nameof(Method1))) + { + Method1(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMethod1))) + { + DoHotfixMethod1(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod1))) + { + AsyncMethod1(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod1))) + { + DoHotfixAsyncMethod1(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod2))) + { + AsyncMethod2(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod2))) + { + DoHotfixAsyncMethod2(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod3))) + { + AsyncMethod3(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod3))) + { + DoHotfixAsyncMethod3(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(AsyncMethod4))) + { + AsyncMethod4(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixAsyncMethod4))) + { + DoHotfixAsyncMethod4(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(MyTask))) + { + MyTask(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask))) + { + DoHotfixMyTask(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(MyTask1))) + { + MyTask1(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask1))) + { + DoHotfixMyTask1(); + } + + position.y += inteval; + rightPposition.y += inteval; + + if (GUI.Button(new Rect(position, size), nameof(MyTask2))) + { + MyTask2(); + } + + if (GUI.Button(new Rect(rightPposition, size), nameof(DoHotfixMyTask2))) + { + DoHotfixMyTask2(); + } + + + string chHint = @"在运行该示例之前,请细致阅读xLua文档,并执行以下步骤: + +1.宏定义:添加 HOTFIX_ENABLE 到 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'。 +(注意:各平台需要分别设置) + +2.生成代码:执行 'XLua > Generate Code' 菜单,等待Unity编译完成。 + +3.注入:执行 'XLua > Hotfix Inject In Editor' 菜单。注入成功会打印 'hotfix inject finish!' 或者 'had injected!' 。"; + string enHint = @"Read documents carefully before you run this example, then follow the steps below: + +1. Define: Add 'HOTFIX_ENABLE' to 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'. +(Note: Each platform needs to set this respectively) + +2.Generate Code: Execute menu 'XLua > Generate Code', wait for Unity's compilation. + + +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."; + GUIStyle style = GUI.skin.textArea; + style.normal.textColor = Color.red; + style.fontSize = 16; + GUI.TextArea(new Rect(10, 100, 500, 290), chHint, style); + GUI.TextArea(new Rect(10, 400, 500, 290), enHint, style); + } + } +} + + + + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs.meta new file mode 100644 index 000000000..09f67fbe7 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 95b78f460e49eea41a858079fd008ef3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity new file mode 100644 index 000000000..9b687b63a --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity @@ -0,0 +1,239 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.2, g: 0.2, b: 0.2, a: 1} + m_AmbientEquatorColor: {r: 0.2, g: 0.2, b: 0.2, a: 1} + m_AmbientGroundColor: {r: 0.2, g: 0.2, b: 0.2, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &4 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 1 + m_BakeResolution: 50 + m_AtlasSize: 1024 + m_AO: 1 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 0 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 1 + m_BakeBackend: 0 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 512 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 0 + m_PVRDenoiserTypeDirect: 0 + m_PVRDenoiserTypeIndirect: 0 + m_PVRDenoiserTypeAO: 0 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 0 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 4890085278179872738, guid: 8c5ded13af488ad48bb6922d92d28040, type: 2} +--- !u!196 &5 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666666 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &1829375650 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1829375656} + - component: {fileID: 1829375655} + - component: {fileID: 1829375653} + - component: {fileID: 1829375652} + - component: {fileID: 1829375657} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1829375652 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1829375650} + m_Enabled: 1 +--- !u!124 &1829375653 +Behaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1829375650} + m_Enabled: 1 +--- !u!20 &1829375655 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1829375650} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 0 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &1829375656 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1829375650} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1829375657 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1829375650} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 95b78f460e49eea41a858079fd008ef3, type: 3} + m_Name: + m_EditorClassIdentifier: + Method1Lua: {fileID: 4900000, guid: a59f9c8e574bd1e468d14345150310b0, type: 3} + AsyncMethod1Lua: {fileID: 4900000, guid: 7adbdc2e78a73584b913b58f384be6a8, type: 3} + AsyncMethod2Lua: {fileID: 4900000, guid: 9c4e4d89f12c0f04eac6514685f20c97, type: 3} + AsyncMethod3Lua: {fileID: 4900000, guid: 891ada61bf6814041aaa71aa9e6b3672, type: 3} + AsyncMethod4Lua: {fileID: 4900000, guid: fb1cf630ec008e34fa7c390922442d07, type: 3} + MyTaskLua: {fileID: 4900000, guid: 88db0679d06b9f042894e74b462bb3ca, type: 3} + MyTask1Lua: {fileID: 4900000, guid: 042048e04fec74f41ab573d2a01e5569, type: 3} + MyTask2Lua: {fileID: 4900000, guid: 61677c80fedcddc49b602a7ae3d1b3b3, type: 3} diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity.meta new file mode 100644 index 000000000..879cc0783 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/HotfixAsyncAwaitTest.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7ece71c1ce3943d4b8b9d0d2bfdc531d +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources.meta new file mode 100644 index 000000000..c9d18c8ba --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5c7d25082887a994485c2a81fd8b00ba +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt new file mode 100644 index 000000000..13c64582d --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt @@ -0,0 +1,6 @@ +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'AsyncMethod1', function(self) + CS.UnityEngine.Debug.Log("AsyncMethod1 in lua!") + end +) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt.meta new file mode 100644 index 000000000..1120b1e30 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod1.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7adbdc2e78a73584b913b58f384be6a8 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt new file mode 100644 index 000000000..98b4819a9 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt @@ -0,0 +1,13 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task + +local function AsyncMethod2(self) + CS.UnityEngine.Debug.Log("AsyncMethod2 in lua! 001") + await(Task.Delay(1000), function () + CS.UnityEngine.Debug.Log("AsyncMethod2 in lua! 002") + end) +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'AsyncMethod2', AsyncMethod2) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt.meta new file mode 100644 index 000000000..1ca80da5a --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod2.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9c4e4d89f12c0f04eac6514685f20c97 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt new file mode 100644 index 000000000..035e9fd22 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt @@ -0,0 +1,16 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task + +local function AsyncMethod3(self) + CS.UnityEngine.Debug.Log("AsyncMethod3 in lua! 001") + await(Task.Delay(1000), function () + CS.UnityEngine.Debug.Log("AsyncMethod3 in lua! 002") + await(self:MyTask(), function () + CS.UnityEngine.Debug.Log("AsyncMethod3 in lua! 003") + end) + end) +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'AsyncMethod3', AsyncMethod3) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt.meta new file mode 100644 index 000000000..409cca8f1 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod3.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 891ada61bf6814041aaa71aa9e6b3672 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt new file mode 100644 index 000000000..d66e361ef --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt @@ -0,0 +1,13 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task + +local function AsyncMethod4(self) + CS.UnityEngine.Debug.Log("AsyncMethod4 in lua! 001") + await(self:MyTask1(), function (result) + CS.UnityEngine.Debug.Log("AsyncMethod4 in lua! 002 MyTask1 Result:"..tostring(result)) + end) +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'AsyncMethod4', AsyncMethod4) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt.meta new file mode 100644 index 000000000..3e8e531a0 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/AsyncMethod4.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: fb1cf630ec008e34fa7c390922442d07 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt new file mode 100644 index 000000000..4a8cf4ee1 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt @@ -0,0 +1,6 @@ +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'Method1', function(self) + CS.UnityEngine.Debug.Log("Method1 in lua!") + end +) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt.meta new file mode 100644 index 000000000..1f8f7ce28 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/Method1.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a59f9c8e574bd1e468d14345150310b0 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt new file mode 100644 index 000000000..d4806b21e --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt @@ -0,0 +1,20 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task +local TaskSource = CS.System.Threading.Tasks.TaskCompletionSource(CS.System.Int32) + +local function MyTask(self) + --使用TaskCompletionSource来模拟C#中async关键字 + local source = TaskSource() + + CS.UnityEngine.Debug.Log("MyTask in lua! 001") + await(Task.Delay(1000), function () + CS.UnityEngine.Debug.Log("MyTask in lua! 002") + source:SetResult(0) + end) + + return source.Task +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'MyTask', MyTask) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt.meta new file mode 100644 index 000000000..230e7c538 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 88db0679d06b9f042894e74b462bb3ca +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt new file mode 100644 index 000000000..cf03d620c --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt @@ -0,0 +1,20 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task +local TaskSource = CS.System.Threading.Tasks.TaskCompletionSource(CS.System.Int32) + +local function MyTask1(self) + --使用TaskCompletionSource来模拟C#中async关键字 + local source = TaskSource() + + CS.UnityEngine.Debug.Log("MyTask1 in lua! 001") + await(Task.Delay(1000), function () + CS.UnityEngine.Debug.Log("MyTask1 in lua! 002") + source:SetResult(9999) + end) + + return source.Task +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'MyTask1', MyTask1) + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt.meta new file mode 100644 index 000000000..a212782f4 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask1.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 042048e04fec74f41ab573d2a01e5569 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt new file mode 100644 index 000000000..296cb58ae --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt @@ -0,0 +1,15 @@ +require 'await' +local Task = CS.System.Threading.Tasks.Task +local FromResult = xlua.get_generic_method(Task, 'FromResult') +local FromIntResult = FromResult(CS.System.Int32) + +local function MyTask2(self) + CS.UnityEngine.Debug.Log("MyTask2 in lua! 001") + return FromIntResult(1000); +end + +xlua.hotfix(CS.XLuaTest.HotfixAsyncAwaitTest, 'MyTask2', MyTask2) + +-- await(MyTask2(nil),function (result) +-- print(result) +-- end) diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt.meta new file mode 100644 index 000000000..6a43aacb8 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/MyTask2.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 61677c80fedcddc49b602a7ae3d1b3b3 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt new file mode 100644 index 000000000..363ec517b --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt @@ -0,0 +1,25 @@ +--如果通过require加载,文件格式不能是UTF-8 with BOM,需要保存为UTF-8 +--或者将await方法复制到util中 + +--- +--- 模拟C# [await](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/await) 关键字 +--- +---@param awaitable any 异步任务实例,或者含有GetAwaiter方法的对象,查看C#可等待对象文档 [查看文档](https://learn.microsoft.com/dotnet/csharp/asynchronous-programming/async-return-types#generalized-async-return-types-and-valuetasktresult) +---@param continuation function 异步延续函数。将C#代码中await关键字下面的代码封装为一个函数,作为异步任务完成时,要执行的回调函数。 [查看文档](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.taskawaiter-1.unsafeoncompleted) +function await(awaitable, continuation) + local awaiter = awaitable:GetAwaiter() + if awaiter.IsCompleted then + --如果同步完成,直接调用异步延续函数 + local result = awaiter:GetResult() + continuation(result) + else + --如果异步挂起,将异步延续函数注册到异步任务中 + awaiter:UnsafeOnCompleted(function () + local result = awaiter:GetResult() + continuation(result) + end) + end +end + + + diff --git a/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt.meta b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt.meta new file mode 100644 index 000000000..43d8e18b5 --- /dev/null +++ b/Assets/XLua/Examples/14_HotfixAsyncAwait/Resources/await.lua.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: effc794ec3dec7645bee0864b61842b2 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md index a9017bf28..9d1a00e23 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ C#主动调用 Lua 也很简单,比如要调用 Lua 的系统函数,推荐 * [10_SignatureLoader](Assets/XLua/Examples/10_SignatureLoader/): 展示如何读取经数字签名的lua脚本,参见[数字签名](Assets/XLua/Doc/signature.md)的文档介绍。 * [11_RawObject](Assets/XLua/Examples/11_RawObject/): 当 C# 参数是object时,如何把一个lua number指定以boxing后的int传递过去。 * [12_ReImplementInLua](Assets/XLua/Examples/12_ReImplementInLua/): 展示如何将复杂值类型改为 Lua 实现。 +* [14_HotfixAsyncAwait](Assets/XLua/Examples/14_HotfixAsyncAwait/): 展示如何将异步函数和await关键字改为 Lua 实现。
diff --git a/README_EN.md b/README_EN.md index 83d49cd34..e282c0faa 100644 --- a/README_EN.md +++ b/README_EN.md @@ -94,6 +94,7 @@ It is recommended that you bind once and reuse it. If code is generated, no gc a * [10_SignatureLoader](Assets/XLua/Examples/10_SignatureLoader/): This example shows how to read the Lua script with a digital signature. See the [Digital Signature](Assets/XLua/Doc/signature.md) document for details. * [11_RawObject](Assets/XLua/Examples/11_RawObject/): This example shows how to specify transferring a Lua number in the int after boxing when the C# parameter is an object. * [12_ReImplementInLua](Assets/XLua/Examples/12_ReImplementInLua/): This shows how to change complex value types to Lua implementations. +* [14_HotfixAsyncAwait](Assets/XLua/Examples/14_HotfixAsyncAwait/): This shows how to change asynchronous functions and await keyword to Lua implementation. ## Technical support