委托类 单例

本文介绍了一个使用C#编写的简单示例,演示了如何通过定义委托和事件来实现松耦合的消息传递机制。该机制允许不同组件注册并响应特定类型的通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;


public class TestDelegate : BehaviourSingleton<TestDelegate>
{
public enum TestType
    {
        test1,
        test2,
        test3
    }
    public delegate void OnBroadcast(TestType type, params object[] objs);
    private Dictionary<TestType, System.Delegate> messageEventTable = new Dictionary<TestType, System.Delegate>();


    public void Subscribe(TestType type, OnBroadcast handler)
    {
        if (!messageEventTable.ContainsKey(type))
        {
            messageEventTable[type] = null;
        }
        messageEventTable[type] = (OnBroadcast)messageEventTable[type] + handler;
    }


    public void Unsubscribe(TestType type, OnBroadcast handler)
    {
        if (!messageEventTable.ContainsKey(type))
        {
            messageEventTable[type] = null;
        }
        messageEventTable[type] = (OnBroadcast)messageEventTable[type] - handler;
    }


    public void RaiseEvent(TestType type, params object[] objs)
    {
        if (messageEventTable.ContainsKey(type))
        {
            OnBroadcast handler = (OnBroadcast)messageEventTable[type];
            if (handler != null)
            {
                Delegate[] list = handler.GetInvocationList();
                bool hasNull = false;
                foreach (Delegate dele in list)
                {
                    if (dele.Target.ToString().Equals("null"))
                    {
                        handler -= (OnBroadcast)dele;
                        messageEventTable[type] = (OnBroadcast)messageEventTable[type] - (OnBroadcast)dele;
                        hasNull = true;
                    }
                }
                handler(type, objs);
            }
        }
    }

}












using UnityEngine;
using System.Collections;


public class Test : MonoBehaviour {


// Use this for initialization
void Start () {
        TestDelegate.Instance.Subscribe(TestDelegate.TestType.test1, ss1);
        TestDelegate.Instance.Subscribe(TestDelegate.TestType.test2, ss2);
        TestDelegate.Instance.Subscribe(TestDelegate.TestType.test3, ss3);




    }

// Update is called once per frame
void Update () {
        if(Input.GetKeyDown(KeyCode.A))
        {
            TestDelegate.Instance.RaiseEvent(TestDelegate.TestType.test1);
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            TestDelegate.Instance.RaiseEvent(TestDelegate.TestType.test2);
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            TestDelegate.Instance.RaiseEvent(TestDelegate.TestType.test3);
        }


    }
    void ss1(TestDelegate.TestType type, params object[] param)
    {
        Debug.Log("1111111111111");
    }
    void ss2(TestDelegate.TestType type, params object[] param)
    {
        Debug.Log("2222222222222222");
    }
    void ss3(TestDelegate.TestType type, params object[] param)
    {
        Debug.Log("3333333333333333333");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值