24年1月17日再次更新,unity通知我bug将在2023.2.7f1版本中修复
代码如下
using System;
using UnityEngine;
public class testMB : MonoBehaviour
{
void Start()
{
test2 t = new test2();
t.Run();
}
}
public class test2 : test
{
public override void A(string s)
{
Debug.Log("O:" + s);
}
}
public class test
{
public virtual void A(string s)
{
Debug.Log("V:" + s);
}
public void Run()
{
Action<string> o = A;
object action = o;
var invokeMethod = action.GetType().GetMethod("Invoke");
invokeMethod.Invoke(action, new object[] { "1-1" });
invokeMethod.Invoke(action, new object[] { "1-2" });
var invokeMethod2 = action.GetType().GetMethod("Invoke");
invokeMethod2.Invoke(action, new object[] { "2-1" });
var invokeMethod3 = action.GetType().GetMethod("Invoke");
invokeMethod3.Invoke(action, new object[] { "3-1" });
}
}
输出结果如下:
O:1-1
V:1-2
V:2-1
V:3-1
第二次开始,没有执行重写方法,而是执行了原始方法!
经过多台电脑测试,在unity2022上错误,在unity2020等之前版本上正常。
与切换.net版本无关。
大家也可以试一下这段代码。
我于23年11月9日向Untiy官方反馈了此BUG,他们也确认了此BUG
再次更新,23年12月15日,unity通知我bug已修复
24年1月17日再次更新,unity通知我bug将在2023.2.7f1版本中修复