问题起源:void DistpatchEvent(EventType, IEventData)
这里的 IEventData是一个接口,代表事件参数。
为了避免每次发送事件产生GC,决定事件参数使用struct,但struct不能继承,所以这里只能使用接口作为基类。
但将结构体传给接口时会发生装箱操作,产生GC。
public class testStructGC : MonoBehaviour { // Start is called before the first frame update void Start() { var ot = new PlayerMoveCmd(); ICmd ic = new PlayerMoveCmd(); } // Update is called once per frame void Update() { } }
对应的IL代码,可以看到
ICmd ic = new PlayerMoveCmd();
这一行产生了装箱操作
.method private hidebysig instance void Start () cil managed { // Method begins at RVA 0x20b0 // Code size 25 (0x19) .maxstack 1