Skip to content

Commit

Permalink
[unity]il2cpp优化打开后,对Delegate.Invoke的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 4, 2024
1 parent c0a018d commit bcfabbd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ private static void Register(Type type, MethodBase[] ctors = null, MethodBase[]
}
}
}
}

Action<string, MethodInfo, bool, bool, bool> AddMethodToType = (string name, MethodInfo method, bool isGetter, bool isSetter, bool isExtensionMethod) =>
{
Expand Down Expand Up @@ -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)
{
Expand Down
40 changes: 40 additions & 0 deletions unity/test/Src/Cases/CrossLang/DelegateTest.cs
Original file line number Diff line number Diff line change
@@ -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<string>("globalThis.info");
Assert.AreEqual("cs_msg", info);

jsEnv.Eval(@"deleteobj.Callback.Invoke('js_msg')");
info = jsEnv.Eval<string>("globalThis.info");
Assert.AreEqual("js_msg", info);
}
}
}

0 comments on commit bcfabbd

Please sign in to comment.