先写个事件中心脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


/// <summary>
/// 游戏事件中心
/// </summary>
public class EventHandler
{
    public static event Action<InventoryLocation, List<InventoryItem>> UpdateInventoryUI;//注册事件

    public static void CallUpadteInventroyUI(InventoryLocation location, List<InventoryItem> list)//调用事件
    {
        UpdateInventoryUI?.Invoke(location, list);
    }
}

下面是调用CollBack

EventHandler.CallUpadteInventroyUI(InventoryLocation.Player, playerBag.itemList);//调用

注册事件

private void OnEnable()
        {
            EventHandler.UpdateInventoryUI += OnUpdateInventoryUI;
        }

        private void OnDisable()
        {
            EventHandler.UpdateInventoryUI -= OnUpdateInventoryUI;
        }

        private void OnUpdateInventoryUI(InventoryLocation location, List<InventoryItem> list)
        {
            
        }//注册方法
Last modification:April 11th, 2022 at 09:59 am
如果觉得我的文章对你有用,请随意赞赏