From bcfabbd250b427707a9c1500ba70dc5d5c422bc7 Mon Sep 17 00:00:00 2001 From: johnche Date: Wed, 4 Dec 2024 11:26:46 +0800 Subject: [PATCH] =?UTF-8?q?[unity]il2cpp=E4=BC=98=E5=8C=96=E6=89=93?= =?UTF-8?q?=E5=BC=80=E5=90=8E=EF=BC=8C=E5=AF=B9Delegate.Invoke=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../upm/Runtime/Src/IL2Cpp/TypeRegister.cs | 5 ++- .../test/Src/Cases/CrossLang/DelegateTest.cs | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 unity/test/Src/Cases/CrossLang/DelegateTest.cs diff --git a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs index b239ac8306..6683f59bab 100644 --- a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs +++ b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs @@ -206,6 +206,7 @@ private static void Register(Type type, MethodBase[] ctors = null, MethodBase[] } } } + } Action AddMethodToType = (string name, MethodInfo method, bool isGetter, bool isSetter, bool isExtensionMethod) => { @@ -277,7 +278,9 @@ private static void Register(Type type, MethodBase[] ctors = null, MethodBase[] AddMethodToType(method.Name, method as MethodInfo, false, false, false); } } - + + if (!isDelegate) + { var extensionMethods = ExtensionMethodInfo.Get(type); if (extensionMethods != null) { diff --git a/unity/test/Src/Cases/CrossLang/DelegateTest.cs b/unity/test/Src/Cases/CrossLang/DelegateTest.cs new file mode 100644 index 0000000000..f391f71242 --- /dev/null +++ b/unity/test/Src/Cases/CrossLang/DelegateTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; + +namespace Puerts.UnitTest +{ + public delegate void TestCallback(string msg); + class DelegateTestClass + { + public TestCallback Callback; + + public void CSMessage() + { + Callback("cs_msg"); + } + }; + + [TestFixture] + public class DelegateTest + { + [Test] + public void DelegateBase() + { + var jsEnv = UnitTestEnv.GetEnv(); + + jsEnv.Eval(@" + globalThis.deleteobj = new CS.Puerts.UnitTest.DelegateTestClass() + deleteobj.Callback = (msg) => globalThis.info = msg; + deleteobj.CSMessage(); + "); + + string info = jsEnv.Eval("globalThis.info"); + Assert.AreEqual("cs_msg", info); + + jsEnv.Eval(@"deleteobj.Callback.Invoke('js_msg')"); + info = jsEnv.Eval("globalThis.info"); + Assert.AreEqual("js_msg", info); + } + } +} \ No newline at end of file